2011-04-13 34 views
1

我目前正在JSF 2.0中使用facelets進行實驗。Facelet:<ui:insert>不能放入屬性值問題

我目前有一個情況,其中一個常見的網格定義被2個facelets使用, 僅在某些區域(如bean名稱,列表名稱,標題,網格ID)有所不同。

因此,我已經記:

  1. 爲網格中創建模板,使得該地區使用的<ui:insert>可以使用它
  2. 那些2頁頁之間的不同,使用的模板,用<ui:define>

這裏提供模板的參數是我gridTemplate.xhtml:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:p="http://primefaces.prime.com.tr/ui" 
    xmlns:fn="http://java.sun.com/jsp/jstl/functions"> 

<h:head> 
    <title>#{msgs.title}</title> 
</h:head> 

<h:body> 
    <ui:composition template="/template/masterlayout.xhtml"> 
     <p:dataTable id="<ui:insert name='gridId' />" var="rpb" 
      value="#{<ui:insert name='bean' />.<ui:insert name='list' />}"> 
      <f:facet name="header"> 
       <h3><ui:insert name='title' /></h3> 
      </f:facet> 

      ..... 

      <f:facet name="footer"> 
       #{fn:length(<ui:insert name='bean' />.<ui:insert name='list' />)} records<br /> 
      </f:facet> 
     </p:dataTable> 
    </ui:composition> 
</h:body> 
</html> 

及這裏的利用網格模板的facelet之一:

<ui:composition template="gridTemplate.xhtml"> 
    <ui:define name="gridId">grid</ui:define> 
    <ui:define name="bean">myBean</ui:define> 
    <ui:define name="list">myList</ui:define> 
    <ui:define name="title">my message !</ui:define> 
</ui:composition> 

而這個實驗結束了:

javax.servlet.ServletException: Error Parsing /gridTemplate.xhtml: Error Traced[line: 17] The value of attribute "id" associated with an element type "null" must not contain the '<' character. 
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:325) 

root cause 

javax.faces.view.facelets.FaceletException: Error Parsing /gridTemplate.xhtml: Error Traced[line: 17] The value of attribute "id" associated with an element type "null" must not contain the '<' character. 
    com.sun.faces.facelets.compiler.SAXCompiler.doCompile(SAXCompiler.java:394) 
    com.sun.faces.facelets.compiler.SAXCompiler.doCompile(SAXCompiler.java:368) 
    com.sun.faces.facelets.compiler.Compiler.compile(Compiler.java:124) 
    com.sun.faces.facelets.impl.DefaultFaceletFactory.createFacelet(DefaultFaceletFactory.java:297) 
    com.sun.faces.facelets.impl.DefaultFaceletFactory.access$100(DefaultFaceletFactory.java:92) 
    com.sun.faces.facelets.impl.DefaultFaceletFactory$1.newInstance(DefaultFaceletFactory.java:162) 
    com.sun.faces.facelets.impl.DefaultFaceletFactory$1.newInstance(DefaultFaceletFactory.java:161) 
    com.sun.faces.facelets.impl.DefaultFaceletCache$1.newInstance(DefaultFaceletCache.java:83) 
    com.sun.faces.facelets.impl.DefaultFaceletCache$1.newInstance(DefaultFaceletCache.java:79) 
    com.sun.faces.util.ExpiringConcurrentCache$1.call(ExpiringConcurrentCache.java:99) 
    java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) 
    java.util.concurrent.FutureTask.run(FutureTask.java:138) 

我認爲這種做法是合理的,因此應該爲這種需求提供更好的解決方案。

我應該在這種情況下使用JSF Facelet標籤或JSF Composite Tag嗎?

請分享您的意見..謝謝!

回答

6

你必須做的,而不是模板

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:cc="http://java.sun.com/jsf/composite"> 

<cc:interface> 
    <cc:attribute name="list"/> 
    <cc:attribute name="title"/> 
</cc:interface> 
<cc:implementation> 
    <p:dataTable id="#{cc.id}" var="rpb" 
      value="#{cc.attrs.list}"> 
      <f:facet name="header"> 
       <h3>#{cc.attrs.title}</h3> 
      </f:facet> 

      ..... 

      <f:facet name="footer"> 
       #{fn:length(cc.attrs.list)} records<br /> 
      </f:facet> 
     </p:dataTable> 

</cc:implementation> 
</html> 

該組件必須放在一個文件夾中的文件夾resources裏面,在你的web應用程序的根目錄中的複合材料部件。組件的名稱將與文件名相同。所以,在這個例子中可以調用組件myComponent/resources/components文件夾中:

/resources/components/myComponent.xhtml 

然後在任意頁面要包括你只需要做包括您的組件的命名空間中的分量

xmlns:albert="http://java.sun.com/jsf/composite/components" 

和渲染你的組件:

<albert:myComponent id="..." title="..." list=#{someBean.someList} /> 
+0

你好,感謝你分享你的想法,利用複合組件來解決問題。但是我有這個疑問,是否僅僅爲了在一個2 jsf頁面上共享一個網格而製作一個複合組件是有點多? – bertie 2011-04-13 15:29:57

+0

這個答案很直接,並且比網上關於這個主題的幾個「教程」更準確。祝賀 – Leo 2015-02-28 20:44:39