java - 由于 FacesValidator,ManageBean 方法不再有效?

标签 java validation jsf jsf-2

我在密码验证方面遇到了一些问题,@BalusC 帮我解决了,here . 但是在我的表单中插入验证代码之后,当我调用 Controller 方法时,没有任何反应。 这是我的 JSF 页面: 注册.xhtml

<!DOCTYPE html>
<html lang="pt-br"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich">

    <h:head>
        <title>TITLE</title>
    </h:head>

    <h:body>
         <h:form id="form">
            <h:panelGrid columns="3" >

                <h:outputLabel for="name"   value="Nome:" />
                <h:inputText   id="name"    value="#{register.person.name}"  >  
                    <f:ajax    event="blur" listener="#{register.validateName}" render="m_name" />
                </h:inputText>
                <rich:message  id="m_name"  for="name"  ajaxRendered="false"/>

                // some fields ..                    

                <h:outputLabel for="password" value="Password" />
                <h:inputSecret id="password" value="#{register.user.password}">
                    <f:validator validatorId="confirmPasswordValidator" />
                    <f:attribute name="confirm" value="#{confirmPassword.submittedValue}" />
                    <f:ajax event="blur" execute="password confirm" render="m_password" />
                </h:inputSecret>
                <rich:message id="m_password" for="password" />

                <h:outputLabel for="confirm" value="Senha (novamente):" />
                <h:inputSecret id="confirm" binding="#{confirmPassword}" >
                    <f:ajax event="blur" execute="password confirm" render="m_password m_confirm" />
                </h:inputSecret>
                <rich:message id="m_confirm" for="confirm" />

                <h:commandButton value="Register" action="#{register.registerPerson}" >
                    <f:ajax execute="@form" render="@form" />
                </h:commandButton>

                <h:outputText value="#{register.recordStatus}" id="out"  />
                <a4j:status>
                    <f:facet name="start">
                        <h:graphicImage name="loader.gif" library="image"/>
                    </f:facet>
                </a4j:status>    
        </h:panelGrid>
    </h:form>
    </h:body>
</html>

因此,当我填写表格并尝试注册时,没有任何反应,甚至没有任何异常启动。

密码 validator (感谢 BalusC 伙伴):

@FacesValidator("confirmPasswordValidator")
public class ConfirmPasswordValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        String password = (String) value;
        String confirm = (String) component.getAttributes().get("confirm");

        if (password == null || confirm == null) {
            return; // Just ignore and let required="true" do its job.
        }

        if (!password.equals(confirm)) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, null, "password not equal"));
        }else{
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, null, "ok"));
        }
    }

}

我觉得这个问题很奇怪,我真的不知道这里发生了什么。 谢谢大家。

最佳答案

您在成功匹配时抛出了 ValidatorException

if (!password.equals(confirm)) {
    throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, null, "password not equal"));
} else {
    throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_INFO, null, "ok"));
}

这会导致 JSF 阻止表单提交。严重程度无关紧要。 validator 异常是 validator 异常。表示验证失败。

宁可使用FacesContext#addMessage()相反。

if (!password.equals(confirm)) {
    throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, null, "password not equal"));
} else {
    context.addMessage(component.getClientId(), new FacesMessage(FacesMessage.SEVERITY_INFO, null, "ok"));
}

关于java - 由于 FacesValidator,ManageBean 方法不再有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7536328/

相关文章:

java - 如何在 IntelliJ 注册表中设置值?

java - 如何在 android 中使用 Room Persistence ORM 工具实现 created_at 和 updated_at 列

java - 为什么 "return"在 Kotlin 中可以返回 "return"?

java - IntelliJ 在整个文件中应用检查修复

javascript - 在字段有效之前,如何禁用 polymer 中的按钮?

ruby - 使用嵌入文档时的 ActiveRecord 嵌套验证 : Mongoid

javascript - 语义 ui 表单验证问题

java - JSF 数据表单元格 - 如果内容太长,则剪切文本并替换为 "..."

jsf - 当选择父列表中的标记项目时重置子 p :selectOneMenus inside a p:dataTable to empty,

validation - JSF不支持跨字段验证,有解决方法吗?