th3an0maly
th3an0maly

Reputation: 3510

JSTL - How to parse input date

I have a date input field in my jsp like this:

<form:input name="inputDate" id="inputDate" value="" path="date"/>

This form is mapped to a @ModelAttribute account which has the date defined like this:

private java.util.Date date ;

When I submit the form, I get an error (of course) that says:

Failed to convert property value of type java.lang.String to required type java.util.Date ...

Please suggest a way I can parse date in the JSP itself, so that the @ModelAttribute can directly set the date field with the value

Upvotes: 0

Views: 1559

Answers (1)

Grzegorz Rożniecki
Grzegorz Rożniecki

Reputation: 28005

Just register binder in your @Controller like in this example:

@Controller
public class MyFormController {

  @InitBinder
  public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class,
        new CustomDateEditor(dateFormat, false));
  }

  // ...
}

Upvotes: 1

Related Questions