Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在PrimeFaces表单中进行验证后,JSF转换器不工作_Java_Jsf_Primefaces_Converter - Fatal编程技术网

Java 在PrimeFaces表单中进行验证后,JSF转换器不工作

Java 在PrimeFaces表单中进行验证后,JSF转换器不工作,java,jsf,primefaces,converter,Java,Jsf,Primefaces,Converter,我正在使用JSF2.1和PrimeFaces3.2服务器Tomcat7开发一个应用程序。现在,我正在制作一个表格来注册新用户。问题出在变频器上 我使用了几个标准字段,其中两个是密码。我有自定义的密码数据类型,所以我想使用转换器将字段中的字符串数据转换为bean中的密码变量。Primefaces表单在提交后使用AJAX,这可能存在问题。如果我完整地填写表单,没有验证错误,那么一切都正常。但如果存在Validation错误且没有转换器错误(我检查转换器中的密码长度),整个表单将停止工作。我必须刷新页

我正在使用JSF2.1和PrimeFaces3.2服务器Tomcat7开发一个应用程序。现在,我正在制作一个表格来注册新用户。问题出在变频器上

我使用了几个标准字段,其中两个是密码。我有自定义的密码数据类型,所以我想使用转换器将字段中的字符串数据转换为bean中的密码变量。Primefaces表单在提交后使用AJAX,这可能存在问题。如果我完整地填写表单,没有验证错误,那么一切都正常。但如果存在Validation错误且没有转换器错误(我检查转换器中的密码长度),整个表单将停止工作。我必须刷新页面才能使其再次工作

以下是一些资料来源:

密码类别:

public class Password {

    public static final short MIN_LENGTH = 5;

    private String text;
    private String hash;

    public Password(String text) {
        this.text = text;
        this.hash = Hasher.sha512(text);
    }

    /**
     * Get password instance with known hash only
     * @param hash SHA-512 hash
     * @return Password instance
     */
    public static Password getFromHash(String hash) {
        Password password = new Password(null);
        password.hash = hash;
        return password;
    }



    @Override
    public int hashCode() {
        return hash.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Password other = (Password) obj;
        if ((this.hash == null) ? (other.hash != null) : !this.hash.equals(other.hash)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return hash;
    }

    /**
     * @return the text
     */
    public String getText() {
        return text;
    }
}
密码转换器:

@FacesConverter(forClass = Password.class)
public class PasswordConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        String text = (String) value;

        if (text.length() >= Password.MIN_LENGTH) {
            return new Password(text);
        }

        FacesMessage msg = new FacesMessage(Texter.get("forms/forms", "shortPassword").replace("%limit%", String.valueOf(Password.MIN_LENGTH)));
        msg.setSeverity(FacesMessage.SEVERITY_ERROR);
        throw new ConverterException(msg);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        try {
            Password password = (Password) value;
            return password.getText();
        } catch (Exception ex) {
            throw new ConverterException(ex);
        }
    }
}
facelet中的表单:

<h:form id="registration">
    <p:panelGrid columns="3">

        <h:outputLabel value="#{commonTxt.email}:" for="email" />
        <p:inputText id="email" value="#{userRegistrationForm.email}" required="true" requiredMessage="#{formsTxt.msgEmpty}">
            <f:validator validatorId="email" />

            <f:validator validatorId="unique" />
            <f:attribute name="entity" value="SystemUser" />
            <f:attribute name="field" value="email" />
            <f:attribute name="uniqueMessage" value="#{formsTxt.nonUniqueEmail}" />
        </p:inputText>
        <p:message for="email" />

        <h:outputLabel value="#{usersTxt.password}:" for="password" />
        <p:password id="password" value="#{userRegistrationForm.password}" binding="#{password}" autocomplete="off" feedback="true" weakLabel="#{formsTxt.passwordWeak}" goodLabel="#{formsTxt.passwordGood}" strongLabel="#{formsTxt.passwordStrong}" promptLabel="#{formsTxt.passwordPrompt}" />
        <p:message for="password" />

        <h:outputLabel value="#{usersTxt.passwordCheck}:" for="passwordCheck" />
        <p:password id="passwordCheck" value="#{userRegistrationForm.passwordCheck}" binding="#{passwordCheckInput}" autocomplete="off">
            <f:validator validatorId="match" />
            <f:attribute name="matchAgainst" value="#{password}" />
            <f:attribute name="matchMessage" value="#{formsTxt.passwordMismatch}" />
        </p:password>
        <p:message for="passwordCheck" />

        <p:column /><p:column /><p:column />

        <h:outputLabel value="#{usersTxt.name}:" for="name" />
        <p:inputText id="name" value="#{userRegistrationForm.name}" maxlength="255" required="true" requiredMessage="#{formsTxt.msgEmpty}" />
        <p:message for="name" />


        <f:facet name="footer">
            <p:commandButton value="#{usersTxt.register}" action="#{userRegistrationForm.register()}" update="registration" />
        </f:facet>
    </p:panelGrid>
</h:form>

我不会发布bean的代码
#{userRegistrationForm}
,这里有两个带有getter和setter的密码属性


任何能帮助我解决问题的人都将不胜感激。提前谢谢。

解决了!我刚才使用了
FacesContext.getCurrentInstance().isValidationFailed()
来查看验证是否失败。在失败的情况下,转换器现在返回null(转换将不会完成),在其他情况下,转换器将返回正确的对象。表单可以很好地进行转换。

解决了!我刚才使用了
FacesContext.getCurrentInstance().isValidationFailed()
来查看验证是否失败。在失败的情况下,转换器现在返回null(转换将不会完成),在其他情况下,转换器将返回正确的对象。表单可以很好地进行转换