2013-04-10 26 views
0

我正在使用PrimeFaces 3.5並面臨部分頁面呈現問題。我需要將來自Facelet文件的內容加載到模板的「動態」部分。部分頁面呈現(PPR)與臉部重定向

index.xhtml

<f:view> 
    <h:form id="form1"> 
     <p:outputPanel layout="block"> 
      <p:commandButton action="content-2?faces-redirect=false" update="display" /> 
     </p:outputPanel> 
     <p:outputPanel id="display" layout="block"> 
      content 1 
     </p:outputPanel> 
    </h:form> 
</f:view> 

content-2.xhtml

<h:body> 
    content 2 loaded 
</h:body> 

當我點擊<p:commandButton>,然後content-2.xhtml將被打開。但是,這會刷新整個頁面。 XML響應包含這樣的:

<partial-response><changes><update id="javax.faces.ViewRoot"> 

當我改變action屬性的方法表達:

<f:view> 
    <h:form id="form1"> 
     <p:outputPanel layout="block"> 
      <p:commandButton action="#{myBean.increment}" update="display" /> 
     </p:outputPanel> 
     <p:outputPanel id="display" layout="block"> 
      #{myBean.count} 
     </p:outputPanel> 
    </h:form> 
</f:view> 

然後display塊更新預期。 XML響應包含這樣的:

<partial-response><changes><update id="form:display"> 

爲什麼action="content-2?faces-redirect=false"方式更新整個頁面?

我也試過<ui:composition>,但在這種情況下,這會重新加載模板的「靜態」部分。我不想要這個。

+0

你是否暗示'#{myBean.method}'返回'content-2.xhtml'? 'faces-redirect = false'已經是默認了,我不明白你爲什麼明確提到這個。你是否暗示'action =「content-2.xhtml」'的行爲有所不同? – BalusC 2013-04-10 19:56:26

+0

不,對不起,我更新了#{myBean.method}的部分。方法體看起來像'count ++'。我嘗試'action =「content-2.xhtml」'它也刷新'javax.faces.ViewRoot',但是'action =「#{myBean.increment}''refresh'display'。我認爲'faces-redirect = false'必須賦值爲'true'或'false'。 – Mrusful 2013-04-11 17:28:27

回答

0

這是完全正常的,預期的行爲,如果你action屬性提供非null結果導航到了不同的看法。然後,JSF將會使用@all的渲染替換部分渲染。 JSF/Facelets不會以某種方式自動保留樹中的通用組件,就像您期望的那樣。如果您打算動態變化部分頁面渲染過程中的組件樹,那麼你就需要內部<p:outputPanel id="display" layout="block">無論是動態包括像這樣(section只是一個整數)

<ui:include src="section#{bean.section}.xhtml" /> 

或多個有條件地呈現部分

<ui:fragment rendered="#{bean.section == 1}"><ui:include src="section1.xhtml" /></ui:fragment> 
<ui:fragment rendered="#{bean.section == 2}"><ui:include src="section2.xhtml" /></ui:fragment> 
<ui:fragment rendered="#{bean.section == 3}"><ui:include src="section3.xhtml" /></ui:fragment> 
<ui:fragment rendered="#{bean.section == 4}"><ui:include src="section4.xhtml" /></ui:fragment> 

根據包含文件是否包含表單/輸入/命令組件和/或引用視圖作用域bean,每種方法都有其優缺點。 <ui:include>即視圖編譯時間運行,如JSTL。另請參閱JSTL in JSF2 Facelets... makes sense?

faces-redirect=false的存在並不重要,因爲它是默認值。如果它是true,那麼將在目標URL上調用JavaScript window.location

+0

謝謝,現在想我明白了。我想我該怎麼做才能做到這一點,頁面上的所有塊可能會獨立於服務器的狀態進行更新。 – Mrusful 2013-04-11 18:11:47

相關問題