2013-03-04 53 views
4

如何在運行時有條件地包含jsf facelets文件?需要 樣品功能是有條件的jsf include

if (add button click) { 

ui:include src="Add.xhtml" 
} 

if (update button click) { 

ui:include src="Update.xhtml" 
} 

上述語法中僅指示...

鑽嘴魚科2.1.1 /的Apache Tomcat 7.0.22/PrimeFaces 3.4

回答

5

ui:include沒有rendered屬性,所以你必須將其封裝在其他組件中。你也可以在點擊按鈕的基礎上在服務器上設置一些屬性。

<h:form> 
    <p:commandButton value="Add" update=":includeContainer"> 
    <f:setPropertyActionListener value="add" target="#{myBean.action}"/> 
    </p:commandButton> 
    <p:commandButton value="Update" update=":includeContainer"> 
    <f:setPropertyActionListener value="update" target="#{myBean.action}"/> 
    </p:commandButton> 
</h:form> 

<h:panelGroup id="includeContainer"> 
    <h:panelGroup rendered="#{myBean.action == 'add'}"> 
    <ui:include src="add.xhtml"/> 
    </h:panelGroup> 
    <h:panelGroup rendered="#{myBean.action == 'update'}"> 
    <ui:include src="update.xhtml"/> 
    </h:panelGroup> 
</h:panelGroup> 

的後盾豆你將有getter和setter:

public void setAction(String action) { 
    this.action = action; 
} 

public String getAction() { 
    return action; 
} 
+0

a subst itute問題是,如果它的一個條件包括那麼爲什麼在調用主文件時處理xhtml ...我通過在add.xhtml文件中包含af:event prerenderview標記來檢查這個例子...我得到通過偵聽器方法打印的system.out ...是視圖範圍bean沒有實例化,但是解析了xhtml並觸發了prerenderview事件.. 。爲什麼當它的條件包括? – sasa 2013-03-04 17:15:09

+1

這是因爲'ui:include'在構建視圖階段執行,渲染屬性在渲染視圖階段稍後計算。這是在JSF中工作的方式。如果這是一個問題,您必須考慮其他解決方案。考慮[這個答案](http://stackoverflow.com/questions/11989631/skip-executing-uiinclude-when-parent-ui-component-is-not-rendered/11991895#11991895)的解決方案。 – partlov 2013-03-04 18:40:20

+0

@ᴠɪɴᴄᴇɴᴛ固定。謝謝。 – partlov 2016-01-12 14:46:26

1

我轉貼partlov因爲...的答案有一定的錯誤,我corect這樣的錯誤在這個POS機的投放,並congratulatiosn partlov得多好的答案

我對你的回答做了補充 首先這是一個帶有素面的xhtml頁面,如果你想使用ap:添加這個frameworc或者其他的。

如果你不會在這裏下載該framewor doenload Download prime Framework for JSF

和祁門功夫了這樣

xmlns:p="http://primefaces.org/ui" 

select.XHTML

<h:head> 
    <title>Facelet Title</title> 
</h:head> 
<h:body> 
    <h:form> 
    <p:commandButton value="Add" update="panel"> 
     <f:setPropertyActionListener value="add" target="#{myBean.action}"/> 
    </p:commandButton> 
    <p:commandButton value="Update" update="panel"> 
     <f:setPropertyActionListener value="update" target="#{myBean.action}"/> 
    </p:commandButton> 


    <h:panelGroup id="panel"> 
     <h:panelGroup rendered="#{myBean.action == 'add'}"> 
     <ui:include src="headerLogin.xhtml"/> 
     </h:panelGroup> 
     <h:panelGroup rendered="#{myBean.action == 'update'}"> 
     <ui:include src="Pajax.xhtml"/> 
     </h:panelGroup> 
    </h:panelGroup> 
    </h:form> 
</h:body> 

NEX步驟是創建一個CLAS爲myBean和使用此字符串作爲您想要渲染的選定的用戶界面 myBean.java

使用這種進口豆

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 

,並使用此鱈魚在CLAS

public class myBean { 
String action; 
public void setAction(String action) { 

    this.action = action; 
} 

public String getAction() { 
    return action; 
} 

}

但要記住把這個陣容的一類

@ManagedBean 
@SessionScoped 
+0

這個例子工作嗎?我複製粘貼代碼並嘗試,但它沒有按預期工作。我需要刷新頁面以繼續下一個選項。任何幫助表示讚賞。 – 2014-09-08 04:10:36