【问题标题】:JSF 2.0 - 2nd validator called even though 1st validator failsJSF 2.0 - 即使第一个验证器失败,也会调用第二个验证器
【发布时间】:2013-04-17 08:07:50
【问题描述】:

我正在使用 WAS 8.0.0.5 的 MyFaces 2.0.4 AND CDI(无法在不丢失 CDI 的情况下替换 WAS 8 的 JSF 2.0 版本)。

我有 2 个自定义验证器注册到一个组件。第一个进行多场比较。第二个验证器使用 CDI 注入 SSB,SSB 使用实体管理器调用数据库。

如果我输入的信息导致第一个验证器故意失败,第二个验证器仍会执行。为什么?如果第一次验证失败,如何避免第二次验证?我认为如果一个验证器失败,那么所有后续验证都会被绕过。

<h:form id="registrationForm">
    <fieldset>
        <p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />
        <legend>Register</legend>
        <div class="form-row">
            <h:outputLabel for="userId" value="*User Id"/>
            <h:inputText id="userId" value="#{registration.userId}" required="true" size="20">
                <f:validator validatorId="userIdPasswordValidator" />
                <f:attribute name="passwordComponent" value="#{passwordComponent}"/>
                <f:validator binding="#{duplicateUserValidator}" /> <-- uses CDI
            </h:inputText>
        </div>
        <div class="form-row">
            <h:outputLabel for="password" value="*Password"/>
            <h:inputSecret id="password" type="password" binding="#{passwordComponent}" value="#{registration.password}" required="true">
            </h:inputSecret>
        </div>
        <div class="form-row">
            <h:outputLabel for="confirmPassword" value="*Confirm Password"/>
            <h:inputSecret id="confirmPassword" type="password" value="#{registration.confirmPassword}" required="true" />
        </div>
        <div class="form-row">
            <h:commandButton styleClass="btn btn-warning" value="Register" type="submit" action="#{registration.register}" />
        </div>
    </fieldset>
</h:form>

第一个验证者:

@FacesValidator("userIdPasswordValidator")
public class UserIdPasswordValidator implements Validator {

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

        if (userId.equals(password)) {
            FacesMessage message = new FacesMessage(null, "The Password cannot be the same as your User ID.");
            throw new ValidatorException(message);
        }
    }
}

第二个验证器:

@Named
@RequestScoped
public class DuplicateUserValidator implements Validator {

@Inject
UserService userService;

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {

    if (userService.getUser(value.toString()) != null) {
        FacesMessage message = new FacesMessage(null, "This User ID is already registered. Please logon or choose another one to register.");
        throw new ValidatorException(message);
    }
}

}

【问题讨论】:

    标签: java jsf jsf-2 cdi myfaces


    【解决方案1】:

    要达到预期的结果,您应该致电 renderResponse(),如以下 oracle 摘录所示:

    如果在当前 FacesContext 上调用了任何验证方法或事件侦听器 renderResponse,JavaServer Faces 实现会跳到渲染响应阶段。


    但恐怕这就是验证阶段的工作方式。 From oracle:

    在此阶段,JavaServer Faces 实现处理所有在树中的组件上注册的验证器。它检查指定验证规则的组件属性,并将这些规则与为组件存储的本地值进行比较。

    如果本地值无效,JavaServer Faces 实现将错误消息添加到 FacesContext 实例

    这里的关键短语是处理所有验证器向 FacesContext 实例添加错误消息。虽然这里不是很清楚,但这意味着整个验证阶段将完成并且消息排队。完成后,进入相应的阶段。

    【讨论】:

    • 谢谢。看起来我会将这 2 个验证器合并为 1 个自定义验证器或两个验证器的包装器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 2011-02-11
    相关资源
    最近更新 更多