webtier@glassfish.java.net

Re: [webtier] JSF2: Facelet tags vs Composite components

From: <webtier_at_javadesktop.org>
Date: Thu, 23 Jul 2009 01:53:11 PDT

Here is a sample:

I have this content in a taglib configuration:

<facelet-taglib
    xmlns="http://java.sun.com/JSF/Facelet"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://mojarra.dev.java.net/source/browse/*checkout*/mojarra/trunk/jsf-api/doc/web-facelettaglibrary_2_0.xsd">

    <namespace>http://www.domain.com/tags</namespace>
    
    <tag>
        <tag-name>page</tag-name>
        <source>tags/page.xhtml</source>
    </tag>
    
    <tag>
        <tag-name>text</tag-name>
        <source>tags/text.xhtml</source>
    </tag>
            
</facelet-taglib>


Here is the content of page.xhtml:

<html
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:composite="http://java.sun.com/jsf/composite"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    
    <composite:interface />
    
    <composite:implementation>
        <h:outputText value="&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;" escape="false" />
        <html>
                    <f:view contentType="text/html;charset=UTF-8">
                                <h:head>
                                        <title>Text</title>
                                </h:head>
                                <h:body>
                                        <ui:insert />
                                </h:body>
                    </f:view>
            </html>
    </composite:implementation>
    
</html>

and text.xhtml:

<html
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:composite="http://java.sun.com/jsf/composite">
    
    <composite:interface>
        <composite:attribute name="value" required="false" />
    </composite:interface>
    
    <composite:implementation>
        Value: #{value}<br />
        <c:forEach var="attr" items="#{component.attributes}">
                #{attr.key}: #{attr.value}<br />
        </c:forEach>
    </composite:implementation>

</html>

I use it like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<t:page
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:t="http://www.domain.com/tags">
    
    <c:set var="value" value="BAD" />
    <t:text value="TEST" />
        
</t:page>


The HTML output is the following and unfortunately it's that well formatted:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
           <title>Text</title>
        </head>
        <body>
        Value: TEST<br />
        javax.faces.component.VIEW_LOCATION_KEY: /WEB-INF/tags/page.xhtml @17,13<br />
        com.sun.faces.facelets.MARK_ID: 615724220_2deafbcf<br />
    </body>
</html>

So, the #{component.attributes} provides information about the "page" tag and not about the "text" tag. The original problem I was describing is that when the value attribute is not specified then the variable containing "BAD" value is displayed inside the "text" tag.

It works better with the new composite components and I think I was able to achieve everything I wanted for now. Are there required attributes supposed to work? I don't see any exception if I don't provide a required composite attribute.

It will be good if there is better control over the formatting of the generated code.

Thanks!
[Message sent by forum member 'rostislav' (rostislav)]

http://forums.java.net/jive/thread.jspa?messageID=357164