Có bạn luôn có thể làm điều đó .Spring có một CustomNumberEditor
đó là một trình soạn thảo bất động sản tùy chỉnh cho bất kỳ Số lớp con như Integer, Long, Float , Double.It được đăng ký theo mặc định bởi BeanWrapperImpl, nhưng, có thể được ghi đè bằng cách đăng ký dụ tùy chỉnh của nó như tùy chỉnh editor.It có nghĩa là bạn có thể mở rộng một lớp như thế này
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 super.getAsText();
return "Your desired text";
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
super.setAsText("set your desired text");
}
}
Và sau đó đăng ký nó bình thường trong bạn điều khiển :
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Float.class,new MyCustomNumberEditor(Float.class, true));
}
Điều này sẽ thực hiện tác vụ.
Nguồn
2013-02-20 10:06:01
Cảm ơn, hoạt động như sự quyến rũ –
Chào mừng bạn ... –