2011-06-02 138 views
14

在JSF2中,是否可以更改ui src的值:使用Ajax請求動態包括(例如PrimeFaces p:commandButton)? 謝謝。JSF動態包含使用Ajax請求

<h:form>       
    <h:commandLink value="Display 2" action="#{fTRNav.doNav()}"> 
     <f:setPropertyActionListener target="#{fTRNav.pageName}" value="/disp2.xhtml" /> 
    </h:commandLink> 
</h:form> 

<ui:include src="#{fTRNav.pageName}"></ui:include> 

這就是我現在所擁有的。是否有可能使它成爲Ajax(使用p:commandButton)?

回答

16

所提出的JSTL標籤在另一個答案中的d是沒有必要的,它不是很好的重用。

下面是一個使用純JSF一個基本的例子(假設你運行的Servlet 3.0/EL 2.2,否則你確實需要在你的問題使用<f:setPropertyActionListener>等爲):

<h:form> 
    <f:ajax render=":include"> 
     <h:commandLink value="page1" action="#{bean.setPage('page1')}" /> 
     <h:commandLink value="page2" action="#{bean.setPage('page2')}" /> 
     <h:commandLink value="page3" action="#{bean.setPage('page3')}" /> 
    </f:ajax> 
</h:form> 

<h:panelGroup id="include"> 
    <ui:include src="#{bean.page}.xhtml" /> 
</h:panelGroup> 

private String page; 

@PostConstruct 
public void init() { 
    this.page = "page1"; // Ensure that default is been set. 
} 

// Getter + setter. 
+0

是的,我使用的是servlet 3.0,但我不太確定EL的版本(我怎麼知道它?)。我正要問這個問題,因爲我絕對不想使用JSTL。這個看起來更好。謝謝。 – 2011-06-03 13:52:28

+1

EL 2.2與Servlet 3.0攜手並進。所以只要你的web.xml聲明符合Servlet 3.0(你的'/ WEB-INF/lib'中沒有任何專有的'el-api.jar','el-impl.jar'等)它會工作。 – BalusC 2011-06-03 14:08:33

1

您需要圍繞ui:include使用<c:if test="condition">標籤,然後在單擊ajax按鈕時,刷新包含ui:include的面板。

首先確保JSTL核心標籤庫是通過在文檔中插入下面的命名空間包括:

<html xmlns:c="http://java.sun.com/jsp/jstl/core>"

然後,您可以使用<c:if>標籤如下:

<c:if test="#{!logBean.loggedIn}"> 
    <ui:include src="loginpage.xhtml" /> 
</c:if> 
<c:if test="#{logBean.loggedIn}"> 
    <ui:include src="home.xhtml" /> 
</c:if> 
+0

這是否適用於ajax請求? – 2011-06-02 19:44:25

+1

謝謝。它實際上就是這樣的。 – 2011-06-02 19:56:01

3

這裏是我如何使用MnagedBean動態地呈現子內容。首先,我在中心設置頁面(將通過菜單進行更改觸發)與private String name="/main_pages/mainpage.xhtml",然後單擊各個時間子菜單中爲helloBean重置"name"和內容由update=":content"更新 - 那麼新的名稱是從豆檢索:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:p="http://primefaces.org/ui"> 




     <h:head> 
      <f:facet name="first"> 
       <meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/> 

      </f:facet> 
     </h:head> 

     <h:body> 

      <p:layout fullPage="true"> 

       <p:layoutUnit position="north" size="150" resizable="true" closable="true" collapsible="true"> 
        <h1>Madeline<br>shop</br></h1> 
       </p:layoutUnit> 

       <p:layoutUnit position="south" size="100" closable="true" collapsible="true"> 
        Zapraszamy do odwiedzania naszego biura! 
       </p:layoutUnit> 

       <p:layoutUnit position="west" size="175" header="Menu" collapsible="true"> 
        <h:form> 
        <p:menu> 
          <f:ajax render=":content"> 
          <p:menuitem value="O naszej agencji" action="#{helloBean.setName('/main_pages/onas.xhtml')}" update=":content" /> 
          <p:menuitem value="Ubezpieczenia pojazdów" action="#{helloBean.setName('/main_pages/ubpoj.xhtml')}" update=":content" /> 
          <p:menuitem value="Ubezpieczenia majątkowe" action="#{helloBean.setName('/main_pages/ubmaj.xhtml')}" update=":content" /> 
          <p:menuitem value="Ubezpieczenia na życie" action="#{helloBean.setName('/main_pages/ubnaz.xhtml')}" update=":content" /> 
          <p:menuitem value="Zapytaj" action="#{helloBean.setName('/main_pages/zapytaj.xhtml')}" update=":content" /> 
          <p:menuitem value="Kontakt" action="#{helloBean.setName('/main_pages/kontakt.xhtml')}" update=":content" /> 
          </f:ajax> 
        </p:menu> 
        </h:form> 
       </p:layoutUnit> 

       <p:layoutUnit position="center"> 

        <br></br><br></br> 
        <p:panel id="content"> 
             <ui:include src="#{helloBean.name}" /> 
        </p:panel>  

       </p:layoutUnit> 

      </p:layout> 

     </h:body> 



</html> 

我的ManagedBean:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.RequestScoped; 
import java.io.Serializable; 
/** 
* 
* @author root 
*/ 
@ManagedBean 
@RequestScoped 
public class HelloBean implements Serializable { 

    /** 
    * Creates a new instance of HelloBean 
    */ 
    private static final long serialVersionUID = 1L; 

    private String name="/main_pages/mainpage.xhtml"; 

    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
}