2014-09-30 66 views
5

我搜索了很多並沒有找到解決方案。驗證失敗後,輸入值不明確

我使用JSF 2.1和RichFaces的4.2.3,並希望驗證用戶

的登錄數據,我有兩個輸入字段。 一個用戶名和密碼一個,都與@NotEmpty

login.xhtml

<html 
xmlns="http://www.w3.org/1999/xhtml" 
xmlns:ui="http://java.sun.com/jsf/facelets" 
xmlns:h="http://java.sun.com/jsf/html" 
xmlns:f="http://java.sun.com/jsf/core" 
xmlns:rich="http://richfaces.org/rich" 
xmlns:a4j="http://richfaces.org/a4j" 
xmlns:mt="http://java.sun.com/jsf/composite/components"> 
<h:body> 
<ui:composition template="/protected/user/login-template.xhtml"> 
    <ui:define name="panel-navigation"> 
     <ui:include src="/protected/user/login-left-menu.xhtml" /> 
    </ui:define> 
    <ui:define name="contentbody"> 
     <h:form> 
      <div id="content"> 
       <div class="subcolumns"> 
        <div class="c65l"> 
         <div class="subcl"> 
          <p class="sum-error-message" /> 
          <fieldset> 
           <h3> 
            <h:outputText value="#{styleguideMessages.login}" /> 
           </h3> 
           <p> 
            <h:outputText value="#{messages.login_text}" /> 
           </p> 
           <div class="subcolumns"> 
            <mt:inputText 
             id="loginName" 
             value="#{authenticationPM.loginName}" 
             label="#{messages.general_label_loginname}" 
             required="true" /> 
            <div class="c33r validation"></div> 
           </div> 
           <div class="subcolumns"> 
            <mt:inputText 
             id="password" 
             value="#{authenticationPM.password}" 
             label="#{styleguideMessages.db_password}" 
             required="true" 
             secret="true" /> 
            <div class="c33r validation"></div> 
           </div> 
           <div class="subcolumns"> 
            <h:commandLink 
             id="loginButton" 
             action="#{authenticationPM.doLogin}" 
             value="#{styleguideMessages.login}" 
             title="#{styleguideMessages.login}" 
             styleClass="button last" /> 
           </div> 
          </fieldset> 
         </div> 
        </div> 
       </div> 
      </div> 
      <mt:commandButton 
       id="login" 
       action="#{authenticationPM.doLogin}" 
       value="hidden" 
       style="visibility:hidden" /> 
     </h:form> 
     <script 
      src="#{facesContext.externalContext.requestContextPath}/js/lib/loginEnter.js" 
      type="text/javascript" 
      charset="utf-8"></script> 
    </ui:define> 
</ui:composition> 

AuthentificationPM.java

import *; 

@Named 
@SessionScoped 
public class AuthenticationPM extends AbstractPM implements Serializable { 

/** 
* The user name from the login. 
*/ 
private String userName; 

/** 
* password. 
*/ 
private String password; 

/** 
* Returns the login name. 
* 
* @return String 
*/ 
@NotNull 
@Pattern(regexp = "^[^\"/\\[\\]:;|=,+?*<>]*$", message = "{user.loginname.pattern}") 
public String getLoginName() { 
    return userName; 
} 

/** 
* Returns the password. 
* 
* @return String 
*/ 
@NotNull 
public String getPassword() { 
    return password; 
} 

/** 
* Sets the login name. 
* 
* @param loginName 
*   - the login name of the user 
*/ 
public void setLoginName(String loginName) { 
    this.userName = loginName; 
} 

/** 
* Sets the password. 
* 
* @param password 
*   - the password of the user 
*/ 
public void setPassword(String password) { 
    this.password = password; 
} 

} 

如果其中的一個(說密碼)是n沒有填充,驗證失敗並顯示一條消息(應該如此)。

現在我刪除用戶名並輸入密碼。 驗證失敗,因爲用戶名爲空。 它會清除密碼字段並顯示用戶名的消息。

現在錯誤發生:前輸入的用戶名重新出現! 如何防止此行爲?

我知道後工藝驗證,在更新模型值調用應用程序被跳過,呈現響應執行。 As described here.呈現響應獲取存儲在UI組件中的值(在應用請求),並使用它們重新渲染而不是刪除無效值。我試過this solution以及this idea。兩種方式都沒有找到我的組件。

任何幫助或想法都非常讚賞。

問候, 凱文

+0

對我來說,這聽起來像瀏覽器緩存頁面 – 2014-09-30 17:22:22

+2

Valar Morghulis! 我已經像上面提到的[這裏]禁用了緩存(http://stackoverflow.com/questions/5690269/disabling-chrome-cache-for-website-development)。 我們的開發環境不允許瀏覽器緩存頁面。 所以這不是原因。 無論如何,謝謝! – Wavemaster 2014-10-01 07:44:18

回答

3

如果你只是刪除從inputfield的用戶名,該值應提交爲empty string,這將是用戶名一個合法的更換,也符合定義的模式。

我只能假設,你已經在你的web.xml配置

<context-param> 
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name> 
    <param-value>true</param-value> 
</context-param> 

。因此,當您嘗試提交空字段時,它會被解釋爲null,這與您的用戶名屬性上的@NotNull註釋不匹配。因此,該值的設置將被取消,並且它將保持其以前的狀態,因爲它是SessionScoped - 這是已輸入一次的用戶名。

至少這是唯一的szenario當提交一個空字符串時,a(String-)值沒有更新我可以想象。

將此值設置爲true可以重現您描述的行爲。


您可能會問,爲什麼這不適用於您在輸入密碼時刪除它的文本框?由於您使用的是secret="true",即使backing bean屬性爲而非設置爲空(刪除密碼後)並且仍包含錯誤密碼,文本框將不會顯示有關輸入字符串的任何信息(即長度)。

但請注意,僅僅刪除此屬性可能會影響其他開發人員依靠的功能,以獲取null而不是empty string。因此,您應該考慮刪除@NotNull註釋並手動檢查null

+1

謝謝你的回答。它絕對推動我朝着正確的方向前進。看起來我必須維護'',但現在我知道_why_它的行爲就像它一樣。有沒有辦法在設置用戶名值爲''「'時維護bean驗證?由於驗證密碼,它不會輸入我的密碼。 – Wavemaster 2014-11-06 10:47:01