开发者

Facelet : <ui:insert> cannot be put inside attribute value problem

Im currently experimenting with facelets in JSF 2.0.

I currently have a case where a common grid definition is used by 2 facelets, differs only in some areas like the bean name, the list name, the title, the grid id.

So what i have in mind is :

  1. create template for that grid, making use of <ui:insert> in the areas that can differ between pages that use it
  2. those 2 pages, make use of the template, supplying the parameters for the template with <ui:define>

Here is my 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 n开发者_JAVA百科ame='list' />)} records<br />
            </f:facet>
        </p:dataTable>
    </ui:composition>
</h:body>
</html>

And here's one of the facelet that makes use of the grid template :

<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>

And this experiment ends up with :

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)

I assume that this approach is not plausible, hence there should be a better solution for this kind of needs.

Should i use JSF Facelet Tag or JSF Composite Tag for this kind of case ?

Please share your opinions .. Thank you !


You have to make a composite component instead of template

<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>

This component has to be placed in a folder inside the resources folder, on the root of your webapp. The name of the component will be the same as the filename. So for this example lets call the component myComponent inside the /resources/components folder:

/resources/components/myComponent.xhtml

Then in any page you want to include the component you just have to do include the namespace of your component

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

and render your component:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜