JSF生命周期过程验证阶段

时间:2015-08-08 08:32:45

标签: validation jsf jsf-2 lifecycle

我编写了一个带有两个验证器的简单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"));
    }
}

我认为如果我们从renderResponse方法调用validate方法,JSF实现应该跳到Render响应阶段。但实际上我有以下控制台输出:

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> 

1 个答案:

答案 0 :(得分:2)

这是指定的行为。来自FacesContext#renderResponse() javadoc

  

表示JavaServer面临的实现,即作为请求处理生命周期的当前阶段已完成,控制应该传递到渲染响应阶段,绕过任何尚未执行的阶段爱好。

因此,它并没有像您预期的那样突然中止当前阶段。它将完成当前阶段,然后进入渲染响应阶段。

仅在默认情况下,当您从验证程序中抛出ValidatorException时,它会执行此操作。所以,两者都做是不必要的。