spring mvc uses @ InitBinder tag to bind form data

Posted by gidiag on Thu, 17 Oct 2019 21:19:40 +0200

Original link: http://www.cnblogs.com/riasky/p/3509073.html

In spring MVC, Date, double and other types are defined in the bean. Without any processing, Date and double cannot be bound.


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

In my project, I added the method initBinder in BaseController and annotated it with the annotation @ initBinder. Then spring m vc Before binding the form, you will register these editors first. Of course, if you don't have any trouble, you can write them in each controller separately. The rest of the controllers inherit the class. spring itself provides a large number of implementation classes, such as CustomDateEditor, customboolean editor, CustomNumberEditor, etc., which are basically enough.


Of course, we can also not use his own editor classes. Let's 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());  
    } 


 

public class org.springframework.beans.propertyeditors.PropertiesEditor extends java.beans.PropertyEditorSupport {


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

 



Let's do it first. Wash it.

 

 

Reproduced at: https://www.cnblogs.com/riasky/p/3509073.html

Topics: Spring REST Java