Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 使用<;用户界面:包括>;是否只初始化一次bean_Jsf_Primefaces - Fatal编程技术网

Jsf 使用<;用户界面:包括>;是否只初始化一次bean

Jsf 使用<;用户界面:包括>;是否只初始化一次bean,jsf,primefaces,Jsf,Primefaces,我正在使用ajax和primefaces布局动态加载页面。这是我的页面 index.xhtml <p:layout fullPage="true"> <p:layoutUnit position="north"> <h1>Header</h1> </p:layoutUnit> <p:layoutUnit position="s

我正在使用ajax和primefaces布局动态加载页面。这是我的页面

index.xhtml

<p:layout fullPage="true">
            <p:layoutUnit position="north">
                <h1>Header</h1>
            </p:layoutUnit>

            <p:layoutUnit position="south">
                <h1>Footer</h1>
            </p:layoutUnit>

            <p:layoutUnit position="west">
                <h:form>

                    <p:menu>
                        <p:menuitem value="Page1" action="#{navigationBean.method('page1')}"
                                    ajax="true" update=":main-content"/>
                        <p:menuitem value="Page2" action="#{navigationBean.method('page2')}"
                                    ajax="true" update=":main-content"/>
                    </p:menu>
                </h:form>
            </p:layoutUnit>

            <p:layoutUnit position="center" >

                <h:panelGroup id="main-content">
                    <ui:include src="#{navigationBean.page}.xhtml" />
                </h:panelGroup>

            </p:layoutUnit>
        </p:layout>
page1.xhtml

<h:outputText value="#{page1Bean.text}"/>
Page2Bean.java与Page1Bean.java类似。 代码运行良好,使用ajax动态加载页面。 但是问题是@PostConstruct,Page1Bean和Page2Bean的构造函数只调用一次。 我的意思是,这些方法是在第一次加载页面时调用的。 但是当第二次加载页面时,不会再次调用它们。 因此,如果有人以前做过或见过它,请帮助我如何处理这种情况

编辑:我根据Luiggi Mendoza的想法在Page1Bean和Page2Bean上尝试了@Viewscope,但仍然没有成功。
它对@REQUESTSCOPE有好处(感谢Luiggi Mendoza),但是如果将来我得到了一些@SESSIONSCOPE bean会怎么样?

将范围从
@SessionScoped
更改为更窄的范围,比如
@ViewScoped
@RequestScoped
。在这种情况下,
@ViewScoped
似乎符合您的目的


BalusC在他的博客文章中解释了每个JSF托管bean作用域的详细信息。

如果你想在每次加载页面时实例化一个bean,那么这个bean应该是一个请求作用域。
Viewscope
对于这个特定视图就像一个
sessionscoped
bean。因此,如果每次加载页面时都需要新实例,我想请求范围是唯一的选项。@SrinivasR
@RequestScoped
将在同一页面上的每个请求(包括ajax请求)上创建。似乎
@ViewScoped
可以解决这个问题,因为每次访问页面时都会创建bean:第一次访问、第二次访问、从其他选项卡访问、刷新页面等等。我不明白你问题的最后一部分:如果将来我得到一些@SESSIONSCOPE bean怎么办。您可以将
@SessionScoped
托管bean注入到
@RequestScoped
中,如代码所示,这样就不会有任何问题。另外,请尝试使用一个更详细的示例,而不仅仅是从一个页面导航到另一个页面,以真正了解
@RequestScoped
@ViewScoped
之间的区别。
<h:outputText value="#{page1Bean.text}"/>
<h:outputText value="#{page2Bean.text}"/>
@ManagedBean
@SessionScoped
public class Page1Bean {

    private String text;

    @PostConstruct
    public void init()
    {
        System.out.println("\nPage1Bean @PostConstruct");;
    }

    public Page1Bean() {
        text = "Page 1 BEan Text Variable";
        System.out.println("Page1Bean Constructor");
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

}