Chapter 11. Component Development Kit

In this chapter, we are going to create a simple JSF component using the Component Development Kit (CDK). Developing a JSF component is not a simple task, and if you do it without the help of the CDK, you will have to write a lot of standard (and not component-specific) code that includes UIComponent class, Renderer class, Tag class, configuration files, and so on. The CDK offers a lot of advantages such as a pre-generated project skeleton to start quickly, a generator that does the task of repetitive task generation using simple configuration files, built-in Ajax functionalities, testing, optimization for specific JSF version, creation of the component renderer using a JSP-like markup code, support for JSF skinning, and more.

In order to show how to start and its basic features, we are going to develop a star rating component that accepts a float value and renders it as stars.

Let's start!

Configuring the environment

All the CDK is based on Apache Maven (http://maven.apache.org/) that is widely used to generate the skeleton code that we can use as a base to work on.

Maven is a tool used by a lot of Java projects in order to manage the project management and building phases. It is close (as a concept) to Apache Ant and very useful to build automation.

Maven can dynamically download dependencies needed to create a project and is very useful to create from scratch and maintain project templates.

Installing Maven

If you haven't installed Maven before starting we need to install it. In order to do so, visit the Maven website (http://maven.apache.org), download it, and follow the instructions to complete the installation.

Configuring

After we have correctly installed Maven, we have to configure it to use it with the CDK.

The setting.xml file contains all the general Maven settings we can use in our Maven projects.

In our case, we are going to add the cdk profile to the settings element; this profile enables general resources such as cdk-specific repositories and plugins, that will be shared among the pom.xml files used for our CDK components.

Go to the maven directory and open the conf/settings.xml file inside an editor, adding a new profile (inside the profiles section) for CDK:

<profile>
<id>cdk</id>
<repositories>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/1</url>
<layout>legacy</layout>
</repository>
<repository>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
<id>repository.jboss.com</id>
<name>Jboss Repository for Maven</name>
<url>http://repository.jboss.com/maven2/</url>
<layout>default</layout>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>maven.jboss.org</id>
<name>JBoss Repository for Maven Snapshots</name>
<url>http://snapshots.jboss.org/maven2/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</pluginRepository>
<pluginRepository>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
<id>repository.jboss.com</id>
<name>Jboss Repository for Maven</name>
<url>http://repository.jboss.com/maven2/ </url>
<layout>default</layout>
</pluginRepository>
</pluginRepositories>
</profile>

As you can see, we have added repository and pluginRepository resources that will be used by our pom.xml file.

After that, we have to activate the profile. In order to do that, just uncomment the activeProfiles section, as in the following code:

<activeProfiles>
activeProfile>cdk</activeProfile>
</activeProfiles>

Now the environment is ready and we can start creating our new component project!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.154.139