Yes, you can always do this. Spring has one CustomNumberEditor, which is a custom property editor for any subclass of Number, such as Integer, Long, Float, Double.It is registered by default with BeanWrapperImpl, but can be overridden by registering a user instance of it as a custom editor. That means you can extend a class like this
public class MyCustomNumberEditor extends CustomNumberEditor{
public MyCustomNumberEditor(Class<? extends Number> numberClass, NumberFormat numberFormat, boolean allowEmpty) throws IllegalArgumentException {
super(numberClass, numberFormat, allowEmpty);
}
public MyCustomNumberEditor(Class<? extends Number> numberClass, boolean allowEmpty) throws IllegalArgumentException {
super(numberClass, allowEmpty);
}
@Override
public String getAsText() {
return "Your desired text";
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
super.setAsText("set your desired text");
}
}
And then register it usually in the controller:
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Float.class,new MyCustomNumberEditor(Float.class, true));
}
This should complete the task.
source
share