4

我的 Spring MVC 有问题。

@RequestMapping(value = "/novo", method = RequestMethod.GET)
public ModelAndView novoTimeSheet() {
    return new ModelAndView("timesheetcrud/novo", "timesheet", new TimeSheet());
}

我的TimeSheet班级有:

@NotNull(message = "timesheet.cadastro.horainicio.obrigatorio")
@Temporal(TemporalType.TIME)
@Column(name = "INICIO", nullable = false)
private Date horaInicio;

@NotNull(message = "timesheet.cadastro.horafim.obrigatorio")
@Temporal(TemporalType.TIME)
@Column(name = "FIM", nullable = false)
private Date horaFim;

我的addTimeSheet

@RequestMapping(value = "/addtimesheet", method = RequestMethod.POST)
public String addTimeSheet(@ModelAttribute("timesheet")TimeSheet timeSheet,
                         ModelMap model) {

在浏览器中,当我输入不相等的日期时,出现此错误:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'timesheet' on field 'horaFim': rejected value [lala]; 
codes [typeMismatch.timesheet.horaFim,typeMismatch.horaFim,typeMismatch.java.util.Date,typeMismatch]; 
arguments [
  org.springframework.context.support.DefaultMessageSourceResolvable: codes [timesheet.horaFim,horaFim]; 
  arguments []; 
  default message [horaFim]
]; 
default message [
  Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'horaFim'; 
  nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @javax.validation.constraints.NotNull @javax.persistence.Temporal @javax.persistence.Column java.util.Date for value 'lala'; 
  nested exception is java.lang.IllegalArgumentException
]
Field error in object 'timesheet' on field 'horaInicio': rejected value [teste]; 
codes [typeMismatch.timesheet.horaInicio,typeMismatch.horaInicio,typeMismatch.java.util.Date,typeMismatch]; 
arguments [
  org.springframework.context.support.DefaultMessageSourceResolvable: codes [timesheet.horaInicio,horaInicio]; 
  arguments []; 
  default message [horaInicio]
]; 
default message [
  Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'horaInicio'; 
  nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @javax.validation.constraints.NotNull @javax.persistence.Temporal @javax.persistence.Column java.util.Date for value 'teste'; 
  nested exception is java.lang.IllegalArgumentException
]

我该怎么做?期待Date,但我投入String了现场,我想在我的控制器之前进行验证。

4

2 回答 2

10

您需要向控制器添加一些绑定,例如:

@InitBinder
protected void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
于 2013-06-10T01:19:36.950 回答
3

尝试这个:

@RequestMapping(value = "/addtimesheet", method = RequestMethod.POST)
public String addTimeSheet(@ModelAttribute("timesheet") TimeSheet timeSheet,
                           BindingResult bindingResult,
                           ModelMap model) {
  if (bindingResult.hasErrors()) {
    // validation error (binding error) handling code goes here.
  }
}

确保 bindingResult 参数放在 timeSheet 参数之后的右侧。

http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.htm

Handler methods which are annotated with this annotation are allowed to 
have very flexible signatures. They may have arguments of the following types, 
in arbitrary order (except for validation results, which need to follow right 
after the corresponding command object, if desired)
于 2013-06-10T23:49:42.293 回答