使用< ui:include>动态加载页面仅初始化一次bean [英] Dynamic Page loading using <ui:include> is initializing beans only once

查看:61
本文介绍了使用< ui:include>动态加载页面仅初始化一次bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm loading page dynamically using ajax and primefaces Layout. Here are my pages.

index.xhtml

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>

NavigationBean.java

NavigationBean.java

@ManagedBean
@SessionScoped
public class NavigationBean {

    @ManagedProperty(value="page1")
    private String page;

public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    public void method(String page){
        this.page = page;
    }
}

page1.xhtml

page1.xhtml

<h:outputText value="#{page1Bean.text}"/>

page2.xhtml

page2.xhtml

<h:outputText value="#{page2Bean.text}"/>

Page1Bean.java

Page1Bean.java

@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;
    }

}

Page2Bean.java与Page1Bean.java类似. 该代码可以正常工作,并且可以使用ajax动态加载页面. 但是问题是 @PostConstruct和Page1Bean和Page2Bean的构造方法仅被调用一次. 我的意思是第一次加载页面时会调用这些方法. 但是,当第二次加载页面时,不会再次调用它们. 因此,如果有人以前做过它或之前看过它,请帮助我处理这种情况.

Page2Bean.java is similar to Page1Bean.java. The code is working fine and loading pages dynamically and using ajax. But the problem is @PostConstruct and Constructors of Page1Bean and Page2Bean are called only once. I mean those methods are called when the page is loaded for first time. But they are not called again when the page is loaded second time. So if anyone had done it before or seen it before Please help me how to handle this situation.

我已经基于Luiggi Mendoza的想法在Page1Bean和Page2Bean上尝试使用@Viewscope,但还是没有运气. 它对对@REQUESTSCOPE 很好(感谢Luiggi Mendoza),但是如果将来我得到一些@SESSIONSCOPE豆怎么办?

EDIT : I've tried @Viewscope on Page1Bean and Page2Bean based on Luiggi Mendoza's idea,but Still no luck. It works GOOD for @REQUESTSCOPE (thanks for that Luiggi Mendoza), but what if in future i get some @SESSIONSCOPE beans??

推荐答案

将范围从@SessionScoped更改为更窄的范围,例如@ViewScoped@RequestScoped.在这种情况下,@ViewScoped似乎可以满足您的要求.

Change the scope from @SessionScoped to a narrower scope like @ViewScoped or @RequestScoped. For this case, it seems that @ViewScoped serves your purpose.

BalusC在其博客文章 JSF 2中的通信:托管Bean范围.

BalusC explains the details of every JSF managed bean scope in his blog article Communication in JSF 2: Managed Bean Scopes.

这篇关于使用&lt; ui:include&gt;动态加载页面仅初始化一次bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆