Spring mvc form data binding, processing of Basic Types and dates WebDataBinder

Source: Internet
Author: User

An error occurs when binding form forms of the native basic type. You need to specify a specific type editor. The usage is as follows: first add the method initBinder in BaseAction, and use annotation @ InitBinder to annotate, then spring mvc will first register these editors before binding the form. All the remaining controllers inherit the class. CustomDateEditor spring provides it by yourself. This is the base class BaseAction: [java] public class BaseAction {@ InitBinder protected void initBinder (WebDataBinder binder) {binder. registerCustomEditor (Date. class, new CustomDateEditor (new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"), true); binder. registerCustomEditor (int. class, new IntegerEditor (); binder. registerCustomEditor (long. class, new LongEditor (); binder. registerCustomEditor (double. class, new DoubleEd Itor (); binder. registerCustomEditor (float. class, new FloatEditor ();} below are various editor classes: Spring provides CustomNumberEditor, or you can skip some editor classes. Use the constructor to construct the Corresponding Editor class. [Java] public class DoubleEditor extends PropertiesEditor {@ Override public void setAsText (String text) throws IllegalArgumentException {if (text = null | text. equals ("") {text = "0";} setValue (Double. parseDouble (text);} @ Override public String getAsText () {return getValue (). toString () ;}} [java] import org. springframework. beans. propertyeditors. propertiesEditor; public class IntegerEditor extends PropertiesEditor {@ Override public void setAsText (String text) throws IllegalArgumentException {if (text = null | text. equals ("") {text = "0";} setValue (Integer. parseInt (text);} @ Override public String getAsText () {return getValue (). toString () ;}} [java] public class LongEditor extends PropertiesEditor {@ Override public void setAsText (String text) throws IllegalArgumentException {if (text = null | text. equals ("") {text = "0";} setValue (Long. parseLong (text);} @ Override public String getAsText () {return getValue (). toString () ;}} [java] public class FloatEditor extends PropertiesEditor {@ Override public void setAsText (String text) throws IllegalArgumentException {if (text = null | text. equals ("") {text = "0";} setValue (Float. parseFloat (text);} @ Override public String getAsText () {return getValue (). toString ();}}

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.