The Artima Developer Community
Sponsored Link

Java Buzz Forum
Facelets makes JSF fun (or: Goodbye JSP)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Norman Richards

Posts: 396
Nickname: orb
Registered: Jun, 2003

Norman Richards is co-author of XDoclet in Action
Facelets makes JSF fun (or: Goodbye JSP) Posted: Oct 11, 2005 12:18 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Norman Richards.
Original Post: Facelets makes JSF fun (or: Goodbye JSP)
Feed Title: Orb [norman richards]
Feed URL: http://members.capmac.org/~orb/blog.cgi/tech/java?flav=rss
Feed Description: Monkey number 312,978,199
Latest Java Buzz Posts
Latest Java Buzz Posts by Norman Richards
Latest Posts From Orb [norman richards]

Advertisement

Working with JSF has been an interesting experience. My first impressions were mixed, but positive. I loved the idea behind JSF, but the actual usage of JSF was a bit awkward.

My first apps were standard J2EE + JSF apps. They worked fine, and using JSF was pleasant enough. But there wasn't much that would make me recommend JSF over other technologies. Later I moved to EJB3 + JSF. That was a huge improvement, but the simplifications in EJB3 and wonder of annotations made me begin to wish I could have the same kind of simplifications at the EJB3 tier. Of course, then Seam rolled around, brining annotations and EJB3 services to front end web components. My experience with Seam led me to one final piece of the JSF puzzle, Facelets.

Facelets is a replacement view technology that solves all of the ugliness of using JSF with JSPs. A JSP file needs to be compiled to a servlet, which is then invoked at runtime to generate a component tree which is the rendered. (or at least that is my understanding - I'm no JSF guru) If you've had to add f:verbatim at seemingly random places in your JSPs when working with JSF, you've run into one of the many quirks in using JSP+JSF. I didn't mind that as much as I minded that JSTL tags didn't seem to mix well with JSF tags. There's no doubt that JSF+JSP is quirky at best.

Facelets provides a templating system to create the UI component tree. It is simple but powerful, and has none of the quirks that JSPs have. At it's simplest, it is almost indistinguishable from a JSP:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>My page</title>
    </head>

    <body>
        <h1>Simple Product View</h1>
        <b>Title</b>: #{selectedProduct.title} <br />
        <b>Description:</b> #{selectedProduct.description} <br />
        <b>Price:</b> #{selectedProduct.price} <br />
    </body>
</html>

Facelets makes templating very simple. Here's a trivial HTML template that defines two insertion points: title and body.

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets">
<head>
    <title><ui:insert name="title">My Application</ui:insert></title>
</head>
<body>
    <ui:insert name="body">
    </ui:insert>
</body>
</html>

Templates can be invoked using a ui:composition with ui:define

<ui:composition template="template.xhtml"
                
xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:h="http://java.sun.com/jsf/html">
    <ui:define name="body">

        Products:

        <h:form>
            <h:dataTable value="#{products}"
                         
var="p">
                <h:column>
                    <f:facet name="header">title</f:facet>
                    #{p.title}
                </h:column>

                <h:column>
                    <f:facet name="header">description</f:facet>
                    #{p.description}
                </h:column>

                <h:column>
                    <f:facet name="header">price</f:facet>
                    #{p.price}
                </h:column>

                <h:column>
                    [<h:commandLink action="#{productcontroller.viewProduct}" value="Show" />]
                    [<h:commandLink action="#{productcontroller.editProduct}" value="Edit" />]
                    [<h:commandLink action="#{productcontroller.deleteProduct}" value="Destroy" />]
                </h:column>
            </h:dataTable>
        </h:form>

    </ui:define>
</ui:composition>

If preserving HTML editability is important, you can insert default HTML into the template and place HTML around the ui:composition to make both pages interesting to a web designer. You can also forgo the JSF tags and use a Tapestry-like jsfc attributes. Here's a trivial example:

                    [<a href="#" jsfc="h:commandLink" action="#{productcontroller.viewProduct}">Show</a>]

Facelets makes including external content very easy. It's much cleaner than JSP, and you get around the annoying JSF subview issues.

        <ui:define name="sidebar">
            <ui:include src="/WEB-INF/incl/login.xhtml" />
            <ui:include src="/WEB-INF/incl/searchbox.xhtml" />
            <ui:include src="/WEB-INF/incl/cart.xhtml" />
        </ui:define>

For completeness, here's the sidebar item referenced above:

<div class="menu"
     xmlns="http://www.w3.org/1999/xhtml"
     xmlns:ui="http://java.sun.com/jsf/facelets"
     xmlns:f="http://java.sun.com/jsf/core"
     xmlns:
h="http://java.sun.com/jsf/html">
    <dl>
        <dt class="menuHeader">#{msgs.browseSearchHeader}</dt>
        <dd class="menuForm">
            <h:form>
                <dl>
                    <dt><h:outputText value="#{msgs.searchTitle}" /></dt>
                    <dd><h:inputText value="#{search.title}" size="15"/></dd>

                    <dt><h:outputText value="#{msgs.searchActor}" /></dt>
                    <dd><h:inputText value="#{search.actor}" size="15"/></dd>
                    
                    <dt><h:outputText value="#{msgs.searchCategory}" /></dt>
                    <dd>
                        <h:selectOneMenu value="#{search.category}">
                            <f:selectItems value="#{search.categories}" />
                        </h:selectOneMenu>
                    </dd>
                    
                    <dd>
                        <h:commandButton action="#{search.doSearch}"
                                         
value="#{msgs.searchButton}"
                                         
class="formButton" style="width: 166px;"/>
                    </dd>
                </dl>
            </h:form>
        </dd>
    </dl>
</div>

I'm perfectly comfortable with those JSF tags, but you could have easily written this template to be more friendly to an web guy if you prefer. It's really up to you. The nice thing about Facelets is that everything works exactly as you would expect. (yes, even JSTL tags - or at least the subset of them that facelets provides) The only thing I'd mark as a negative is that it is a bit annoying to declare all of the XML namespaces in each document. It would be nice if I could do that once for my entire project and just use them. The second downside is that normal JSP taglibs don't apply. If you have a huge investment in taglibs, you might have to do some coding to make them work under Facelets. However, the advantages of Facelets in my mind make that well worth it. I've been using Facelets for a month or two now and have no plans to ever voluntarily go back to JSPs.

Read: Facelets makes JSF fun (or: Goodbye JSP)

Topic: LINQ 101 to Ruby 101 to Groovy 101 Previous Topic   Next Topic Topic: Eclipse JSR220 ORM Plugin

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use