单击菜单到Ajax </p:layoutunit>的链接,动态更改<p:layoutunit>的内容

时间:2012-04-26 15:54:49

标签: jsf-2 primefaces

我正在使用在Tomcat 7上运行的Primefaces的JSF2。我在baseLayout.xhtml中创建了一个布局,如下所示: -

<h:body>
    <p:layout fullPage="true">
        <p:layoutUnit position="north" size="50" id="top">
            <h:form>
                <ui:include src="/template/header.xhtml" />
            </h:form>
        </p:layoutUnit>
        <p:layoutUnit position="south" size="20">
            <h:form>
                <ui:include src="/template/footer.xhtml" />
            </h:form>
        </p:layoutUnit>
        <p:layoutUnit position="west" size="400">
            <h:form>
                <ui:include src="/template/menu.xhtml" />
            </h:form>
        </p:layoutUnit>
        <p:layoutUnit position="center" size="400">
            <h:panelGroup id="centerContentPanel">
                <ui:include src="#{navigationBean.pageName}.xhtml" />
            </h:panelGroup>
        </p:layoutUnit>
    </p:layout>
</h:body>

我想动态更改 centerContentPanel 的来源,而不刷新整个页面,只需 centerContentPanel ,即点击menu.xhtml中的链接,如下所示: -

<h:form id="form2">
       <f:ajax render=":centerContentPanel" execute="@this">
           <h:commandLink value="page1" action="#{navigationBean.doNav}" >
             <f:param name="test" value="/pages/page1" />
           </h:commandLink>
           <h:commandLink value="page2" action="#{navigationBean.doNav}" >
             <f:param name="test" value="/pages/page2" />
           </h:commandLink>
    </f:ajax>
</h:form>

。我尝试这样做,但它改为刷新整个页面而不更改URL,当我再次刷新它时,包含新页面。我不知道发生了什么。我的NavigationBean如下: -

public class NavigationBean {

    private String pageName="/template/body";

    public NavigationBean() {
    }

    public String doNav() {
        System.out.println("Hello");
        String str = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("test");
        this.pageName = str;
        return pageName;
    }

    public String getPageName() {
        return pageName;
    }

    public void setPageName(String pageName) {
        this.pageName = pageName;
    }
}

1 个答案:

答案 0 :(得分:4)

doNav()更改为void,无需返回值...(因为它会导致commandLink重新加载页面...)您已经更新了pageName中正在使用的<ui:include src="#{navigationBean.pageName}.xhtml" /> 1}}

doNav应如下所示:

public void doNav() {
    System.out.println("Hello");
    String str = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("test");
    this.pageName = str;

}

action="..."的返回值会导致refreshes the whole page without changing the URL