I guess this is turning into a series on time handling, but I wanted to share how to use the data binders in Spring to pass around date classes. After all, you may want to have URLs that end in some representation of a "day" to page through time-sensitive data, or filter your data based on time, etc.
For example, if you wanted to have a URL parameter of the form "yyyy-MM-dd", i.e. the XML Schema format, and marshal it into a java.util.Date, how would you go about doing this?
Well, Spring provides:
- an InitBinder to initialize WebDataBinders on a controller - i.e. setup a the binding mechanism on a Spring controller automatically
- WebDataBinder to register custom property editors to convert certain types. In this case the type is java.util.Date, and the property editor is also provided by Spring - CustomDate Editor
- @RequestParam(value="date", required=false) Date date
- @RequestMapping(value="/date/{date}", method=RequestMethod.GET) ...
- @PathVariable("date") Date date
Here's the code:
Enjoy!
No comments:
Post a Comment