深入Facelets (一)简介

The web community is eagerly seeking a framework like Tapestry, backed by JavaServer Faces as the industry standard. While JavaServer Faces and JSP are meant to be aligned, Facelets steps outside of the JSP specification and provides a highly performant, JSF-centric view technology. Anyone who has created a JSP page will be able to do the same with Facelets and familiar XML-tag use. The difference is under the hood where all the burden of the JSP vendor API is removed to greatly enhance JSF as a platform and provide easy plug-and-go development without requiring JSP tag development.

JavaServer Faces UIComponents are first class citizens within Facelets; there's no need to develop extra objects to integrate. There's also no need to learn yet another XML schema to define your views.

Here's an example of how easy it is to define a view with Facelets:

  1. <html xmlns="http://www.w3.org/1999/xhtml"  
  2.       xmlns:h="http://java.sun.com/jsf/html"  
  3.       xmlns:c="http://java.sun.com/jstl/core">  
  4. <head>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />  
  6. <title>Hellotitle>  
  7. head>  
  8. <body>  
  9.     <form id="helloForm" jsfc="h:form">  
  10.         <h2>  
  11.             Hi. My name is Duke.  I'm thinking of a number from   
  12.             #{UserNumberBean.minimum} to #{UserNumberBean.maximum}.   
  13.             Can you guess it?   
  14.         h2>  
  15.         <img id="waveImage" src="wave.med.gif" />  
  16.            
  17.         <input type="text" jsfc="h:inputText" id="userNo"  
  18.                value="#{UserNumberBean.userNumber}"  
  19.                validator="#{UserNumberBean.validate}"/>  
  20.                   
  21.         <input type="submit" jsfc="h:commandButton" id="submit"  
  22.                action="success" value="Submit" />  
  23.         <p />  
  24.         <h:message showSummary="true" id="errors1" for="userNo"/>  
  25.         <p />  
  26.         <c:forEach begin="1" end="4" varStatus="v">  
  27.         #{view.viewId} #{v.index}<br/>  
  28.         c:forEach>  
  29.     form>  
  30.   
  31. body>  
  32. html>  

You may be thinking that the markup looks extremely familiar. That's the point. There's no need to learn another templating language or schema.

Why Use Facelets?

First and foremost, Facelets isn't dependent on a JSP container. This means that you can start using the new JSF 1.2 features without waiting for a JSP 2.1 container. There were some serious issues with combining JSF 1.1 and JSP use when it first came out. Many of these issues were highlighted in an article by Hans Bergsten titled, "Improving JSF by Dumping JSP." Reading that article is strongly recommended as a case for moving to JSF 1.2 and Facelets.

Secondly, Facelets is designer friendly. Taking from Tapestry's jwcid attribute, Facelets uses jsfc and proper namespaces to convert HTML elements to their associated JSF component. In the example above, jsfc (JSF Compilation) is used to tell the compiler to instead build a h:inputText component into the view.

Last to be mentioned here, Facelets is very light weight. Some of you may be familiar with Velocity's capabilities as a templating engine. Facelets has the same capabilities such that you can define and test JSF views without a Servlet container. The framework overhead is minimal and practical for production use. It also opens the doors for JSF use in deployments outside of servlets, including Portlets.

Here's how you create a new Facelet:

 
  1. // grab our FaceletFactory and create a Facelet   
  2. FaceletFactory factory = FaceletFactory.getInstance();   
  3. Facelet f = factory.getFacelet(viewToRender.getViewId());   
  4.   
  5. // populate UIViewRoot   
  6. f.apply(context, viewToRender);  

Short List of Features

Here's a short list of Faclet's current feature set:
  • Works with JSF 1.1 and JSF 1.2, including Sun's RI and Apache MyFaces.
  • Zero Tag development time for UIComponents
  • Fast Templating/Decorators for Components and Pages
  • The ability to specify UIComponent trees in separate files (UICompositions)
  • Line/Tag/Attribute precise Error Reporting
  • Specify Tags in Separate Files, even packaged with Jars
  • Full EL support, including Functions
  • Compile-time EL Validation
  • XML configuration files aren't necessary, but are available
  • Reserves the 'jsfc' attribute which acts the same as Tapestry's jwcid
    1. (Example: <input id="bar" type="text" jsfc="h:inputText" value="#{foo.bar}"/>)    
  • Plugable Decorators to really make designer's job easy
    1. (Example: transform <input type="text"/> to <h:inputText/> at compile time)    
  • Does *not* required any special RenderKits
  • Facelets isn't dependent on a Web Container

Why Facelets will Succeed

Everyone wants to be more designer friendly, and Tapestry seems to be the only choice developers are pursuing. On the other hand, JSF is the standard everyone would like to have happen, but JSF needs a more "pluggable" ViewHandler framework that is both designer and developer friendly.

Developers go through enough work defining UIComponents/Converters/Validators in their faces-config.xml, and Facelets only asks them to specify a name alias to plug their objects into their pages (no XML necessary). The key is ease of integration and development.

In conclusion, Facelets is a clean slate for correcting concerns with JSF. Templating, re-use, and ease of development are top priorities that will help bring developers on board with JSF as a suitable platform for large scale projects.

你可能感兴趣的:(jsp,xml,JSF,velocity,tapestry)