从ui:component内部更新primefaces面板

时间:2014-12-04 13:46:42

标签: ajax jsf jsf-2 primefaces

当我点击'转到第1页的内容'(page2.xhtml)时,centerPanel(index.xhtml)只是空白而我的doNav方法没有按如下方式调用:

的index.xhtml

<p:layout fullPage="true">

                <p:layoutUnit position="west" size="245">
                    <h:form>
                        <p:menu>
                            <p:submenu label="Resources">
                                <p:menuitem value="Page 1" actionListener="#{navBean.doNav}" update=":centerPanel">
                                    <f:param name="page" value="page1" />
                                </p:menuitem>
                                <p:menuitem value="Page 2" actionListener="#{navBean.doNav}" update=":centerPanel">
                                    <f:param name="page" value="page2" />
                                </p:menuitem>
                            </p:submenu>
                        </p:menu>
                    </h:form>
                </p:layoutUnit>


                <p:layoutUnit position="center">
                    <p:panel id="centerPanel">
                        <c:if test="#{navBean.page != null}">
                            <ui:include src="#{navBean.page}.xhtml" />
                        </c:if>
                    </p:panel>
                </p:layoutUnit>


            </p:layout>

page1.xhtml

    <ui:composition xmlns="http://www.w3.org/1999/xhtml"
                    xmlns:ui="http://xmlns.jcp.org/jsf/facelets">

        Page 1

    </ui:composition>

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns:p="http://primefaces.org/ui"
                xmlns:f="http://xmlns.jcp.org/jsf/core">

page2.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns:p="http://primefaces.org/ui"
                xmlns:f="http://xmlns.jcp.org/jsf/core">

    <h3>Page 2</h3>

    <p:commandButton value="Go to page 1 from included" actionListener="#{navBean.doNav}" update=":centerPanel">
        <f:param name="page" value="page1" />
    </p:commandButton>

</ui:composition>

NavBean

@ManagedBean(name = "navBean")
public class NavBean {

    private String page = null;

    public void doNav() {
        this.page = FacesContext.getCurrentInstance().getExternalContext()
                .getRequestParameterMap().get("page");
    }

    public String getPage() {
        return page;
    }

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

}

无论如何我能做到吗?

1 个答案:

答案 0 :(得分:1)

我看到的问题是你的NavBean是@NoneScoped,因为你没有指定范围。每次从表达式引用NavBean时,@NoneScoped都会导致创建一个NavBean,因此在使用它时,您的页面变量尚未设置。您需要为NavBean指定一个范围至少为@ViewScoped。阅读这些问题: