2012-04-09 79 views
1

我正在使用JSF 2.1和PrimeFaces 3.2,服務器Tomcat 7開發應用程序。現在,我正在爲表單註冊新用戶。問題在於轉換器。JSF轉換器在PrimeFaces表格驗證後無法工作

我使用幾個標準字段,其中兩個是密碼。我有自定義數據類型的密碼,所以我想使用轉換器將字段中的字符串數據轉換爲Bean中的密碼變量。提交後Primefaces表單使用AJAX,可能存在問題。如果我填寫完整的表格,沒有驗證錯誤,一切正常。但是,如果有一個有效的錯誤,並且沒有轉換器錯誤(我檢查轉換器中的密碼長度),整個表單就完全停止工作。我必須刷新頁面才能再次運行。

這裏有一些來源:

密碼類:

public class Password { 

    public static final short MIN_LENGTH = 5; 

    private String text; 
    private String hash; 

    public Password(String text) { 
     this.text = text; 
     this.hash = Hasher.sha512(text); 
    } 

    /** 
    * Get password instance with known hash only 
    * @param hash SHA-512 hash 
    * @return Password instance 
    */ 
    public static Password getFromHash(String hash) { 
     Password password = new Password(null); 
     password.hash = hash; 
     return password; 
    } 



    @Override 
    public int hashCode() { 
     return hash.hashCode(); 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (obj == null) { 
      return false; 
     } 
     if (getClass() != obj.getClass()) { 
      return false; 
     } 
     final Password other = (Password) obj; 
     if ((this.hash == null) ? (other.hash != null) : !this.hash.equals(other.hash)) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return hash; 
    } 

    /** 
    * @return the text 
    */ 
    public String getText() { 
     return text; 
    } 
} 

密碼器:

<h:form id="registration"> 
    <p:panelGrid columns="3"> 

     <h:outputLabel value="#{commonTxt.email}:" for="email" /> 
     <p:inputText id="email" value="#{userRegistrationForm.email}" required="true" requiredMessage="#{formsTxt.msgEmpty}"> 
      <f:validator validatorId="email" /> 

      <f:validator validatorId="unique" /> 
      <f:attribute name="entity" value="SystemUser" /> 
      <f:attribute name="field" value="email" /> 
      <f:attribute name="uniqueMessage" value="#{formsTxt.nonUniqueEmail}" /> 
     </p:inputText> 
     <p:message for="email" /> 

     <h:outputLabel value="#{usersTxt.password}:" for="password" /> 
     <p:password id="password" value="#{userRegistrationForm.password}" binding="#{password}" autocomplete="off" feedback="true" weakLabel="#{formsTxt.passwordWeak}" goodLabel="#{formsTxt.passwordGood}" strongLabel="#{formsTxt.passwordStrong}" promptLabel="#{formsTxt.passwordPrompt}" /> 
     <p:message for="password" /> 

     <h:outputLabel value="#{usersTxt.passwordCheck}:" for="passwordCheck" /> 
     <p:password id="passwordCheck" value="#{userRegistrationForm.passwordCheck}" binding="#{passwordCheckInput}" autocomplete="off"> 
      <f:validator validatorId="match" /> 
      <f:attribute name="matchAgainst" value="#{password}" /> 
      <f:attribute name="matchMessage" value="#{formsTxt.passwordMismatch}" /> 
     </p:password> 
     <p:message for="passwordCheck" /> 

     <p:column /><p:column /><p:column /> 

     <h:outputLabel value="#{usersTxt.name}:" for="name" /> 
     <p:inputText id="name" value="#{userRegistrationForm.name}" maxlength="255" required="true" requiredMessage="#{formsTxt.msgEmpty}" /> 
     <p:message for="name" /> 


     <f:facet name="footer"> 
      <p:commandButton value="#{usersTxt.register}" action="#{userRegistrationForm.register()}" update="registration" /> 
     </f:facet> 
    </p:panelGrid> 
</h:form> 

我不會發布:

@FacesConverter(forClass = Password.class) 
public class PasswordConverter implements Converter { 

    @Override 
    public Object getAsObject(FacesContext context, UIComponent component, String value) { 
     String text = (String) value; 

     if (text.length() >= Password.MIN_LENGTH) { 
      return new Password(text); 
     } 

     FacesMessage msg = new FacesMessage(Texter.get("forms/forms", "shortPassword").replace("%limit%", String.valueOf(Password.MIN_LENGTH))); 
     msg.setSeverity(FacesMessage.SEVERITY_ERROR); 
     throw new ConverterException(msg); 
    } 

    @Override 
    public String getAsString(FacesContext context, UIComponent component, Object value) { 
     try { 
      Password password = (Password) value; 
      return password.getText(); 
     } catch (Exception ex) { 
      throw new ConverterException(ex); 
     } 
    } 
} 

中的facelet形式豆的代碼,有兩個密碼屬性與getter和setter。

任何有助於解決我的問題的幫助表示讚賞。提前致謝。

回答

1

解決!我只是使用FacesContext.getCurrentInstance().isValidationFailed()來查看驗證是否失敗。如果發生故障,轉換器現在返回空(轉換不會完成),否則轉換器將返回適當的對象。該表單在轉換工作中正常工作。

相關問題