JSF 2.0 ViewScoped 生命周期

标签 jsf jsf-2

我的问题是我的 ViewScoped 之一bean 在同一 View 中创建了多次。 ViewScopedBean的构造函数每次我在树中选择一个节点时都会创建。

<h:form>
    <p:tree value="#{treeBean.root}" var="node"  
        selectionMode="single" selection="#{viewScopedBean.selectedNode}">  
        <p:ajax event="select" update="selectedNode, treeBeanUpdate, otherBeanUpdate, panel" listener="#{treeBean.onNodeSelect}" /> 
        <p:treeNode>  
            <h:outputText value="#{node}" />  
        </p:treeNode>  
    </p:tree>  
    Selected Node: <h:outputText value="#{viewScopedBean.selectedNode}" id="selectedNode"/><br/>
    Current TreeBean: <h:outputText value="#{treeBean}" id="treeBeanUpdate"/><br/>
    Current OtherBean: <h:outputText value="#{viewScopedBean}" id="otherBeanUpdate"/><br/>
    <p:outputPanel id="panel">
        <ag:profileComponent managedBean="#{viewScopedBean.profileBean}"/>
    </p:outputPanel>
</h:form>

如果我删除这部分(引用复合组件), ViewScopedBean 的构造函数没有被调用:

    <p:outputPanel id="panel">
        <ag:profileComponent managedBean="#{viewScopedBean.profileBean}"/>
    </p:outputPanel>

所有使用的bean都设置为@ViewScoped .

@ManagedBean
@ViewScoped
public class ViewScopedBean implements Serializable {

    private TreeNode selectedNode;
    private ProfileBean profileBean;

    public ViewScopedBean() {
        System.out.println("Constructor of ViewScopedBean " + this);
    }

    @PostConstruct
    public void init() {
        System.out.println("ViewScoped init" + this);
        profileBean = new ProfileBean();
    }
}

这是正确的行为吗?如果不是的话是什么原因造成的?

更新:我尝试使用空复合 Material ,但遇到了同样的问题。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"      
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:composite="http://java.sun.com/jsf/composite">

      <composite:interface>
            <composite:attribute name="managedBean" required="true"/>
      </composite:interface>

      <composite:implementation>
      </composite:implementation>

</html>

但是如果我做了managedBean不需要,没关系。

我不明白的另一件事是,当调用构造函数时,似乎没有使用创建的对象。

启动 View (控制台输出):

Constructor of ViewScopedBean xxx.bean.ViewScopedBean@4e1d2b8e

在树上点击 2 次:

Constructor of ViewScopedBean xxx.bean.ViewScopedBean@4eb64f2e
Constructor of ViewScopedBean xxx.bean.ViewScopedBean@66863941

然后我打开调试窗口<ui:debug/>viewScopedBean设置为xxx.bean.ViewScopedBean@4e1d2b8e

最佳答案

当您使用像 <c:if> 这样的 JSTL 标签时, View 作用域 bean 将在来自/到同一 View 的每个请求上重新创建。 , <c:forEach>在 View 中等等,或者当您使用 binding 将 JSF 组件绑定(bind)为 View 作用域 bean 的属性时属性。这显然就是复合组件中发生的情况。

您需要重写复合组件,使其不使用任何 JSTL 标记。也可以通过多种方式避免将某些 JSF 组件绑定(bind)为 bean 的属性,但如果这确实无法避免,则在 web.xml 中禁用部分状态保存。在大多数情况下应该有效:

<context-param>
    <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
    <param-value>false</param-value>
</context-param>

如果这对您不起作用,那么您确实必须与我们分享您的复合组件实现代码,以便我们可以指出注意事项并提出正确的方法。

关于JSF 2.0 ViewScoped 生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6914662/

相关文章:

java - 无法从上下文中将请求字符编码设置为 UTF-8

jsf - 在 Servlet 2.4 容器上运行 JSF 2.0

jsf - 条件渲染 f :selectItem possible problems

java - 向复合组件添加操作方法

JSF Flash Scope 记住太多消息

ajax - 在 Spring Security 中重定向处理 session 超时的 PrimeFaces Ajax 请求

jsf - PrimeFaces.current().focus 不适用于 commandButton

java - <rich :calendar> doesn't accept java. util.Calendar

java - 让 JSF 2.0 在分配给参数时执行方法,而不是存储对方法的引用?

java - 如何关联两个 selectManyListbox?