A simple JSF facelets taglib example
November 03, 2011 19:47:38 Last update: November 08, 2011 20:24:47
This is a step-by-step example to create a really simple facelet taglib (in JSF 2 with Maven).
To use the taglib, simply drop the jar file into
- Create a simple Maven project with:
mvn archetype:create -DgroupId=com.example -DartifactId=facelet-tag
Three files are created as a result:-
pom.xml -
src/main/java/com/example/App.java -
src/test/java/com/example/AppTest.java
This project should be able to build with:mvn package
-
- Add facelet API dependencies to
pom.xml:<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>facelet-tag</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>facelet-tag</name> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.myfaces.core</groupId> <artifactId>myfaces-api</artifactId> <version>2.1.3</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project>
The compiler plugin section is optional.
- Remove
src/main/java/com/example/App.java, create a new Java class as the facelet Tag Handler (HelloTagHandler.java):package com.example; import java.io.IOException; import javax.faces.component.UIComponent; import javax.faces.component.UIComponentBase; import javax.faces.context.FacesContext; import javax.faces.context.ResponseWriter; import javax.faces.view.facelets.FaceletContext; import javax.faces.view.facelets.TagAttribute; import javax.faces.view.facelets.TagHandler; import javax.faces.view.facelets.TagConfig; /** * Custom facelet tag that says hello. * */ public class HelloTagHandler extends TagHandler { private String name = "Anonymous"; public HelloTagHandler(TagConfig config) { super(config); TagAttribute a = getAttribute("name"); if (a != null) { name = a.getValue(); } } public void apply(FaceletContext context, UIComponent parent) throws IOException { UIComponentBase c = new UIComponentBase() { public void encodeEnd(FacesContext ctx) throws IOException { ResponseWriter w = ctx.getResponseWriter(); w.write(String.format( "<p>Hello %s! I am FaceletTag.</p>", name )); } // abstract method in base, must override public String getFamily() { return "com.example.facelettag.test"; } }; parent.getChildren().add(c); } }
This tag handler simply prints a "Hello" message.
- Create facelet tag declaration file
src/main/resources/META-INF/hello.taglib.xml:<?xml version="1.0" encoding="UTF-8"?> <facelet-taglib xmlns="http://java.sun.com/xml/ns/javaee" version="2.0"> <namespace>http://www.example.com/ns/facelettags</namespace> <tag> <tag-name>hello</tag-name> <handler-class>com.example.HelloTagHandler</handler-class> </tag> </facelet-taglib>
- Build the JAR with
mvn clean package
- Optionally, install it to the local repository:
mvn install
To use the taglib, simply drop the jar file into
WEB-INF/lib inside the WAR. This is a test page:
<!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:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:my="http://www.example.com/ns/facelettags"> <h:head> <title>Facelet Tag Test</title> </h:head> <h:body> <h1>Facelet Tag Test</h1> <my:hello name="Jack"/> </h:body> </html>