`

深入 Facelets (二)使用Facelets

阅读更多

Getting Started

Facelets is implemented as a JSF ViewHandler, which can be easily configured in your application. Simply make sure the Facelets JAR (jsf-facelets.jar) is in your project's classpath and add a single modification to your faces-config.xml:

  1. <faces-config>  
  2.   <application>  
  3.     <!-- tell JSF to use Facelets -->  
  4.     <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>  
  5.   </application>  
  6. </faces-config>  
 

 

JavaServer Faces defaults to JSP files for defining views (*.jsp). You will want to change this to some other type in your WEB-INF/web.xml.

 

  1. <context-param>  
  2.   <param-name>javax.faces.DEFAULT_SUFFIX</param-name>  
  3.   <param-value>.xhtml</param-value>  
  4. </context-param>  
 

According to the context-param defined above, you can start writing Facelets and saving them with .xhtml extensions. You are now ready to start using Facelets.

Built-in Libraries

Facelets comes ready to use all of the UIComponents in the JavaServer Faces API under the same rules as you would with JSP. This means you may reference the online documentation for JSF's JSP tag libraries.

There is also a simple version of JSTL's core library available which gives you

 
  1. <c:forEach/>  

 <c:foreach></c:foreach>and

 
  1. <c:if/>  

 <c:if></c:if>.

Finally, there is a UI tag library built into Facelets which gives you all of the templating and re-use capabilities. More on this in another article.

Creating a Facelet

Facelets only needs to be proper XML. They aren't dependent on XHTML syntax and could be used along side WML, SVG, or even SOAP. With JSP, you often times import tag libraries, but Facelets just looks for namespaces at compilation, similar to JSPX. Here is the start of a XHTML Facelet document:

  1. <html xmlns="http://www.w3.org/1999/xhtml"  
  2.       xmlns:h="http://java.sun.com/jsf/html"  
  3.       xmlns:f="http://java.sun.com/jsf/core">  
  4. <head>  
  5. <meta http-equiv="Content-Type"    
  6.         content="text/html; charset=iso-8859-1" />  
  7. <title>My First Facelet</title>  
  8. </head>  
  9. <body>  
  10.       <!-- body content goes here -->  
  11.       Hello #{param.name}!   
  12. </body>  
  13. </html>  

You should be able to save this code as test.xhtml and then immediately request test.jsf?name=Jacob (if your FacesServlet was mapped to *.jsf). This should be a good test to guarantee that you are setup correctly for Facelets.

Using EL

Facelets uses the new EL-API specification with JSF. This means that you can use either ${ } or #{ } interchangeably unlike JSP. As the example above shows, expressions can be inlined with regular text.

If you are using JSF 1.2, you can add your own custom ELResolvers for integrating Spring, EJB, JNDI, etc.-- all at once if you choose. You can even have ELResolvers of your own that work with objects specific to your application.

The EL-API is part of the JSP 2.1 specification, but doesn't have any dependencies on JSP or JSF. This means that it can be used with Facelets in any kind of deployment.

Using Components

In the example test.xhtml above, you will notice that there are extra namespaces declared. These are the same namespaces included in the JavaServer Faces API for the built-in JSP tag libraries. Again, Facelets wants to build off of familiar territory and documentation.

When you start using the built-in JSF components with Facelets, it's a good idea to have the tag library documentation available.

Since we already have our test.xhtml open, lets create a very simple form. Add the following code to the body of your page:

<h:form>
 
<h:commandbutton action="#{person.action}"></h:commandbutton>
  1. <h:form>  
  2.     <h:inputText value="#{person.name}"/>  
  3.     <h:commandButton action="#{person.action}"/>  
  4. </h:form>  
</h:form>

The next step would be creating your person bean and modifying your faces-config.xml to back this simple example.

Aliasing Components (jsfc)

The previous example makes use of special tags that probably won't look so hot inside of an HTML designer tool like Dreamweaver. Facelets offers a different way to specify components within your page with the jsfc attribute within a standard HTML element (much like Tapestry and Struts Shale's Clay plugin. )

The Facelets compiler looks for the jsfc attribute for every element in the document. The value of the jsfc attribute is an alternate element name or "qname" to replace the one the designer used in the page.

  1. <input type="text" jsfc="h:inputText" value="#{foo.bar}"/>  

Facelets, at compile time, will instead create an h:inputText component in the document while automatically wiring all of the available attributes.

Aliasing components allows the desinger's tool to see a normal HTML input tag, but the programmer can treat it as a JSF component as specified by the jsfc attribute.

Facelet Compilation

As mentioned in the previous article, Facelets compiles valid XML. These files could be one or three lines long, again the only point is that they are valid XML.

Facelets uses SAX to compile the document into a stateless tree of TagHandlers contained within a Facelet object. Facelets are thread-safe and can easily be used in large scale deployments by multiple requests at once. Facelets will provide warning messages at compilation time along with information on libraries used and view handling within JSF.

Executing Facelets compilation is simple, as shown below:

java 代码
  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);  

More information about Facelet's architecture will be provided in a later article.

Exceptions & Debugging

Great care was taken with Facelets and it's error handling. Lets say for a moment that you mistyped an expression in an attribute. Facelets will generate an error like this at compilation:

  1. greeting.xhtml @18,97 <input action="#{success[}"> Error Parsing: #{success[}  

As you can see above, Facelets provides you with error message that tell you what file, line, and column number the error occured. More specifically it also provides the element and attribute that was errant.

Facelets also uses the java.util.logging package to handle output of debug messages. You can read more here on setting up logging with your JRE.

Custom Components

Facelets allows you to use an XML file or Java code to define your component libraries, although I will only go through the XML configuration option in this article as it is the preferred choice.

<facelet-taglib></facelet-taglib>
<component>
 
  1. <facelet-taglib>  
  2.   <namespace>http://www.mycompany.com/jsf</namespace>  
  3.   <tag>  
  4.     <tag-name>bar</tag-name>  
  5.     <component>  
  6.       <component-type>javax.faces.Data</component-type>  
  7.       <renderer-type>com.mycompany.Bar</renderer-type>  
  8.     </component>  
  9.   </tag>  
  10. </facelet-taglib>  
</component>

That's all you need to integrate your component into Facelets. These XML files can be refrenced in two ways:

  1. Reference them in a ";" delimitted list within your web.xml under the init-param "facelets.LIBRARIES". These files are relative to your application, just like referencing Struts configuration files.
  2. Package them in your JAR's META-INF folder with a file extension of ".taglib.xml". Facelets will automatically pick these up for compilation just as with JSP tld files.

Within a facelet-taglib file, you can also specify converters, validators, and custom TagHandlers for ultimate control over document processing.

Custom Validators & Converters

Validators and Converters can also be represented in a Facelet via the same facelet-taglib file.

  1. <facelet-taglib>  
  2.   <namespace>http://www.jsfcentral.com/public</namespace>  
  3.   <tag>  
  4.     <tag-name>validateRegExp</tag-name>  
  5.     <validator>  
  6.       <validator-id>foo.bar.RegExp</validator-id>      
  7.     </validator>  
  8.   </tag>  
  9.   <tag>  
  10.     <tag-name>convertUtilDate</tag-name>  
  11.     <converter>  
  12.       <converter-id>foo.bar.UtilDate</converter-id>      
  13.     </converter>  
  14.   </tag>  
  15. </facelet-taglib>  

Again, Facelets works closely with JavaServer Faces's existing API and this is all you have to define. Facelets will automatically wire all properties on your Converter or Validator objects.

Custom Tags

Okay, I haven't thought of it yet and you want to add your own custom tag to Facelets. This includes cases where you want unique behavior for wiring properties and attributes to UIComponents, Validators, or Converters.

Facelets provides many foundation classes you can extend in order to make your job simpler. Let's first take a look at source for <c:if></c:if>to give you an idea:

  1. public final class IfHandler extends TagHandler {   
  2.   
  3.     protected final TagAttribute test;   
  4.     protected final TagAttribute var;   
  5.   
  6.     public IfHandler(TagConfig config) {   
  7.         super(config);   
  8.         this.test = this.getRequiredAttribute("test");   
  9.         this.var = this.getAttribute("var");   
  10.     }   
  11.   
  12.     public void apply(FaceletContext ctx, UIComponent parent)   
  13.             throws IOException, FacesException, ELException {   
  14.         boolean b = this.test.getBoolean(ctx);   
  15.         if (this.var != null) {   
  16.             ctx.setAttribute(var.getValue(ctx), new Boolean(b));   
  17.         }   
  18.         if (b) {   
  19.             this.nextHandler.apply(ctx, parent);   
  20.         }   
  21.     }   

That's it! You will notice that Facelets uses the "good citizen" principle and constructor injection when the document is compiled. Facelets and all tags are stateless, unlike JSP (which also pools tag handler objects). More technical details are provided in a separate article, so lets describe what's going on in the tag.

TagHandler, which <c:if></c:if>extends, is a great example of the foundation classes you can use to develop custom tags. Think of TagHandler as JSP's TagSupport or BodyTagSupport.

 

At construction, the representation of the Tag is passed (TagConfig) which conveys the structure of the document including: location, child tags, and attributes.

TagAttributes are representations of the attributes specified in the XML document. They have many methods for handling EL expressions and coercions to the types you need. They too are stateless and their methods require passing the FaceletContext to provide the current state.

The last point of interest is that the children are represented by the member variable nextHandler. You can apply the children as many times as you want or not at all as in the case of <c:if></c:if>.

Once your custom tag is written, you can go ahead and add it to a facelet-taglib file like so:

<facelet-taglib></facelet-taglib>
  1. <facelet-taglib>  
  2.   <namespace>http://www.jsfcentral.com/public</namespace>  
  3.   <tag>  
  4.     <tag-name>if</tag-name>  
  5.     <handler-class>com.jsfcentral.IfHandler</handler-class>  
  6.   </tag>  
  7. </facelet-taglib>  

Summary

This was just a short introduction to using the Facelets framework. More articles will follow that will go into greater detail with templating, features, and customizing Facelets.

 

 
分享到:
评论

相关推荐

    facelets源码包

    这个"facelets源码包"包含了Facelets的源代码和帮助文档,是深入理解Facelets工作原理和开发自定义功能的重要资源。 1. **Facelets 概述** - Facelets 是JSF的默认视图层技术,替代了之前的JSP。 - 它基于XML,...

    facelets 教程

    #### 二、Facelets 的核心优势:简化复杂性 Facelets 的设计哲学在于简化复杂性。在传统 JSP 页面中,开发者常常需要面对繁杂的脚本语言和 HTML 标签的交织,这不仅增加了代码的可读性和维护难度,而且限制了组件...

    facelets中文教程(初级+高级)

    本教程将深入探讨Facelets的基础以及高级特性,旨在帮助初学者和有经验的开发者更好地理解和应用Facelets。 一、Facelets基础 Facelets作为JSF的默认视图描述语言,取代了原来的JSP(JavaServer Pages)。它提供了...

    facelets_demo

    Facelets 是一种用于构建用户界面的视图技术,主要在JavaServer Faces (JSF) 应用程序中使用。它提供了声明式的方式去定义页面结构和逻辑,使得开发者能够更高效地创建和维护Web应用程序的前端。在这个"facelets_...

    netbeans_Facelets_Seam_Ejb3编程入门

    《NetBeans Facelets Seam Ejb3编程入门》是一份详尽的指南,不仅涵盖了NetBeans IDE的基本配置和使用,还深入探讨了如何利用Facelets、Seam和EJB3技术开发企业级Java Web应用。对于希望使用这些技术进行开发的初学...

    JSF与Facelets应用程序

    1. **定义组件**: 使用Facelets的XML语法(通常以`.xhtml`为扩展名)创建页面,这些页面可以包含JSF组件(如`&lt;h:inputText&gt;`,`&lt;h:commandButton&gt;`等)。组件可以有属性和事件,与后台bean进行数据交互。 2. **页面...

    Facelets Essentials - APRESS

    总而言之,《Facelets Essentials》是一本专为想要深入了解和掌握Facelets框架的开发者准备的指南。通过学习本书,开发者不仅能够提高开发JSF应用的效率,还能确保应用具有更好的性能和更高的代码质量。无论你是JSF...

    Apress.Facelets.Essentials.May.2008

    总的来说,《Facelets Essentials》这本书为Java Web开发人员提供了一个深入学习Facelets的资源,涵盖了Facelets的基本概念、高级特性和实际应用,同时也可能涉及到与其他Web开发技术的比较,以帮助开发者做出最佳的...

    Apress - Definitive Guide To Apache Myfaces And Facelets (2008)

    综上所述,《Apress - Definitive Guide To Apache Myfaces And Facelets (2008)》这本书不仅为开发者提供了全面的技术指导,而且还深入探讨了许多实用技巧和最佳实践。无论是初学者还是有一定经验的开发者,都能...

    Facelets:jsf、primefaces、facelets、spring 集成、jstl、

    在Facelets中,虽然JSTL标签可以直接使用,但由于Facelets的强大功能,JSTL的使用通常较少,但仍然可以在某些场景下提供帮助。 **Facelets与Spring的集成示例** 集成Facelets和Spring通常涉及以下步骤: 1. 添加...

    facelets.......................................

    通过阅读博客和研究这些布局文件,开发者可以深入理解Facelets的页面布局机制,以及如何根据需求调整和创建自定义布局。这些布局示例为实践提供了很好的起点,可以进一步探索和应用到实际项目中。

    WebApplicationFacelets-源码.rar

    二、Facelets的核心特性 1. **模板和组件**:Facelets允许开发者定义可重用的组件,这些组件可以像HTML元素一样嵌套。通过使用模板,开发者可以创建可复用的页面布局。 2. **动态标签**:Facelets支持自定义标签,...

    WebApplicationFacelets:与 WebApplication 相同,但带有 facelets ..

    此外,运行示例应用并进行调试也是深入理解Facelets工作原理的好方法。 总的来说,WebApplicationFacelets是一个强调使用Facelets技术的Web应用示例,对于想要学习和掌握Java Web开发,特别是JSF和Facelets的开发者...

    JSF框架入门教程和JSF的jar包

    3. **编写第一个JSF页面**: 使用Facelets创建一个简单的Hello, World页面,了解组件的使用。 4. **Managed Bean的创建和使用**: 编写Managed Bean,处理页面请求和数据。 5. **调试和测试**: 运行应用,通过浏览器...

    jsf仿frame

    【JSF(JavaServer Faces)】 JSF是一种用于构建Web应用程序的JavaEE技术,它提供了用户界面组件、...开发者可以通过研究这个实例,深入理解JSF和Facelets的工作原理,以及如何利用它们来创建高效且可维护的Web应用。

    jsf经典入门

    然而,由于Facelets提供了更好的可维护性和性能,现代JSF应用通常倾向于使用Facelets。 在实际开发中,JSF可以通过以下方式提高效率: 1. **组件库**:如PrimeFaces、MyFaces和RichFaces等第三方组件库提供了大量...

    《JSF标签》简体中文版

    《JSF标签》简体中文版这本书可能深入浅出地介绍了JSF中的标签使用,帮助中文开发者更好地理解和应用这个技术。 JSF标签是JSF组件库的一部分,它们是预定义的UI组件,可以在JSP或Facelets视图中使用。这些标签通常...

    JSF架构使用教程.rar

    本教程将深入探讨JSF架构的使用,帮助开发者掌握这一强大的技术。 首先,我们需要理解JSF的核心概念。JSF架构由几个主要组件构成:视图(View)、模型(Model)和控制器(Controller)。视图负责展示用户界面,模型...

    推荐经典《JSF入门》简体中文版教程

    二、Facelets简介 Facelets是JSF的标准视图层技术,用于生成HTML页面。与JSP相比,Facelets更易于维护,支持模板和组件重用。在Facelets中,开发者可以使用XML语法定义UI组件和布局,同时支持EL(Expression ...

    JSF框架使用part1

    JSF提供了一个丰富的组件库,如输入字段、按钮、表格、数据列等,这些组件可以直接在Facelets模板中使用。此外,还有第三方库如PrimeFaces、RichFaces和ICEfaces等,它们提供了更多功能和更美观的UI组件。 **JSF与...

Global site tag (gtag.js) - Google Analytics