2011-12-07 66 views
1

我的應用程序有一個帶強制複選框的註冊頁面。如果提交表單時沒有勾選複選框,我希望在嘗試提交表單時顯示錯誤消息。rejectValue並在jsp中使用。 spring3 mvc; JSP;

在我的控制,我有:

if(req.getParameter("tcCheck")==null) 
    result.rejectValue("tcCheck","Check.tc", "Need to accept terms and conditions"); 

我的jsp:

<form:form modelAttribute="user" method="post"> 
    ... 
    <form:errors path=tcCheck/> 
    ... 
</form:form> 

在運行時,我得到這個錯誤:

Error 500: org.springframework.beans.NotReadablePropertyException: Invalid property 'tcCheck' of bean class [com.domgen.recupl.domain.User]: Bean property 'tcCheck' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

用戶域對象沒有tcCheck作爲它的一個實例變量,當然它不應該放在User bean中。

我想知道如何在不編輯用戶bean的情況下顯示錯誤信息? 謝謝。

A

回答

0

如何使用errors.rejectValue?有關示例,請參閱here

+0

這是我想已經這樣做並沒有奏效。 –

0

在你的控制器:使用addError方法

if(req.getParameter("tcCheck")==null) 
    result.addError(new ObjectError("tcCheck", "error message")); 



在你的JSP:使用元素屬性,而不是路徑

<form:form modelAttribute="user" method="post"> 
    ... 
    <form:errors element="tcCheck"/> 
    ... 
</form:form> 

幫助!?

+0

試着這樣做。'< form:errors元素=「一些文本」/>'似乎產生與'相同的結果,即所有的錯誤都顯示在一個地方,而不是可分開的。多於一個錯誤,並給出解決方案,所有的錯誤都出現,即使我只是想不出現tcCheck錯誤。 –

0

要使用類似的表單錯誤,您必須在與表單字段匹配的表單支持對象上有一個getter。所以,如果你想使用

​​

您必須在用戶對象上的方法getTcCheck(),或者你的用戶對象的子對象。即

<form:errors path="subObject.tcCheck"/> 

在這種情況下,你可以使用rejectValue(「subObject.tcCheck」,...