Using Spring's MVC, the page parameters are bound directly to the object, and the object has an error when the attribute is date, which needs to be dealt with at this time.
Similarly, other types that need to be processed can also be used in this way.
Adding code to the controller
@InitBinder protected void Initbinder (httpservletrequest request, throws Exception { // For a property that needs to be converted to a date type, use Dateeditor to process binder.registercustomeditor (date. Classnewtrue); }
Dateeditor is a custom processing class that inherits from PropertyEditorSupport and is handled as public void Setastext (String text) throws IllegalArgumentException
PackageCom.elong.activity.web.filter;ImportJava.beans.PropertyEditorSupport;ImportJava.text.DateFormat;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;Importcom.elong.common.util.StringUtils;/*** * (conversion) * * <p> * Revision history: <br> * Modification Date Modified by person Revision Content <br> *-------------------------------------------------<br> * June 15, 2015 6:16:17 User 1.0 Initialization Creation <br> * </p> * *@authorPeng.li *@version1.0 *@sinceJDK1.7*/ Public classDateeditorextendsPropertyEditorSupport {Private Static FinalDateFormat DateFormat =NewSimpleDateFormat ("Yyyy-mm-dd"); Private Static FinalDateFormat TimeFormat =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); PrivateDateFormat DateFormat; Private BooleanAllowEmpty =true; PublicDateeditor () {} Publicdateeditor (DateFormat dateformat) { This. DateFormat =DateFormat; } PublicDateeditor (DateFormat DateFormat,Booleanallowempty) { This. DateFormat =DateFormat; This. AllowEmpty =AllowEmpty; } /*** Parse The Date from the given text, using the specified DateFormat. */@Override Public voidSetastext (String text)throwsIllegalArgumentException {if( This. AllowEmpty &&Stringutils.isblank (text)) { //Treat empty String as null value.SetValue (NULL); } Else { Try { if( This. DateFormat! =NULL) SetValue ( This. Dateformat.parse (text)); Else { if(Text.contains (":") ) SetValue (timeformat.parse (text)); ElseSetValue (Dateformat.parse (text)); } } Catch(ParseException ex) {Throw NewIllegalArgumentException ("Could Not parse Date:" +ex.getmessage (), ex); } } } /*** Format The Date as String, using the specified DateFormat. */@Override PublicString Getastext () {Date value=(Date) getValue (); DateFormat DateFormat= This. DateFormat; if(DateFormat = =NULL) DateFormat=TimeFormat; return(Value! =NULL? Dateformat.format (value): ""); }}
The second is how the annotations are made:
Import Org.springframework.format.annotation.DateTimeFormat;
/** * Check- in date * * @DateTimeFormat (Pattern= "Yyyy-mm-dd HH:mm:ss" Private Date checkintime; /** * Check-out date * /@DateTimeFormat (Pattern= "Yyyy-mm-dd HH:mm:ss" private Date checkouttime ;
For more information see blog: http://relive123-yahoo-com-cn.iteye.com/blog/1678376
Spring MVC Binding object string to date resolves an issue in which the argument cannot be date