2011-10-27 26 views
0

我已經離開了包含不同頁面鏈接的面板。根據左側面板中單擊的鏈接,右側面板是應該使用新頁面進行更新的面板。我希望它是一個Ajax更新,以便左側面板,頁眉和頁腳不刷新。我嘗試過使用ui:include,但只在第二次點擊時刷新頁面。該線程的引用如何在JSF2和Primefaces中實現局部導航2

ui:include in richfaces 4 only gets updated on second click

有沒有替代的方式來實現JSF2和Primefaces 2.2.1相同。 由於

更新

<ui:define name="content"> 
    <h:form id="form_01"> 

    <div style="margin:0; padding:0; float:right; width:740px;background:#FFF; "> 
     <p:outputPanel id="pagePanel"> 
      <ui:include src="#{panelMenu.currentPage}.xhtml"></ui:include> 
     </p:outputPanel> 
    </div> 

    <div style="padding:0; float:left;"> 
     <p:panel style="width:205px;" header="User Menu"> 
      <h:panelGrid columns="1"> 
       <p:commandLink update="pagePanel" action="#{panelMenu.setCurrentPage('/pages/group_message')}"> 
        <h:outputText value="Compose"/> 
       </p:commandLink> 
       <p:separator/> 
       <p:commandLink update="pagePanel" action="#{panelMenu.setCurrentPage('/pages/group_detail')}"> 
        <h:outputText value="Groups"/> 
       </p:commandLink> 
       <p:separator/> 

       </h:panelGrid> 
     </p:panel> 
    </div> 
    </h:form> 
</ui:define> 

輔助Bean

@ManagedBean(name="panelMenu") 
@SessionScoped 

public class PanelMenu { 
private String currentPage = "/pages/group_detail"; 

public String getCurrentPage() { 
    System.out.println(Thread.currentThread().getStackTrace()[1]); 
    System.out.println(FacesContext.getCurrentInstance().getCurrentPhaseId()); 
    return currentPage; 
} 

public void setCurrentPage(String currentPage) { 
    System.out.println(Thread.currentThread().getStackTrace()[1]); 
    System.out.println(FacesContext.getCurrentInstance().getCurrentPhaseId()); 
    this.currentPage = currentPage; 
} 

}

+0

是的,你可以實現這一目標使用JSF2和Primefaces2.2.1。 –

+0

你可以舉一些如何做的例子。 – WuR

回答

1

下面是一個例子:

託管豆:

@ManagedBean 
@ViewScoped 
public class PartialNavBean { 
    private String pageName = "/pages/group_member"; 

    public String getPageName() { 
     return pageName; 
    } 

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

查看:

<p:panel id="leftPanel"> 
    <h:form> 
     <p:commandLink value="Group member" 
         action="#{partialNavBean.setPageName('/pages/group_member')}" 
         update="rightPanel"/> 
     <br/> 
     <p:commandLink value="Group detail" 
         action="#{partialNavBean.setPageName('/pages/group_detail')}" 
         update="rightPanel"/> 
    </h:form> 
</p:panel> 

<p:panel id="rightPanel"> 
    <ui:include src="#{partialNavBean.pageName}.xhtml"/> 
</p:panel> 

更新:

我試過如下:

public String getPageName() { 
    System.out.println(Thread.currentThread().getStackTrace()[1]); 
    System.out.println(FacesContext.getCurrentInstance().getCurrentPhaseId()); 
    return pageName; 
} 

public void setPageName(String pageName) { 
    System.out.println(Thread.currentThread().getStackTrace()[1]); 
    System.out.println(FacesContext.getCurrentInstance().getCurrentPhaseId()); 
    this.pageName = pageName; 
} 

和輸出如下:

 
INFO: pkg.PartialNavBean.getPageName(PartialNavBean.java:14) 
INFO: RESTORE_VIEW 1 
INFO: pkg.PartialNavBean.setPageName(PartialNavBean.java:20) 
INFO: INVOKE_APPLICATION 5 
INFO: pkg.PartialNavBean.getPageName(PartialNavBean.java:14) 
INFO: RENDER_RESPONSE 6 
INFO: pkg.PartialNavBean.getPageName(PartialNavBean.java:14) 
INFO: RESTORE_VIEW 1 
INFO: pkg.PartialNavBean.setPageName(PartialNavBean.java:20) 
INFO: INVOKE_APPLICATION 5 
INFO: pkg.PartialNavBean.getPageName(PartialNavBean.java:14) 
INFO: RENDER_RESPONSE 6 
+0

感謝Bhesh的例子,但它有與我在鏈接中指出的相同的問題。該頁面僅在第二次點擊後刷新。如果你跟蹤你的應用程序,你會注意到,單擊鏈接後,getPageName會在setPageName之前被調用。 – WuR

+0

@ WuR:我已經嘗試過了,它的工作原理與它應有的一樣。在Render Response階段調用'setPageName',在Render Response階段調用'getPageName'。我不是爲什麼它不適合你的情況。嘗試爲這兩個鏈接添加'immediate =「true」'。 –

+0

@ WuR:查看更新。你可以試試看看它爲什麼會起作用。 –