Spring MVC Binding object string to date resolves an issue in which the argument cannot be date

Source: Internet
Author: User
Tags dateformat

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.