Validation 检查注册表单中是否已存在用户名

Validation 检查注册表单中是否已存在用户名,validation,jakarta-ee,primefaces,user-input,Validation,Jakarta Ee,Primefaces,User Input,我有以下登记表: <h:form id="newCustomerForm" > <fieldset> <legend>Register Form</legend> <table border="0" > <tbody >

我有以下登记表:

 <h:form id="newCustomerForm"  >
                <fieldset>
                    <legend>Register Form</legend>
                    <table border="0" >
                        <tbody > 
                            <tr type="text">

                                <td>
                                    <p:outputLabel  value="Account Login :" for="pseudo"/>    
                                </td>
                                <p:spacer height="5px" />
                                <td>
                                    <p:inputText id="pseudo" value="#{customerMB.login}" title="Pseudo" required="true" requiredMessage="The Pseudo field is required."/>
                                    <p:watermark for="pseudo" value="Login" />  
                                    <p:message for="pseudo"/>
                                </td>

                            </tr>
                            <p:spacer height="5px" />  
                            <tr type="password">

                                <td>
                                    <p:outputLabel  value="Password :" for="pwd1"/>
                                </td>
                                <p:spacer height="5px" />
                                <td>
                                    <p:password id="pwd1" value="#{customerMB.password}" feedback="true" match="pwd2" label="Password 1" required="true" requiredMessage="The Password field is required."/>  
                                    <p:watermark for="pwd1" value="Password" />  
                                    <p:message for="pwd1"/>
                                </td>

                            </tr>
                            <p:spacer height="5px" /> 
                            <tr type="password">
                                <td>
                                    <p:outputLabel  value="Confirm password :" for="pwd2"/>
                                </td>
                                <p:spacer height="5px" />
                                <td>
                                    <p:password id="pwd2" value="#{customerMB.password}" feedback="true" label="Password 2" required="true" requiredMessage="The confirm password field is required."/>  
                                    <p:watermark for="pwd2" value="Confirm Password" />  
                                    <p:message for="pwd2"/>
                                </td>
                            </tr>
                            <p:spacer height="5px" />         
                            <tr type="text">
                                <td>
                                    <p:outputLabel  value="Email address :" for="email"/>
                                </td>
                                <p:spacer height="5px" />
                                <td>
                                    <p:inputText id="email" value="#{customerMB.email}"  title="Email" required="true" validatorMessage="Insert a valid email" requiredMessage="The Email field is required.">
                                        <f:validateRegex pattern="([^.@]+)(\.[^.@]+)*@([^.@]+\.)+([^.@]+)" />
                                    </p:inputText>
                                    <p:watermark for="email" value="Email" /> 
                                    <p:message for="email"/>
                                </td>
                            </tr>
                            <p:spacer height="5px" /> 

                            <tr type="text">  
                                <td>
                                    <p:outputLabel  value="First Name :" for="name"/>
                                </td>
                                <p:spacer height="5px" />
                                <td>
                                    <p:inputText id="name" value="#{customerMB.name}" title="Name" required="true" requiredMessage="The Name field is required." styleClass="error"/>
                                    <p:watermark for="name" value="First Name" />  
                                    <p:message for="name"/>
                                </td>
                            </tr>
                            <p:spacer height="5px" />
                            <tr type="text">
                                <td>
                                    <p:outputLabel  value="Last Name :" for="familyName"/>
                                </td>
                                <p:spacer height="5px" />
                                <td>
                                    <p:inputText id="familyName" value="#{customerMB.familyName}" title="FamilyName" required="true" requiredMessage="The FamilyName field is required."/>                                                         
                                    <p:watermark for="familyName" value="Last Name" />  
                                    <p:message for="familyName"/>
                                </td>
                            </tr>
                            <p:spacer height="5px" />
                            <tr type="text">
                                <td>
                                    <p:outputLabel  value="Organization :" for="organisation"/>
                                </td>
                                <p:spacer height="5px" />
                                <td>
                                    <p:inputText id="organisation" value="#{customerMB.organisation}" title="Organisation" required="true" requiredMessage="The Organisation field is required."/>
                                    <p:watermark for="organisation" value="Organisation" />   
                                    <p:message for="organisation"/>
                                </td>
                            </tr>
                            <p:spacer height="5px" />   
                            <tr type="text">
                                <td>
                                    <p:outputLabel  value="Country :" for="country"/>
                                </td>
                                <p:spacer height="5px" />
                                <td>
                                    <p:selectOneMenu id="country" style="width: 292px" value="#{customerMB.address.country}" required="true" requiredMessage="The Status field is required." >
                                        <f:selectItem itemLabel="Select Country"  itemValue="" />
                                        <f:selectItems value="#{customerMB.allCountries}" />
                                    </p:selectOneMenu>
                                    <p:watermark for="country" value="Country" /> 
                                    <p:message for="country"/>
                                </td>
                            </tr>
                            <p:spacer height="5px" />                         
                            <p:spacer height="5px" />
                        </tbody>
                    </table>
                    <div type="submit" align="right" style="margin-top: 5px">

                        <p:commandButton style="margin-right: 20px" value="Save" ajax="false" icon="ui-icon-circle-check" styleClass="ui-priority-primary" action="#{customerMB.addCustomer()}" />                                                    
                        <p:commandButton  value="Cancel" icon="ui-icon-arrowrefresh-1-n" onclick="location.href = 'login.jsp';"  process="@this"  >  
                            <p:resetInput target="customerPannel" />  
                        </p:commandButton> 

                    </div>  
                </fieldset>    

            </h:form>
这是我的帐户ManagedBean

public class AccountBusiness implements AccountBusinessLocal {

    @Inject
    private AccountManagerLocal accountManagerLocal;

    @Override
    public Account addAccount(String login, String password, Customer customer) {
        Account account;
        account = accountManagerLocal.createAccount(login, password);
        account.setCustomer(customer);
        accountManagerLocal.persistAccount(account);
        return account;
    }

    @Override
    public boolean authentificateUser(String username, String password) {
        Account account = accountManagerLocal.findByLogin(username);
        if (account != null) {
            if (password.equalsIgnoreCase(account.getPassword())) {
                return true;
            }
        }
        return false;
    }
}
我想在帐户登录输入文本中添加一个控件,用于检查用户名是否已被其他用户使用。如果是,用户应收到一条消息并重新输入可用的用户名。
我怎样才能做到这一点呢?

一种方法是提交关于模糊的输入并检查是否重复。如果它被复制,您将需要为该组件注册一条特定的消息,在这种情况下,我将绑定输入,以便能够从我的bean访问组件的id

下面是一个示例(经过测试)代码,其中包含您需要的相关元素:

查看

<h:form id="mainForm">
    <h:panelGrid id="grid" columns="3" cellpadding="4">  
        <p:outputLabel for="pseudo" value="Login" />
        <p:inputText id="pseudo" value="#{viewMBean.login}" binding="#{viewMBean.loginInput}" title="Pseudo" required="true" requiredMessage="The Pseudo field is required."
                     placeholder="Login">
            <p:ajax event="blur" listener="#{viewMBean.checkDuplicateLogin}" update="pseudoMsg" />
        </p:inputText>
        <p:message id="pseudoMsg" for="pseudo" />
    </h:panelGrid>
    <p:commandButton value="Register" />
</h:form>

对addCustomer()方法进行如下更改。我已经添加了一些评论供您了解

public String addCustomer() throws IOException {
        //you don't need password to check though
        boolean isExist = accountBusinessLocal.authentificateUser(login,password);
        if(isExist){
        //return empty
        FacesContext context = FacesContext.getCurrentInstance(
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Duplicated Login", null));
        return "";          
        }

        logger.log(Level.SEVERE, "*****add customer***** ");
        creation = new Date();
        right = Right.SimpleUser;
        Customer customerRegister = customerLocal.addUser(name, familyName, address, email, right, creation, organisation);
        accountBusinessLocal.addAccount(login, password, customerRegister);
        return "login.jsp";
    }
更新: 为了显示消息,在表单中添加p:message标记

<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />  


在您的帮助下,我无法再插入重复的登录名。但如何在注册表中向用户显示特定消息??“此登录名已保留给其他用户”我们已在打印消息,请参阅“重复登录名”。根据需要进行更改。请参阅我的更新答案以添加消息标记。创建消息时,您需要指定客户端id。@DanielCamargo不一定。如果您不知道,请将其保留为null。假设他仍希望在
public String addCustomer() throws IOException {
        //you don't need password to check though
        boolean isExist = accountBusinessLocal.authentificateUser(login,password);
        if(isExist){
        //return empty
        FacesContext context = FacesContext.getCurrentInstance(
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Duplicated Login", null));
        return "";          
        }

        logger.log(Level.SEVERE, "*****add customer***** ");
        creation = new Date();
        right = Right.SimpleUser;
        Customer customerRegister = customerLocal.addUser(name, familyName, address, email, right, creation, organisation);
        accountBusinessLocal.addAccount(login, password, customerRegister);
        return "login.jsp";
    }
<p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />