Enterprise Java

JSF 2.2 Create a custom Hello World component in 30 seconds

Let’s jump directly to the cool stuff and say that in JSF 2.0 a custom component was made available to page authors by configuring it in a Facelet tag library (*taglib.xml). Moreover, when the component is mapped in a JAR, a special entry in web.xml is needed to point to the *taglib.xml file. As of JSF 2.2, we don’t need these files anymore. A JSF 2.2 simple custom component contains a single class, and it may look like the following code:

 @FacesComponent(value = "components.HelloWorldComponent", createTag = true)
public class HelloWorldComponent extends UIComponentBase {

 @Override
 public String getFamily() {
  return "hello.world.component";
 }

 @Override
 public void encodeBegin(FacesContext context) throws IOException {
  ResponseWriter writer = context.getResponseWriter();
  writer.write("Hello World!");
 }
}

Most of the hard work is accomplished by the @FacesComponent annotation (javax.faces.component.FacesComponent). All we need to do is set the createTag element to true, and JSF should create the tag for us. Further, we can easily exploit our custom components, as shown in the following code:

 <?xml version='1.0' encoding='UTF-8' ?>
<!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://xmlns.jcp.org/jsf/html"
      xmlns:t="http://xmlns.jcp.org/jsf/component">
 <h:head>
  <title></title>
 </h:head>
 <h:body>
  <t:helloWorldComponent/>
 </h:body>
</html>

Note: Notice that the default namespace of the component is http://xmlns.jcp.org/jsf/component. This is true for all components that don’t have an explicit namespace.

The entire list of elements supported by JSF 2.2 @FacesComponent is as follows:

  • createTag: This can be set to true or false. When it is set to true, JSF will generate the tag for us (to be more specific, JSF will create, at runtime, a Facelet tag handler that extends ComponentHandler). This element can be used only in JSF 2.2.
  • tagName: This allows us to indicate the tag name. When createTag is set to true, JSF will use this name for the generated tag. This element can only be used in JSF 2.2.
  • namespace: This allows us to indicate the tag namespace. When createTag is set to true, JSF will use this namespace for the generated tag. When namespace is not specified, JSF will use the http://xmlns.jcp.org/jsf/ component namespace. This element can be used only in JSF 2.2.
  • value: This element comes from JSF 2.0 and indicates the component type. The component type can be used as the argument of the Application.createComponent(java.lang.String) method for creating instances of the Component class. As of JSF 2.2, if the value element is missing or is null, JSF will obtain it by calling the getSimpleName() method on the class to which @FacesComponent is attached and lowercasing the first character.

Anghel Leonard

Anghel Leonard is a senior Java developer with more than 13 years of experience in Java SE, Java EE, and related frameworks. He has written and published more than 50 articles about Java technologies and more than 500 tips and tricks for many websites that are dedicated to programming. In addition, he has written many books including Mastering JavaServer Faces 2.2.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Poschi
Poschi
8 years ago

But what’s about attribute definition for tags? My taglib.xml looks like this:

[…]

example

MyComponent

data
com.example.MyComponentDataProvider
true
Source for data

transparent
java.lang.Boolean
Make the background color transparent

[…]

Is it also possible to replace the attribute parts by an annotation?

Anghel Leonard
Anghel Leonard
8 years ago
Reply to  Poschi

This is a simple approach. For more details please check here: http://www.omnifaces-fans.org/2014/11/omnifaces-component-family-component.html

Back to top button