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

时间:2014-02-25 12:04:32

标签: validation java-ee primefaces user-input

我有以下注册表格:

 <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>

在我的ViewScoped Bean中保留用户帐户的方法是:

public String addCustomer() throws IOException {
        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";
    }

这是我的帐户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;
    }
}

我想在帐户登录输入文本中添加一个控件,用于检查用户名是否已被其他用户使用。如果是,则用户应该收到消息并重新输入可用的用户名。 我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

一种方法是通过提交关于模糊的输入并检查它是否重复。如果它是重复的,你需要注册一个特定于该组件的消息,在这种情况下,我将绑定输入以便能够从我的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>

<强>豆

import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

@ManagedBean
@ViewScoped
public class ViewMBean implements Serializable {

    private UIComponent loginInput;

    private String login;

    public void checkDuplicateLogin() {
        //replace with your own fancy code
        if (login.equals("admin")) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(loginInput.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR, "Duplicated Login", null));
        }
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public UIComponent getLoginInput() {
        return loginInput;
    }

    public void setLoginInput(UIComponent loginInput) {
        this.loginInput = loginInput;
    }

}

答案 1 :(得分:1)

对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" />