我的问题是让 Spring 将从表单获取的数据绑定到 JPA 实体。奇怪的是,如果我不查看 BindingResults,它就可以正常工作。 BindingResults 表示,当为字段毕业传入空字符串时,会出现绑定错误,但我知道它确实正确绑定了它们,因为当我不检查 Hibernate 时,会完美更新数据库。有没有办法不必编写逻辑来绕过错误触发的绑定错误?
@Entity
@Table(name="child")
public class Child {
@Id
@Column(name="id")
private Integer childId;
@ManyToOne(fetch=FetchType.EAGER )
@JoinColumn(name="house", referencedColumnName="house")
private House house;
@NotNull()
@Past()
@Column(name="birthday")
private Date birthday;
@Column(name="graduation_date")
private Date graduationDay;
}
我在属性编辑器中尝试了以下几行但无济于事
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy");
registry.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
这是控制器方法处理请求的方法签名
@Controller
@SessionAttributes(value="child")
@RequestMapping(value="child")
public class ChildModController {
@RequestMapping(value="save-child.do", params="update", method = RequestMethod.POST)
public @ResponseBody Map<String,?> updateChild(
HttpServletRequest request,
@Valid @ModelAttribute(value="child")Child child,
BindingResult results)
}
这是我从 BindingResult 类中得到的消息
09:01:36.006 [http-thread-pool-28081(5)] INFO simple - Found fieldError: graduationDay,
Failed to convert property value of type java.lang.String to required type java.util.Date for property graduationDay;
nested exception is org.springframework.core.convert.ConversionFailedException:
Failed to convert from type java.lang.String to type @javax.persistence.Column java.util.Date for value ';
nested exception is java.lang.IllegalArgumentException