Converting String to Date and Date-formatting in Spring-MVC

This week alone I answered the question about date formatting and how to bind Strings to Objects multiple times. If I would get an euro/dollar for everytime I gave the same answer. So I figured maybe it is time to create simple example to show how it is done in Spring. (The sources used are included in the propertyeditors.zip attached to this post, rename to .war to deploy it in tomcat or jetty (tested in Jetty 6.0.2))

For this project we will only use a WebApplicationContext so the only thing we need to do is configure the Spring DispatcherServlet.

Step-By-Step
sbs
org.springframework.web.servlet.DispatcherServlet
1

sbs
*.do

index.jsp

As you see we mapped all the *.do to the dispatcher servlet. Problem with the servlet spec is that the welcome page has to be a physical file. So the index.jsp only does a redirect to index.do. The Servlet is named sbs. As with the Spring convention we then need to create sbs-servlet.xml to get the context loaded. For this example I use the default BeanNameUrlHandlerMapping, which resolve an URI to a Controller.

We also need something to resolve our views (a ViewResolver) so I configured an InternalResourceViewResolver, which looks up jsp in the WEB-INF/jsp directory.

[sourcode language=’xml’]
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd”>

[/code]Well that is it, configuration wise. We have a controller, with a form and a validator, we have 2 views (entry and success).

The controller isn’t that big of a deal, it extends simpleformcontroller and only implements the initBinder method. For binding to other objects then Strings or primitives we need a PropertyEditor. Luckily for us Spring comes shipped with some PropertyEditors. For this example we will use the CustomDatePropertyEditor. The PropertyEditor will convert a String to a Date and vice-versa.

public class DateEntryController extends SimpleFormController {
  protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
    binder.registerCustomEditor(Date.class, editor);
  }
}

The initBinder method as mentioned here, registers a CustomDateEditor which takes a text in the format (dd-MM-yyyy) and transforms that into a valid date.

the line ‘binder.registerCustomEditor(Date.class, editor);‘ tells the binder to register this propertyeditor for all the Date properties on the command object. If there is a need for multiple property editors for the same type you can also specify the name of the property on the command object.

The Validator is a simple one (just to generate some errors and show that valid dates are translated back into strings).

public class DateEntryValidator implements Validator {

  public boolean supports(Class clazz) {
    return DateEntryFormObject.class.isAssignableFrom(clazz);
  }

  public void validate(Object target, Errors errors) {
    DateEntryFormObject commandObject = (DateEntryFormObject) target;
    Date startDate = commandObject.getStartDate();
    Date endDate = commandObject.getEndDate();

    if (startDate == null) {
      errors.rejectValue("startDate", "required", "Starting Date is required.");
    }

    if (startDate != null && endDate != null && endDate.before(startDate)) {
      errors.rejectValue("endDate", "notbefore.startdate", "End date cannot be before start date.");
    }
  }
}

Well that’s all there is. The only remaining things are the 2 jsps and the command object.

public class DateEntryFormObject implements Serializable {

  private Date startDate;
  private Date endDate;

  public Date getEndDate() {
    return endDate;
  }

  public void setEndDate(Date endDate) {
    this.endDate = endDate;
  }

  public Date getStartDate() {
    return startDate;
  }

  public void setStartDate(Date startDate) {
    this.startDate = startDate;
  }
}

entry.jsp

<%@taglib prefix="form" uri="http://www.springframework.org/tags/form">
<table>
  <tr>
    <td colspan="3">Enter dates in format (dd-MM-yyyy)</td>
  </tr>
  <tr>
    <td>Start Date:</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>End Date:</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
  <td colspan="3"><input value="Submit dates" name="enter" type="submit" /></td>
  </tr>
</table>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form">
<table>
  <tr>
    <td colspan="2"><h2>Dates entered</h2></td>
  </tr>
  <tr>
    <td>Start Date:</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>End Date:</td>
    <td>&nbsp;</td>
  </tr>
</table>

There is just one important thing to remember PropertyEditors work only for the command object, not for reference data, or objects stored somewhere else. For those to format correctly you will need the good ol’ fmt tags.

About these ads

One comment on “Converting String to Date and Date-formatting in Spring-MVC

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s