【发布时间】:2015-08-08 08:32:45
【问题描述】:
我编写了一个带有两个验证器的简单 JSF 应用程序来了解 JSF 流程验证阶段。以下是验证器:
@FacesValidator("second")
public class AnotherValidator implements Validator{
public void validate(FacesContext arg0, UIComponent arg1, Object arg2)
throws ValidatorException {
System.out.println("Validation phase, the second");
FacesContext.getCurrentInstance().renderResponse();
throw new ValidatorException(new FacesMessage("The second"));
}
}
和
@FacesValidator("first")
public class ProducerValidator implements Validator{
public void validate(FacesContext arg0, UIComponent arg1, Object arg2)
throws ValidatorException {
System.out.println("Validation phase, the first");
FacesContext.getCurrentInstance().renderResponse();
throw new ValidatorException(new FacesMessage("The first"));
}
}
我认为如果我们从validate 方法调用renderResponse 方法,JSF 实现应该跳到渲染响应阶段。但实际上我有以下控制台输出:
Validation phase, the first
Validation phase, the second
尽管从第一个验证器调用了renderResponse,第二个验证器还是被调用了……为什么? Facelets 标记:
<h:inputText value="#{helloBean.p}" converter="conv">
<f:validator validatorId="first"/>
<f:validator validatorId="second" />
</h:inputText>
【问题讨论】:
-
@Tiny 对我来说,通过
facesContext.addMessage(String, FacesMessage)发布特定组件的验证消息并使用<h:messages for="id" />似乎更清楚... -
@Tiny BTW,我试图避免在不指定组件 ID 或
globalOnly的情况下使用<h:messages />,这是真的吗?
标签: validation jsf jsf-2 lifecycle