1

如何使用确认密码验证密码,login.jsp并且只有在相等的情况下,然后重定向到success.jsp哪个显示Welcome #{beans.username}

4

3 回答 3

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.

在 action 方法中,您可以按通常的方式返回导航案例结果:

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。请考虑将JSP迁移到 Facelets。然后,您将能够使用新的 JSF 2.0<f:ajax>和强大的 Facelets 模板功能。

于 2012-10-03T12:49:05.577 回答
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()函数将被执行,并根据其返回值确定重定向。

于 2012-10-03T07:46:33.177 回答
0

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

   String pass1 ="#s27zaiyt0";
   String pass2 ="#s27zaiyt0";
   sysout(pass1.equals(pass2));
于 2012-10-03T07:32:58.263 回答