2010-12-09 68 views
1

我有一个自定义标记,它扩展了Spring的InputTag以显示",###.0"格式的数字。我为Double类注册了一个自定义PropertyEditor来处理格式。Spring MVC - 选择性格式化

当表单提交并且验证失败时,所有无效值都应该按原样重新显示,不进行格式设置,以便用户可以看到他犯的错误。我如何通知自定义标签的验证结果,以便它不做任何格式?

我使用Spring MVC 3.

谢谢。

回答

1

重写的AbstractDataBoundFormElementTaggetPropertyEditor()方法,和空返回代替PropertyEditor实例(所以ValueFormatter将不会通过对象值PropertyEditor用于格式化目的)。

public class CustomInputTag extends InputTag { 
@Override 
protected PropertyEditor getPropertyEditor() throws JspException { 
    if(getBindStatus().getErrors().hasErrors()) { 
     return null; 
    } 
    return super.getPropertyEditor(); 
    } 
}