验证失败后更新字段

时间:2016-08-21 15:54:57

标签: jsf primefaces client-side-validation

我正在使用帖子标题中提到的技术构建一个Web应用程序。我搜索过类似的帖子,但没有一个能解决我的问题。我已经配置了我的Maven项目,pom文件是正确的,JSF和PrimeFaces正在工作,但是当我执行以下操作时遇到问题:

我从Prime Faces Showcase网站复制了客户端验证示例,而在我看来,它没有按预期工作。

当字段验证失败时,字段右侧会出现错误,只要它在那里,我就无法更新字段。当我尝试输入某些内容时,屏幕上没有任何反应,但如果我突出显示该字段中的文字,我可以在那里看到它。

这是我的registration.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        Hello from Facelets

        <h2>Registration Form</h2>
        <h:form id="form">
            <p:panel id="panel" header="Form" style="margin-bottom:10px;">
                <p:fieldset legend="Registration Form" widgetVar="regWidget" style="width: 600px;">
                    <h:panelGrid columns="3" width="550" border="0">
                        <h:outputLabel value="UserName" />
                        <p:inputText value="#{regCont.prospect.userName}" 
                                     id="userName"
                                     required="true" 
                                     requiredMessage="UserName is required"
                                     validatorMessage="UserName should be of length from 5 to 15 chars"
                                     >
                            <f:validateLength minimum="5" maximum="15" for="userName"></f:validateLength>
                        </p:inputText>
                        <p:message for="userName"/>


                        <h:outputLabel value="Password" />
                        <p:password value="#{regCont.password}"
                                    id="password"
                                    required="true" 
                                    requiredMessage="Password is required"
                                    validatorMessage="Password should be of length from 5 to 15 chars"
                                    >
                            <f:validateRegex pattern="^(?=^.{8,12}$)(?!.*(.)\1{3,})(?=.*[A-Z])(?=.*[a-z]{3,})(?=.*[^a-zA-Z0-9]{2,})(?=.*[0-9]{2,})[a-zA-Z]"/>

                        </p:password>
                        <p:message for="password" />

                        <h:outputLabel value="FirstName" />
                        <p:inputText value="#{regCont.prospect.firstName}"
                                     id="firstName"
                                     required="true" 
                                     requiredMessage="FirstName is required"
                                     validatorMessage="FirstName should be of length from 5 to 15 chars"
                                     >
                            <f:validateLength minimum="1" maximum="45" for="firstName"></f:validateLength>
                        </p:inputText>
                        <p:message for="firstName" display="tooltip" />


                        <h:outputLabel value="LastName" />
                        <p:inputText value="#{regCont.prospect.lastName}"
                                     id="lastName"></p:inputText>
                        <p:message for="lastName" display="tooltip"/>

                        <h:outputLabel value="Email" />
                        <p:inputText value="#{regCont.prospect.email}"
                                     id="email"
                                     validatorMessage="Invalid Email">
                            <f:validateRegex pattern="[a-zA-Z0-9]+@[a-zA-Z]+.[a-zA-Z]{2,3}"></f:validateRegex> 
                        </p:inputText>
                        <p:message for="email" />

                        <h:outputLabel value="Nation" />
                        <p:inputText value="#{regCont.prospect.nation}"
                                     id="nation">
                        </p:inputText>
                        <p:message for="nation" />

                        <h3 style="margin-top:0">User type</h3>
                        <h:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5">
                            <p:outputLabel for="type" value="User type:" />
                            <p:selectOneRadio id="type" value="#{regCont.prospect.userType}">
                                <f:selectItem itemLabel="Delegate" itemValue="Delegate" />
                                <f:selectItem itemLabel="Leader" itemValue="Leader" />
                            </p:selectOneRadio>
                        </h:panelGrid>

                        <p:commandButton value="Register" update="panel" icon="ui-icon-check" validateClient="true" style="margin-right:10px"/>

                        <h:commandButton value="Reset p:ajax" style="margin-right:20px;" >
                            <p:ajax update="panel" resetValues="true" />
                        </h:commandButton>
                    </h:panelGrid>
                </p:fieldset>
            </p:panel>
        </h:form>


    </h:body>
</html>

这是我的RegistrationController类:

@ManagedBean(name = "regCont")
@RequestScoped
public class RegistrationController implements Controller {

    @ManagedProperty(value = "#{prospect}")
    private Prospect prospect;
    @ManagedProperty(value = "#{user}")
    private User user;
    private String msg;
    private String password;

    public void hashPassword() throws NoSuchAlgorithmException {
        byte[] salt = Passwords.getNextSalt();
        prospect.setSalt(salt);
        prospect.setPasswordHash(Passwords.getHashWithSalt(password, salt));
        password = "";
    }

    public Prospect getProspect() {
        return prospect;
    }

    public void setProspect(Prospect prospect) {
        this.prospect = prospect;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

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

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    private void clearAll() {
        user.setFirstName("");
        user.setLastName("");
        user.setNation("");
        user.setEmail("");
        user.setUserName("");
        user.setPasswordHash(null);

        prospect.setFirstName("");
        prospect.setLastName("");
        prospect.setNation("");
        prospect.setEmail("");
        prospect.setUserType("");
        prospect.setUserName("");
        prospect.setPasswordHash(null);
    }

    public void saveUser() {
        UserDAO dao = new UserDAO();
        dao.createEntity(user);
        this.msg = "Member Info Saved Successfull!";
        clearAll();
    }

    public void saveProspect() {
        ProspectDAO dao = new ProspectDAO();
        try {
            hashPassword();
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        dao.createEntity(prospect);
        this.msg = "Member Info Saved Successfull!";
    }

    public void reset() {
        RequestContext.getCurrentInstance().reset("form");
    }

    public void updateUser(int id) {
        UserDAO dao = new UserDAO();
        try {
            hashPassword();
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        dao.updateEntity(id);
        this.msg = "Member Info Update Successfull!";
        clearAll();
    }

    public void deleteUser(int id) {
        UserDAO dao = new UserDAO();
        dao.deleteEntity(id);
        this.msg = "Member Info Delete Successfull!";
        clearAll();
    }

}

只有这种情况有效,当我实现重置按钮时,验证失败时,我点击重置,然后我可以将值输入到字段中。

此外,当我将鼠标悬停在字段(p:inputText)上方时,光标不会像其他字段(例如h:inputText)那样更改为文本光标。真的不知道同一段代码如何在PrimeFaces ShowCase网站上运行,而不是在我的最终?

有人知道我该如何解决这个问题?

我正在使用JSF 2.2 + PrimeFaces 5.3

0 个答案:

没有答案