如何使用确认密码验证密码然后重定向?

时间:2012-10-03 07:28:59

标签: java jsp java-ee jsf-2

如何在login.jsp中使用确认密码验证密码,并且仅在相等的情况下验证密码,然后重定向到显示success.jsp的{​​{1}}。

3 个答案:

答案 0 :(得分:3)

以通常的方式实施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">中。

请注意,您只需要将密码作为支持bean中的属性,而不是确认密码。

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视图技术?这是deprecated,因为JSF 2.0支持Facelets。请考虑migrating JSP to Facelets。然后,您将能够使用新的JSF 2.0 <f:ajax>强大的功能和强大的Facelets模板功能。

答案 1 :(得分: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";
    }
}

最后,在您的 Login.jsp 表单中,您将定义一个登录按钮(以及用于登录和密码值的h:inputText),类似于:

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

因此,当用户按下登录按钮时,将执行login()功能,并根据其返回值确定重定向。

答案 2 :(得分:0)

因为您的密码是String类型。你可以使用String.equals()方法来检查它们是否相等。

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