给定以下简单的测试用例。
<h:form id="form" prependId="true">
<p:commandButton value="Submit" actionListener="#{testManagedBean.action}"/>
<p:commandLink value="Submit" actionListener="#{testManagedBean.action}"/>
</h:form>
涉及的 JSF 托管 bean:
@ManagedBean
@ViewScoped
public final class TestManagedBean implements Serializable
{
private static final long serialVersionUID = 1L;
@PostConstruct
private void init() {
System.out.println("init() called");
// May invoke expensive business services like EJB calls.
}
public void action() {
System.out.println("action() called.");
}
}
当按下<p:commandButton>
或给定时,除了动作侦听器之外,还会调用该方法 - 。<p:commandLink>
init()
action()
这不应该发生,因为该init()
方法可能具有昂贵的业务服务,不应在每个 AJAX 请求上不必要地调用这些服务。
有没有办法防止这种行为?init()
不应在 AJAX 调用上调用该方法。
我正在使用 JSF 2.2.6 和 PrimeFaces 5.0 final。