Spring mvc uses the @ InitBinder label to bind the form data. mvc @ initbinder

Source: Internet
Author: User

Spring mvc uses the @ InitBinder label to bind the form data. mvc @ initbinder

In SpringMVC, bean defines the Date, double, and Other types. If no processing is done, the Date and double cannot be bound.

The solution is to use the @ InitBinder tag provided by spring mvc.

In my project, I add the method initBinder to BaseController and use annotation @ InitBinder for annotation. Then spring mvc will first register these editors before binding the form. Of course, if you don't bother, you can also write it in each of your controllers. All the remaining controllers inherit the class. Spring itself provides a large number of implementation classes, such as CustomDateEditor, CustomBooleanEditor, and CustomNumberEditor, which are basically enough.

Of course, we can also choose not to use the built-in editor class, then we will construct several

import org.springframework.beans.propertyeditors.PropertiesEditor;  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();    }  }  
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();    }  }  
import org.springframework.beans.propertyeditors.PropertiesEditor;  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();    }  }  
import org.springframework.beans.propertyeditors.PropertiesEditor; 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();    }  } 

In BaseController

@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 CustomNumberEditor(int.class, true));      binder.registerCustomEditor(int.class, new IntegerEditor());  /    binder.registerCustomEditor(long.class, new CustomNumberEditor(long.class, true));     binder.registerCustomEditor(long.class, new LongEditor());      binder.registerCustomEditor(double.class, new DoubleEditor());      binder.registerCustomEditor(float.class, new FloatEditor());    }  

Copy codeThe Code is as follows:
Public class org. springframework. beans. propertyeditors. PropertiesEditor extends java. beans. PropertyEditorSupport {

See? If your editor class inherits PropertyEditorSupport, you can also.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.