Spring MVC data conversion

Spring MVC data conversion

 

In SpringMVC, Date, double and other types are defined in beans. If no processing is done, neither date nor double can be bound.

 

Workaround: @InitBinder

 

In my project, the method initBinder is added to BaseController and marked with the annotation @InitBinder, so spring mvc will register these editors before binding the form. Of course, if you don't mind the trouble, you can also write them separately in in each of your controllers. The rest of the controllers inherit from this class. Spring itself provides a large number of implementation classes, such as CustomDateEditor, CustomBooleanEditor, CustomNumberEditor, etc., which are basically sufficient.

 

Of course, you can also construct a new Editor yourself.

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();    
    }    
}    

 

 

Generally, a BaseController is built as a parent class, and then all Controllers inherit it, and the editors to be registered are written here.

@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());    
   }   

 

If your editor class directly inherits PropertyEditorSupport it's fine too.

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

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326525488&siteId=291194637