JSF实现注册时的二次密码验证

这篇博文介绍如何使用JSF在用户注册时进行二次密码验证,确保两次输入的密码一致。通过示例代码解析问题并解决注册过程中可能出现的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在我的上篇博文中已经为大家讲解了如何使用JSF实现登录时验证用户名和密码是否匹配的问题


在这篇博文中,我将使用类似的方法为大家实现注册时二次验证密码的功能。


注册页面:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Register</title>
    </h:head>
    <h:body>
        <h:form>
            <div>
                <h:outputText value="密码:"/>  
                <h:inputSecret id="password"
                               title="请输入你的密码"
                               value="${register.password}"
                               required="true"
                               requiredMessage="密码必填"
                               maxlength="20"
                               />
                <h:message for="password" style="color:red"/>
                <br/>
            </div>
            <div class="form-group">
                <h:outputText value="确认密码:" />
                <h:inputSecret id="confirmPassword"
                               value="#{register.passwordConfirm}"
                               title="请再次输入你的密码" 
                               required="true"  
                               requiredMessage="请确认密码"
                               validator = "#{register.isPasswordRight}"
                               />
                <h:message for="confirmPassword" style="color:red"/>
                <br/>
            </div>
            <div>
                <h:commandButton  id="register" value="注册"></h:commandButton>                        
            </div>
        </h:form>        
    </h:body>
</html>


对应的backing bean


import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlInputSecret;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author ambers
 */
@ManagedBean(name = "register")
@RequestScoped
public class RegisterBean {

    private String password;
    private String passwordConfirm;

    /**
     * Creates a new instance of RegisterBean
     */
    public RegisterBean() {
    }


    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPasswordConfirm() {
        return passwordConfirm;
    }

    public void setPasswordConfirm(String passwordConfirm) {
        this.passwordConfirm = passwordConfirm;
    }
    
    

    public void isPasswordRight(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        UIComponent c = null;

        for (UIComponent ui : component.getParent().getChildren()) {
            if ("password".equals(ui.getId())) {
                c = ui;
                break;
            }
        }

        HtmlInputText htmlInputText = (HtmlInputText) c;
        if (!value.toString().trim().equals(htmlInputText.getValue().toString().trim())) {
            FacesMessage msg = new FacesMessage("两次输入密码不一致!");
            throw new ValidatorException(msg);
        }
    }
}

按我的上篇博文中所讲的方法大家可以不难写出上述的代码,但是大家运行之后就会发现这样写是错误的。这是为什么呢?好好看看报错信息,仔细思考一下吧!相信聪明的猿友们都不难得出答案。


实在想不出来?戳这里-->我是答案<---


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值