Spring MVC bound object string to date

Source: Internet
Author: User
Tags dateformat

Using Spring's MVC, you directly bind page parameters to an object, and an error occurs when a property in the object has a date, which needs to be handled at this time.

Similarly, other types that need to be processed can be used in this way.

Add code to Controller

@InitBinder
protected void Initbinder (HttpServletRequest request,
                              Servletrequestdatabinder binder) throws Exception {
    //for properties that need to be converted to Date types, dateeditor is used for processing
    binder.registercustomeditor (Date.class, New Dateeditor ()) ;
}

Dateeditor is inherited from PropertyEditorSupport for a custom processing class, and the processing method is public void Setastext (String text) throws IllegalArgumentException

Import Org.springframework.util.StringUtils;
Import Java.beans.PropertyEditorSupport;
Import Java.text.DateFormat;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;

Import Java.util.Date; public class Dateeditor extends PropertyEditorSupport {private static final dateformat DateFormat = new Simpledatefo
    Rmat ("Yyyy-mm-dd");

    private static final DateFormat TimeFormat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
    Private DateFormat DateFormat;

    Private Boolean allowempty = true;
    Public Dateeditor () {} public dateeditor (DateFormat dateformat) {this.dateformat = DateFormat;
        Public Dateeditor (DateFormat DateFormat, Boolean allowempty) {This.dateformat = DateFormat;
    This.allowempty = AllowEmpty;
     }/** * Parse the Date from the given text, using the specified DateFormat. */@Override public void Setastext (String text) throws IllegalArgumentException {if (this.allowempty &&!
            Stringutils.hastext (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));
                Else SetValue (dateformat.parse (text)); } catch (ParseException ex) {throw new IllegalArgumentException ("Could not parse Date:" +
            Ex.getmessage (), ex);
     }}/** * Format the Date as String, using the specified DateFormat.
        */@Override public String Getastext () {Date value = (date) getValue ();
        DateFormat DateFormat = This.dateformat;
        if (DateFormat = = null) DateFormat = TimeFormat; return (value!= null Dateformat.format (value): "");
 }
}


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.