gpt4 book ai didi

java - JSF 2.0 - 即使第一个 validator 失败也会调用第二个 validator

转载 作者:行者123 更新时间:2023-12-01 23:52:05 24 4
gpt4 key购买 nike

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

我有 2 个注册到组件的自定义 validator 。第一个进行多字段比较。第二个 validator 使用 CDI 注入(inject) SSB,后者使用实体管理器调用数据库。

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

<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);
}
}
}

第二个 validator :

@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);
}
}

}

最佳答案

要实现所需的结果,您应该调用 renderResponse(),如以下 Oracle 摘录所示:

If any validate methods or event listeners called renderResponse on the current FacesContext, the JavaServer Faces implementation skips to the render response phase.

<小时/>

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

During this phase, the JavaServer Faces implementation processes all validators registered on the components in the tree. It examines the component attributes that specify the rules for the validation and compares these rules to the local value stored for the component.

If the local value is invalid, the JavaServer Faces implementation adds an error message to the FacesContext instance

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

关于java - JSF 2.0 - 即使第一个 validator 失败也会调用第二个 validator ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16180718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com