2012-10-03 21 views

回答

2

只是實現Validator通常的方式。其他人建議/暗示的行動方法肯定不應該進行驗證。如果驗證失敗,那麼默認情況下,操作方法將不會被調用,並且相同的頁面將只會重新顯示驗證錯誤。您可以將密碼字段作爲<f:attribute>傳遞給確認密碼字段,以便驗證器可以獲取其測試值。

這裏的視圖的開球例如:

<h:inputSecret id="password" binding="#{passwordComponent}" value="#{bean.password}" required="true" /> 
<h:message for="password" /> 

<h:inputSecret id="confirm" required="#{not empty password.value}"> 
    <f:validator validatorId="confirmPasswordValidator" /> 
    <f:attribute name="passwordComponent" value="#{passwordComponent}" /> 
</h:inputText> 
<h:message for="confirm" /> 

和驗證:

@FacesValidator("confirmPasswordValidator") 
public class ConfirmPasswordValidator implements Validator { 

    @Override 
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { 
     UIInput passwordComponent = (UIInput) component.getAttributes().get("passwordComponent"); 
     String password = (String) passwordComponent.getValue(); 
     String confirmPassword = (String) value; 

     if (confirmPassword != null && !confirmPassword.equals(password)) { 
      throw new ValidatorException(new FacesMessage(
       FacesMessage.SEVERITY_ERROR, "Confirm password is not the same as password", null)); 
     } 
    } 

} 

該消息將出現在<h:message for="confirm">

請注意,您只需要有密碼作爲後盾豆,不確認密碼的屬性。

private String password; 

// Getter+setter. 

在操作方法中,你可以只返回導航的情況下的結果通常的方式:

public String login() { 
    User user = userService.find(username, password); 

    if (user != null) { 
     FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("user", user); 
     return "success?faces-redirect=true"; 
    } else { 
     FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
      FacesMessage.SEVERITY_WARN, "Unknown login, please try again.", null)); 
     return null; 
    } 
} 

由於JSF 2.0沒有必要詳細導航案件faces-config.xml了,因爲對方的回答提示。


無關的具體問題,你爲什麼還在堅持傳統的JSP視圖技術?自從JSF 2.0開始支持Facelets以來,這已經是deprecated。請考慮migrating JSP到Facelets。然後,您將能夠使用其中的新JSF 2.0 <f:ajax>迷人和強大的Facelets模板功能。

0

您的密碼是String類型。你可以使用String.equals()方法來檢查它們是否相等。

String pass1 ="#s27zaiyt0"; 
    String pass2 ="#s27zaiyt0"; 
    sysout(pass1.equals(pass2)); 
+0

我可以做到這一點,但如何將其重定向到另一個頁面,如果pass1和pass2不相等 –

1

您必須在託管bean中定義登錄函數。這個函數應該返回該值將適用於您的faces-config.xml中描述的導航規則的字符串。

faces-config.xml中將包括:

<navigation-rule> 
    <description> 
     Login 
    </description> 
    <from-view-id>/Login.jsp</from-view-id> 
    <navigation-case> 
     <from-outcome>login_success</from-outcome> 
     <to-view-id>/Success.jsp</to-view-id> 
    </navigation-case> 
    <navigation-case> 
     <from-outcome>login_failed</from-outcome> 
     <to-view-id>/Login_failed.jsp</to-view-id> 
    </navigation-case> 
</navigation-rule> 

Beans.java管理bean將包括登錄功能:在你的登錄

public String login(){ 
    //Compare login and password against a DB, file, etc. 

    if(entered_password.equals(stored_passwd){ 
     return "login_success"; 
    }else{ 
     return "login_failed"; 
    } 
} 

最後, .JSP表格,您將定義一個登錄按鈕(加H:爲登錄名和密碼值的inputText)與此類似:

<h:commandbutton value="LOGIN" action="#{Beans.login}" /> 

所以,當用戶按下登錄按鈕時,login()功能將被執行,並且重定向將根據它的返回值來確定。

+0

這是正確的,但我有一些問題,我有三個字段在jsf –

+0

1.username 2.密碼3.確認密碼。 ....所以如果兩個密碼是相同的,那麼它顯示登錄成功,但仍然是用用戶名 –

+0

進入succes.jsp讓我看看我是否理解......如果'username'和'password'都是正確的,它重定向到succes.jsp。而且,如果'password'和'confirm_password'相等,它也會在succes.jsp中顯示一條消息? – jmrodrigg