My spring application have problem with save 'date" to "database". where is mistake?
error
Failed to convert property value of type [java.lang.String] to required type [java.sql.Date] for property bornDate; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "2016-11-02"
mysql
use lifecalc;
create table Man (
manId int not null auto_increment primary key,
name varchar(30) not null,
bornDate date,
lastDate date
);
insert into man value
(null, "Pawel Cichon", "1920-11-30", "2000-02-20");
entity
@Entity
@Table(name="Man")
public class Man {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="manId")
private int manId;
@Column
private String name;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column
private java.sql.Date bornDate;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column
private java.sql.Date lastDate;
//getter end setter
controller
@Controller
@RequestMapping("/")
public class AppController {
@Autowired
private ManService manService;
@ModelAttribute("man")
public Man modelToAddMan(){
return new Man();
}
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor( dateFormat, true));
}
@RequestMapping(value="/addMan.html", method = RequestMethod.POST)
public String addManFinish(@Valid @ModelAttribute("man") Man man, BindingResult result) {
if(result.hasErrors()){
return "addMan";
} else{
manService.addMan(man);
return "redirect:/index.html";
}
}
addMan.html
<form:form method="POST" modelAttribute="man">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">name</label>
<form:input path="name" />
<form:errors path="name" cssClass="error"/>
</div>
<div class="form-group">
<label for="bornDate" class="col-sm-2 control-label">bornDate</label>
<form:input path="bornDate" />
<form:errors path="bornDate" cssClass="error"/>
</div>
<div class="form-group">
<label for="lastDate" class="col-sm-2 control-label">lastDate</label>
<form:input path="lastDate" />
<form:errors path="lastDate" cssClass="error"/>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success" value="save" /> <a
class="btn btn-danger" role="button"
href="<spring:url value="/index.html" />">cancel </a>
</div>
</form:form>