2011-11-05 70 views
2

我添加一些代碼來驗證一個javafaces網站領域:在Java中重置屬性面臨

<h:outputLabel value="Password:"></h:outputLabel> 
<h:inputSecret id="password" binding="#{password}" value="#{register.password}" required="true" requiredMessage="Password field must be filled in"></h:inputSecret> 
<h:outputLabel value="Repeat password:"></h:outputLabel> 
<h:inputSecret id="password2" required="true" requiredMessage="Repeat password field must be filled in"> 
    <f:validator validatorId="sameValueValidator"/> 
    <f:attribute name="value" value="#{password.value}"/> 
    <f:attribute name="message" value="Passwords do not match"/> 
</h:inputSecret> 

第一次提交表單的權利屬性值傳遞給驗證。不管用戶在字段中鍵入的內容如何,​​但該值與第一個值相同。任何想法如何重置它?

回答

-1

<h:inputSecret id="password2" required="true" requiredMessage="Repeat password field must be filled in"> <f:validator validatorId="sameValueValidator"/> <f:attribute name="value" value="#{password.value}"/> <f:attribute name="message" value="Passwords do not match"/> </h:inputSecret>

,我想你應該立即作出「真正」的inputSecret

+0

這根本沒有幫助。它將在第一個字段之前驗證第二個字段,因此在第一次提交時'#{password.value}'將爲null。 – BalusC

1

代碼看起來很好,應該是做工精細,假設您的驗證正確實施。只有當您離開該字段爲空時,纔會出現獲取第一個密碼字段的以前提交的值的症狀。那是因爲你通過了#{password.value},當驗證失敗時它將包含以前的值。你應該通過,但是當驗證成功時,它將包含null

如果您在該字段中輸入其他內容時確實獲得了以前的值,那麼可能是由您的問題中顯示的其他內容導致的錯誤。也許在JSF實現中,或者通過代碼中的其他東西。也許你在命令按鈕上使用了<f:ajax>,而不是重新渲染輸入字段。