我怎樣才能在login.jsp
驗證與確認密碼的密碼,只有在平等的,然後重定向到success.jsp
這顯示Welcome #{beans.username}
。如何使用確認密碼驗證密碼,然後重定向?
回答
只是實現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模板功能。
您的密碼是String類型。你可以使用String.equals()方法來檢查它們是否相等。
String pass1 ="#s27zaiyt0";
String pass2 ="#s27zaiyt0";
sysout(pass1.equals(pass2));
您必須在託管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()
功能將被執行,並且重定向將根據它的返回值來確定。
這是正確的,但我有一些問題,我有三個字段在jsf –
1.username 2.密碼3.確認密碼。 ....所以如果兩個密碼是相同的,那麼它顯示登錄成功,但仍然是用用戶名 –
進入succes.jsp讓我看看我是否理解......如果'username'和'password'都是正確的,它重定向到succes.jsp。而且,如果'password'和'confirm_password'相等,它也會在succes.jsp中顯示一條消息? – jmrodrigg
- 1. 如何使用jsp驗證密碼並確認密碼字段?
- 2. 驗證密碼和確認密碼NG2
- 3. 密碼確認密碼驗證檢查
- 4. 驗證確認密碼?
- 5. jquery確認密碼驗證
- 6. AngularJS確認密碼驗證
- 7. 確認密碼驗證
- 8. 密碼確認驗證CakePHP
- 9. jquery驗證確認密碼
- 10. 如何驗證密碼和確認密碼是否相等?
- 11. 如何驗證電子郵件,密碼和確認密碼?
- 12. Jquery密碼和確認密碼驗證不起作用正確
- 13. 密碼匹配(密碼和確認密碼)驗證使用ionic2和angular2
- 14. 如何在加密後確認密碼
- 15. 驗證不適用於密碼並確認密碼
- 16. 登錄驗證密碼/確認密碼不起作用。 PHP/MySQL
- 17. 設計不驗證密碼/密碼確認
- 18. ASP MVC驗證密碼並確認密碼
- 19. 驗證密碼和確認密碼爲XAML
- 20. Spring 3基於批註的驗證:密碼和確認密碼
- 21. html 5驗證密碼並確認密碼
- 22. Javascript:驗證多個密碼並確認密碼字段
- 23. 密碼並確認javascript錯誤中的密碼驗證?
- 24. django確認密碼驗證器
- 25. validatorJS-不驗證密碼確認
- 26. 確認密碼驗證ExtJS的4
- 27. jquery驗證:密碼更改和確認
- 28. 密碼驗證中斷設計確認
- 29. Jquery驗證器確認密碼問題
- 30. hapi route joi驗證密碼確認
我可以做到這一點,但如何將其重定向到另一個頁面,如果pass1和pass2不相等 –