我试图在我的postgres表中使用@InitBinder作为我的2列:
JSP
<form:input type="date" id="start_date" name="start_date" path="startDate"/>
<form:input type="date" id="expiry_date" name="expiry_date" path="expiryDate"/>
控制器
@InitBinder
public void customizeBinding (WebDataBinder binder) {
SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
dateFormatter.setLenient(false);
binder.registerCustomEditor(Date.class, "startDate",
new CustomDateEditor(dateFormatter, true));
binder.registerCustomEditor(Date.class, "expiryDate",
new CustomDateEditor(dateFormatter, true));
}
@RequestMapping(value = {"/create/add","/home/create/add"}, method = RequestMethod.POST)
public String addContractHeader(@ModelAttribute("contractHeader") ContractHeader p, BindingResult bindingResult, Model model) {
System.out.println("ENTER");
this.contractHeaderService.addContractHeader(p);
return "redirect:/home/create";
}
模型
@Column(name = "start_date")
private Date startDate;
@Column(name = "expiry_date")
private Date expiryDate;
错误
descriptionThe server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
root cause
org.hibernate.exception.ConstraintViolationException: could not execute statement
root cause
org.postgresql.util.PSQLException: ERROR: null value in column "start_date" violates not-null constraint
Detail: Failing row contains (5, dfgdsfsd, dsfdsf, sdfdsfsd, sdfsdfdsf, (888) 888-8888, arnold@yahoo.com, License, Draft, Gold, null, MYR, 4444.01, null, null, test).
控制台日志
2017-04-26T16:32:50.125+0800|Info: Hibernate: insert into CONTRACT_HEADER (contact_email, contact_number, contact_person, contract_number, contract_package, contract_status, contract_sum, contract_type, currency_type, customer_address, customer_name, expiry_date, package_other, remarks, start_date) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2017-04-26T16:32:50.126+0800|WARN: SQL Error: 0, SQLState: 23502
2017-04-26T16:32:50.126+0800|ERROR: ERROR: null value in column "start_date" violates not-null constraint
Detail: Failing row contains (7, qqqqq, qqqq, qqqqqqqq, qqqqqq, (888) 888-8888, arnold@yahoo.com, License, Draft, Gold, null, MYR, 4444.01, null, null, dfdfdfd).
当我已经指定04/26/2017和04/30/2017作为两个字段的输入时,为什么它会尝试在我的日期列上插入null,null?
编辑:
和CustomDateEditor
public CustomDateEditor(DateFormat dateFormat, boolean allowEmpty) {
this.dateFormat = dateFormat;
this.allowEmpty = allowEmpty;
this.exactDateLength = -1;
}