2011-04-13 30 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