2010-12-09 22 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(); 
    } 
}