Tag Archives: java-ee

Related softwares:

  1. JBoss 5.1.0
  2. Ubuntu 9.04

As of time of writing, Ubuntu repository only provides JBoss AS 4. So, to install the latest JBoss (5.1.0), you need to manually download it from:
http://sourceforge.net/projects/jboss/files/JBoss/JBoss-5.1.0.GA/jboss-5.1.0.GA-jdk6.zip/download

Once you downloaded the jboss-5.1.0.GA-jdk6.zip archive file, unzip it and put the folder jboss-5.1.0.GA in /opt folder. Thus, I’ve chosen /opt/jboss-5.1.0.GA path as my $JBOSS_HOME.

To setup the JBoss to startup on machine bootup, below are the steps.

Create a system user named jboss by running below command in the terminal:

sudo adduser --system jboss

Change the owner of $JBOSS_HOME and its subfolders to jboss user by typing below command:

 sudo chown jboss /opt/jboss-5.1.0.GA -R
 

Create a script file named jboss in /etc/init.d folder. Type in below content into the jboss script:

#! /bin/sh

JBOSS_HOME=/opt/jboss-5.1.0.GA

start(){
 echo "Starting jboss.."

 sudo -u jboss ${JBOSS_HOME}/bin/run.sh -b 0.0.0.0 > /dev/null &
}

stop(){
 echo "Stopping jboss.."

 sudo -u jboss ${JBOSS_HOME}/bin/shutdown.sh -S > /dev/null &
 #give time to shutdown jboss services.
 sleep 60
 #kill all java services started by user jboss
 su -l jboss -c 'killall java'
}

restart(){
 stop
 start
}

case "$1" in
 start)
 start
 ;;
 stop)
 stop
 ;;
 restart)
 restart
 ;;
 *)
 echo "Usage: jboss {start|stop|restart}"
 exit 1
esac

exit 0

Include above startup script into Ubuntu bootup sequence by typing command below:

sudo update-rc.d /etc/init.d/jboss defaults
sudo service jboss start

And you’re done.

References:

  1. http://www.jboss.org/community/wiki/StartJBossOnBootWithLinux

Related softwares:

  1. Eclipse 3.5
  2. JDK 1.6
  3. Maven 2.2.1

First, install M2clipse-plugin in Eclipse via below M2clipse update site:

http://m2eclipse.sonatype.org/update/

If you don’t know how, please refer to this article on how to install Eclipse plugin via update site.

To use the latest Maven2, which is version 2.2.1 at the point of writing this article, you need to download the binaries from below site:

http://lawyersdb.com/mirrors/apache/maven/binaries/apache-maven-2.2.1-bin.tar.gz

Once downloaded the apache-maven-2.2.1.tar.gz archive file, unzip/untar it and place the apache-maven-2.2.1 folder in /opt. So, /opt/apache-maven-2.2.1 will be known as $M2_HOME. To set this environment variable manuallly into the system, add this below line in /etc/environment file.

JAVA_HOME=/usr/lib/jvm/java-6-sun
M2_HOME=/opt/apache-maven-2.2.1

Add also the $JAVA_HOME variable, if you haven’t done so. But make sure it is pointing to your JDK home folder. You need to re-login into your Ubuntu for this new environment variable to take effect

After re-login, create a mvn link in /usr/bin, for you to run the mvn command globally. Type the following:

cd /usr/bin
ln $M2_HOME/bin/mvn /usr/bin/mvn

To check Maven2 installation status, type below command in your console:

mvn -version

You should see Maven2 printing some info regarding the version.

Back to the Eclipse. Now, you need to setup External Tools to point to this new /usr/bin/mvn command we just created. At Eclipse main menu, choose Run > External Tools > External Tools Configuration…

Create External Tools

External Tools Configuration dialog will be prompted as below.

Empty External Tools Configuration

Right-click on Program and choose New.

Create New External Tool Menu

A new program form will be displayed. Enter the following details:

  1. Name : Maven
  2. Location : /usr/bin/mvn
  3. Working Directory : ${project_loc}
  4. Arguments : ${string_prompt}

Maven2 External Tools Config Setup

Click on Apply and Close buttons.

To use the newly created External Tools we just created, click to highlight your Maven2 project.

Choose Maven2 Project

On the Eclipse main menu, choose Run > External Tools > External Tools Configuration…

Choose External Configuration Menu to Run Maven2

Then, on the External Tools Configuration dialog, choose Maven and click on Run button.

Choose Maven to Run External Tools

A Variable input dialog will be prompted. Key in your mvn parameter you wish to pass and click OK button.

Variable Input dialog

You will see your Maven2 console is running and processing your command as shown in below.

Maven2 console

If you had setup a private remote repository, you can save the hassle for downloading artifacts from the public remote repository (central repo). Your private remote repository will download it for you and cache it on the private server. If you want to download from your private remote repository, you need to update or create your Maven2 settings.xml file. This file should be located at ${USER_HOME}/.m2 folder. The content of the file is as following:

<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
 <pluginGroups/>
 <proxies/>
 <servers/>
 <mirrors>
   <mirror>
     <id>myPrivateRemoteRepository</id>
     <mirrorOf>*</mirrorOf>
     <name>My Private Remote Repository</name>
     <url>[PRIVATE_REMOTE_REPOSITORY_URL]</url>
   </mirror>
 </mirrors>
 <profiles/>
</settings>

Replace the [PRIVATE_REMOTE_REPOSITORY_URL] with your private remote repository URL. You can put any value on the id and name elements, but it just need to be unique between other mirrors.

Traditionally, creating a new Java project is very cumbersome and time consuming, especially when you required a lot of external libraries. I might have to go to each library’s project website to search and download the jar file. Most probably I might just copied from previous project’s libraries.

Maven and Eclipse help me to avoid this mundane work. The feature of Maven with Eclipse I like to demo right now is the ‘Search for dependency from central repository’.

First, you need to do is create a Maven Project like below.

Create New Maven Project

Create New Maven Project

Then you will have the new Maven project in your package explorer. New project will consists of JUnit library by default.

Maven Project in Package Explorer

Maven Project in Package Explorer

It will also comes with App.java class by default. I will use this java class for the demo.

Standard App.java

Standard App.java

Let’s say I need to use commons-logging library, which is not yet included in the project’s classpath. I just typed in the code and of course I will get the errors as below.

Added Log and LogFactory object.

Added Log and LogFactory object.

Next, I highlighted the ‘Log’ object and I pressed Ctrl+1 to pop out the context menu. Then, choose ‘Search dependency for Log’ option.

Search for dependency from context menu

Search for dependency from context menu

I typed ‘*logging’ to search for the specific Log’s library. It will search the maven central repository based on the pattern. Then, I select Log class from commons-logging artifact.

Choose specific library

Choose specific library

Automatically, commons-logging library will be in my project’s classpath.

Commons-logging jar file is added

Commons-logging jar file is added

Log class also will be automatically imported into App.java class.

Log object is imported

Log object is imported

Since LogFactory class is from the same library, I just need to import the class by pressing Ctrl+Shift+O. This would solve all the problem and error.

Import LogFactory class.

Import LogFactory class.

My pom file is also updated automatically.

POM updated

POM updated

Isn’t this a great feature? No more searching and downloading the libraries manually from the web. Hellooo productivitayyyy.

Need to start a project in lightning speed? If you want to skip those dirty works in setting up a project framework, well…, I just found out a great utility just for that. It is called Appfuse 2. Now with version 2, it is tied together with Maven 2 which makes the setup a lot easier. The prerequisites for applying Appfuse is Maven 2 and MySQL 5. You need to install these 2 in your machine.
There are several archetypes you could choose from, and for the sake of writing, I just choose appfuse-basic-jsf. So I just run below command, it will create a project with JSF,Spring & Hibernate. There are also other persistence files included, such as JPA and iBatis.

mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-basic-jsf -DremoteRepositories=http://static.appfuse.org/releases -DarchetypeVersion=2.0.2 -DgroupId=org.dhydrated.appfuse.jsf -DartifactId=appfuseJsfWeb

Then, I need to tweak the pom according to my MySQL installation. By default, it uses root with no password as the MySQL user. So I just simply add my password in the pom file below.


<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>org.dhydrated.appfuse.jsf</groupId>
<artifactId>appfuseJsfWeb</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>AppFuse JSF Application</name>
<url>http://www.mycompany.com</url>
...
<properties>
...
<jdbc.username>root</jdbc.username>
<jdbc.password>password</jdbc.password>
</properties>
</project>

Then, I run below command and let Maven 2 do all the boring stuffs such as dependencies checking, creating tables in database and etc. It will take a while if you doing it for the first time.

mvn

Then, I need to package it into war, so I just simply run below command.

mvn war:war

Next, I need to tweak again the pom since I’m deploying the application into Glassfish 2 in my machine. By default, the application is set to run with Jetty or Tomcat. So if your choice of application server is one of these default settings, you can skip this step. I’ll add below maven plugin to enable me to deploy my application into Glassfish 2.


<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
...
    <build>
        <finalName>${artifactId}</finalName>
<plugins>
<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>deploy</id>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>C:/Program Files/glassfish-v2ur1/bin/asadmin.bat</executable>
                    <arguments>
                        <argument>deploy</argument>
                        <argument>--user=admin</argument>
                        <argument>--passwordfile="C:/passwordfile.txt"</argument>
                        <argument>--host=localhost</argument>
                        <argument>--port=14360</argument>
                        <argument>target/${artifactId}.${packaging}</argument>
                    </arguments>
                </configuration>
            </plugin>
            ...
        </plugins>
    </build>
    ...
    </project>

Above, I’m using exec-maven-plugin to run an executable command to deploy my war into Glassfish 2. In order to call above plugin, I typed below command.

mvn exec:exec

After that, you should be able to see your application is being deploy from your server console. After all these steps, Presto!, now you have a full-fledged Java Enterprise application running on your server. Thanks to Spring, it is also equipped with security and transaction management. Couple of users with specific roles are already included.

If I run my application, I should get the Login page as below.

appfuse-jsf-login

appfuse-jsf-login

Above is the login page. One of the default user is admin.

appfuse-jsf-home

appfuse-jsf-home

Above is the home page once you are logged in. It uses Struts-Menu for menu layout.

appfuse-jsf-users

appfuse-jsf-users

It also includes CRUD pages for Users.

How’s that for speed? It only took 10 minutes to create this application (if you know how to do it, of course). There are several other UI frameworks you could choose from, besides JSF. I guess the 2 most commonly used are JSF and Struts2. If I only knew about Appfuse 2, I would have used it on my last project. It is awesome. I don’t need to do all the basic stuffs anymore and I can just straight away focus on the business requirements. Yeah!

I was given a task to work on Ms Access database. I had to choose either JDBC, iBatis and Hibernate for the persistence mechanic. So I tried each of them to see which one is the best.

First, I worked with JDBC with Spring. Below is one of the query where I mapped the result to Country object.

public class CountryDaoJdbcImpl extends AbstractJdbcDao implements CountryDao {

    public List getAllCountries() {

        List countries = getJdbcTemplate().
                query(
                "SELECT * FROM Country_Code", new RowMapper() {

            public Object mapRow(ResultSet rs, int rowNum)
                    throws SQLException, DataAccessException {
                Country country = new Country();
                country.setCode(rs.getString(1));
                country.setName(rs.getString(2));
                country.setIsdCode(rs.getString(3));
                return country;
            }
        });

        return countries;
    }
}

It is easy to implement but the code is too verbose. Plus, if I have relationships between objects, the code will be more complicated and maintenance would be tough.

Then, I tried to implement iBatis with Spring. Below is the iBatis config for an object with a relationship with another object.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="General">

    <resultMap id="CategoryResultMap" class="Category" groupBy="categoryCode">
        <result property="categoryCode" column="categoryCode" />
        <result property="description" column="description" />
    </resultMap>    

    <resultMap id="SubCategoryResultMap" class="SubCategory" groupBy="subCategoryCode">
        <result property="subCategoryCode" column="subCategoryCode" />
        <result property="description" column="description" />
        <result property="categoryCode" column="categoryCode" />
        <result property="category"
                column="categoryCode"
                select="General.getCategoryByCategoryCode" />
    </resultMap>

    <select id="getAllCountries" resultClass="Country">
        SELECT
        country_code as code, name, isd_code as isdCode
        FROM
        country_code
    </select>   

    <select id="getAllSubCategories" resultMap="SubCategoryResultMap">
        SELECT
        sub_category_code as subCategoryCode,
        sub_category_name as description,
        category_code as categoryCode
        FROM
        sub_category_code
    </select>  

    <select id="getCategoryByCategoryCode" parameterClass="string"
            resultMap="CategoryResultMap" >
        SELECT
        category as categoryCode,
        description
        FROM
        category_code
        WHERE
        category = #value#
    </select>

</sqlMap>

Above is the iBatis config for a SubCategory object that has a Category object as a parent.

public class SubCategoryDaoIbatisImpl extends AbstractIbatisDao implements SubCategoryDao{

    public SubCategoryDaoIbatisImpl(DaoManager daoManager){
        super(daoManager);
    }

    @Override
    public List<SubCategory> getAllSubCategories() throws SQLException {
        return getSqlMapExecutor().queryForList("General.getAllSubCategories", null);
    }
}

Above is the query made via SqlMapExecutor, but this doesn’t work with MS Access. When I migrated the database to MySQL, it worked fine. So, iBatis is a NO too for Ms Access.

My last resort was Hibernate with Spring. Below is my code for the same object above.

...

/**
* @author Taufek
*/
@Entity
@Table(name = "sub_category_code")
public class SubCategory extends AbstractEntity {

private Category category;
private String subCategoryCode;
private String description;

@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.EAGER)
@JoinColumn(name = "category_code")
public Category getCategory() {
return category;
}

public void setCategory(Category category) {
this.category = category;
}

@Column(name = "sub_category_name")
public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

@Id
@Column(name = "sub_category_code")
public String getSubCategoryCode() {
return subCategoryCode;
}

public void setSubCategoryCode(String subCategoryCode) {
this.subCategoryCode = subCategoryCode;
}
...
}

Above is the SubCategory object.

...
/**
* @author Taufek
*/
@Entity
@Table(name="category_code")
public class Category extends AbstractEntity{

private String categoryCode;
private String description;

@Id
@Column(name="category")
public String getCategoryCode() {
return categoryCode;
}

public void setCategoryCode(String code) {
this.categoryCode = code;
}

@Column(name="description")
public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
...
}

Above is Category object which is the parent of SubCategory.

public class SubCategoryDaoHibernateImpl extends AbstractHibernateDao implements SubCategoryDao{

    public List getAllSubCategories() throws SQLException {
        return getHibernateTemplate().loadAll(SubCategory.class);
    }

}

Above is the query. It is simpler with Hibernate. I only need to put the Hibernate annotations on each property in an entity to map it to its counterpart in the database. Then, by using HibernateTemplate from Spring, just use one of the methods provided to make a query.

I thought, I was done with choosing persistence method, but whenever there is an object which has more than 1 relationship, the query will return error. I found out that whenever there is a query with a relationship, hibernate will construct a query consists of LEFT OUTER JOIN to combine between 2 tables. I was surprised to find out that MS Access query can only has 1 LEFT OUTER JOIN. If you run a query with more than 1 LEFT OUTER JOIN, it will return an error.

So now,I’m back to square one with ol’ JDBC.

Most of JEE’s MVC frameworks nowadays, requires a bit of learning on their XML configurations. Some might get frustrated when seeing those long list of XMLs in a project. But for me, XML configuration is easier to maintain and learn if you know where to look. Now, Struts2 has taken the next step, and their goal is zero configuration. There are not actually there yet, but they have done tremendous job on reducing XML configs. But if you are a fan of XML like me, you can still stick to XML configs.

Struts2 took advantage of Annotation to move those configurations from XML to Action class itself. The only XML you need to set is your ol’ trusty web.xml. You need to set the heart of Struts2 component which is the FilterDispatcher like below.


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9"
         version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Dhydrated's Fortune Teller Web</display-name>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.FilterDispatcher
        </filter-class>
        <!-- Scanning for annotation packages -->
        <init-param>
<param-name>actionPackages</param-name>
<param-value>org.dhydrated</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

There is nothing special about above settings but just note that within FilterDispatcher element, I’m declaring actionPackages with org.dhydrated value. Struts2 will scan through this java packages for Action classes.

Below is my first Action class called Index.java.


package org.dhydrated.sample;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.config.Result;

/**
 * @author Dhydrated
 */
@Result( value="/pages/VisitorForm.jsp" )
public class Index extends ActionSupport{  

}

Above will be the first Action class to be hit when entering the application. From the index.jsp, I will just redirect by calling response.sendRedirect(“sample/index.action”);. Index action class will do nothing except redirecting to the VisitorForm.jsp. Below is my VisitorForm.jsp code.


<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
    <head>
        <title>Dhydrated's Fortune Teller</title>
    </head>
    <body>
<h4>Although I'm a fortune teller, please state your name</h4>
<s:form action="fortuneTeller">
            <s:textfield name="name" label="Your name"/>
            <s:submit/>
        </s:form>
    </body>
</html>

Below is the page being rendered.

On this page, I’m just trying to capture the user name and bind it to name property of FortuneTellerAction action class. When I submit this form, FortuneTellerAction action class will be executed. Below is my FortuneTellerAction action class.


package org.dhydrated.sample;

import org.apache.struts2.config.Result;

/**
 * @author Dhydrated
 */
@Result(name="SUCCESS", value="/pages/FortuneTeller.jsp" )
public class FortuneTellerAction {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFortune(){
        return "Howdy, " + name + ". Today's is your lucky day.";
    }

    public String execute() {
        return "SUCCESS";
    }
}

By now, above class already captured user’s data into name property. execute method will be called and redirect user to FortuneTeller.jsp page. This is set at the above code as @Result(name=”SUCCESS”, value=”/pages/FortuneTeller.jsp” ). FortuneTeller.jsp code is as below.


<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
    <head>
        <title>Dhydrated's Fortune Teller</title>
    </head>
    <body>
<h4><s:property value="fortune"/></h4>
</body>
</html>

Value fortune is referring to the getFortune() method from the FortuneTellerAction action class. The output of the page should be like below.


There are 2 points you should notice here. First, there are 2 ways of naming your Action class when using annotation. The Index action class extends Struts2 ActionSupport class, but the FortuneTellerAction action class does not. So, either you extends from ActionSupport class or end your action class with Action suffix, Struts2 will recognizes it as an Action class.

Second, if you recall, we set org.dhydrated as the annotation packages. My Index action class is under org.dhydrated.sample package. The value after org.dhydrated. will be treated as the URL namespace. So, the URL to call my Index action class is http://<host>:<port>/<servlet-contextpath>/sample/index.action. And of course, there will be .action suffix for every Action class.

Annotation really helps to slam down the number of XML config files. But when the project grew bigger, it could get complicated when you need to browse through all the Action classes to understand your project flow. For me, XML is still the best way to put all your Action declarations and project flows. Usually, XML files is group within a same folder so that it is easy to find. Some IDEs have great visual editor that works with XML to simplify the process in building the configs.

After working with JSF for quite sometimes, what should I learn next? Naturally, it would be Facelets. It is a templating language which is suitable for JSF. Although, Facelets was build with JSF in mind but it is said that, Facelets is flexible enough to be use with other UI language besides JSF.

So, what are needed for Facelets to work? Glad you asked. Below are the libraries needed for running JSF with Facelets. As you can see, MyFaces have made into my top choice. But don’t fret about it, just swap MyFaces libraries with Sun-RI if you prefer the latter.

<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/maven-v4_0_0.xsd">
    ...
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>com.sun.facelets</groupId>
            <artifactId>jsf-facelets</artifactId>
            <version>1.1.13</version>
        </dependency>
        <dependency>
            <groupId>myfaces</groupId>
            <artifactId>myfaces-api</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>myfaces</groupId>
            <artifactId>myfaces-impl</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.6</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>commons-digester</groupId>
            <artifactId>commons-digester</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>commons-el</groupId>
            <artifactId>commons-el</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

</project>

Then, I need to amend the web deployment descriptor to include the JSF servlet. Facelets has a standard file suffix which is ‘xhtml’. Hence, context-param named javax.faces.DEFAULT_SUFFIX and JSF Servlet’s URL Pattern are set to .xhtml.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
    </context-param>
    <context-param>
<param-name>facelets.SKIP_COMMENTS</param-name>
<param-value>true</param-value>
    </context-param>
    <context-param>
<param-name>com.sun.faces.verifyObjects</param-name>
<param-value>false</param-value>
    </context-param>
    <context-param>
<param-name>com.sun.faces.validateXml</param-name>
<param-value>true</param-value>
    </context-param>
    <context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
    </context-param>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

Next stop is the faces-config.xml file. Minimally, what I need to add is just the Facelets’ view handler as below.

<faces-config version="1.2"
              xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
    <application>
        <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
    </application>
</faces-config>

After configuring the XMLs, then I can start working on the UI. Ideally, we will start by creating a template for all our pages. Facelets has a strict requirement which the page must be a fully XHTML-compliant. Working with IDE sure helps a lot in weeding out those pesky errors. Below is a simple template named template.xhtml.

<!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:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">
    <head>
        <title>Dhydrated's Well</title>
    </head>
    <body>
        <h:form>
                <ui:insert name="body-content">
                    Body to be replaced.
                </ui:insert>
        </h:form>
    </body>
</html>

As you could have guessed, Facelets tag has ui prefix. Above, I’ve created a hook on the template named body-content by using the ui:insert tag, which could be replaced by my JSF view. Now, lets create a JSF view that uses above template.

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html">
    <body>
        <ui:composition template="template.xhtml">

            <ui:define name="body-content">
                <t:outputText value="Value from JSF View" />
            </ui:define>
        </ui:composition>
    </body>
</html>

Above JSF view, uses ui:composition tag to state which template it referring to. Then, it uses ui:define to position it content in the template by matching it’s name with template’s ui:insert tag’s name. So when you run this view on the browser, Facelets will help to decorate the page based on the choosen template.

What I had shown above is just one of the benefits from Facelets. There are other great goodies from Facelets which are component composition (which is my favourite), customizable tag libs, extendable components and etc. It also helps to nicely interweave JSTL and JSP expression language (EL) together. Although, there are others templating utilities such as Tiles, FreeMarker, Velocity and SiteMesh, Facelets does has a good edge when applied with JSF.

Starting a new project is time consuming. You need to determine the technologies involved, build the project structure, setup the configuration files, work out the dependencies of jar files and etc. Before I had encountered Maven, I just used to copy my existing project, and start working from there. I need to add/delete, Java, jar and other resources files prior to starting a new project. With Maven came into the picture, I need not to worry about most of the tasks I just mentioned.

There are several ways you could start you project with Maven. But I choose to use the generate goal from archetype plug-in because it seems to be the easiest way to start a new project. Archetype is like a project template, where Maven has a set of predefined project templates we could choose from. If we do not specify a specific archetype, it would goes into an interactive mode, which is awesome. I would hate it if I need to remember those archetype names. Below picture, is how I run the generate task command.

When you hit the [Enter] key, you would get the archetype list as below picture.

From the picture above you could see, there are 44 archetypes you could choose from. For a simple webapp, I should choose number 18, which represents maven-archetype-webapp archetype. Key in number 18 and hit [Enter] key.

Then, you will be asked to enter your project details as below.

Console will ask you to confirm the given details as below.

Finally, Maven will start working in the background and produce below output in the console.

If you are running for the first time, it will check all dependencies and download it if necessary. Downloaded dependencies are stored in your local repository. By default, if you are installing Maven in Windows XP, it will be at C:\Documents and Settings\<user>\.m path.

Now, Maven had created below project structure.

From here, you need to add more details to your pom.xml file to add more dependencies details in order for Maven to download the necessary jar files. By default, the pom.xml will be configured with JUnit the testing component.

Maven is a good utility to speed up and facilitate the building process of your application. It adopts standard approach so all projects has similar architecture. New developer, who just comes in to join a project, could easily get a good understanding of an application. Developer will requires less time on wading through specific configurations and project structure.

For those who are fan of Eclipse or NetBean IDE, there is a good news for you. Maven have plug-ins which enables it to be integrated with these IDE. Working with Maven through IDE, is much simpler than above mentioned process. :)

Being a conservative JSF developer, I’ve been resorting to Ajax4JSF (A4J) for my ajaxified applications. All I need to do is just jack-in A4J into my application and extend from my existing JSF tags. So easy, I don’t need to know the underlying implementation of Ajax. But I’ve been thinking, what if one day, I have to develop or maintain an application without JSF? A4J will not be there anymore to help me out. So, I decided to look for a new utility which could facilitates in making an Ajax call. There are myriads of Ajax helper libraries, but one that does caught my attention is DWR. Current stable version is 2.0 but version 3.0 is under heavy development. So, we could expect the latter version anytime soon.

Why I like it so much? – because it makes Ajax implementation simple for Java developer. It actually generates the Javascript objects for client side, which kind of mimicking the Java objects on the server side.

Here is a simple example of DWR. Below is my Java object, which is a normal POJO with a method getting a User object.
DWR Java Method

Next, I just need to declare my POJO in dwr.xml file.
dwr.xml file
The convert tag, is where I declare my User bean class, so DWR will able to marshall and unmarshall the bean between client and server side. The create tag, is where I declare my POJO, which will be mapped to the generated DWR’s Javascript object.

Now server side configurations are out of my way. Lets go to the client side. In my page, I need to includes below scripts.
DWR Javascript Import
First script tag, imports the common DWR engine script which is a mandatory script to be imported. Second script tag, imports DWR utility script. Utility is optional but it provides lots of handy functions. Third script tag, is the generated script which contains the Javascript object that maps to my UserService POJO.

Now, I can just call the method from Javascript like below and let DWR do all the dirty work.

DWR Javascript Functions
From the first function, you can see that I’m using the generated UserService object to make the Ajax call via getUser method. The way we call the method from a Javascript is a bit different compared to calling it from a Java class. Although, my method only accepts 1 argument in Java, but here, I have to put a callback function as the 2nd parameter which acts as the handler for the return object. DWR will handles all the heavy lifting on marshalling the request to the server and unmarshalling the response back to the Javascript object. The good thing is, not only you could pass and receive all the primitive and wrapper classes, but DWR also works on your POJO. As in this example, I’m expecting a User POJO as the return object. You don’t need to work on any XML or JSON format in marshalling and unmarshalling your data.

Although, comparing to A4J, there are more work to do here, especially on the client side, but it does give you more flexibility and control over your Ajax call. More control means you are able to fine tune your Ajax calls for performance optimization. Plus, DWR requires no specific framework. Hence, learning DWR, is beneficial because you could implement it regardless of your application architecture. It is also possible to patch DWR into your existing applications. DWR definitely will be in my list for my future projects.

Currently, I’m learning on how to incorporate iBatis into my application’s persistence layer. iBatis relies heavily on its xml files, which are used to map the SQLs to Java objects. If your project, contains a large number of tables, this could be a problem. You have to hand-code all the Java objects and SQLs manually. Being a self proclaimed Gung-Ho of open source technologies, I believe there must be something out there that could ease this burden. Well, there is!

There is a tool which was known as Abator, and was renamed to iBator. Wondering why changed the name? It was stated at the iBator web site, it was due to trade registration dispute. Well, changed name doesn’t affect the functionality at all. Functionally, iBator helps to generate the SQL Map Xmls and Java files based on the existing tables. Not only that, it could also generates DAO layer for you. It could tailor your DAO layer according to the Spring DAO standards. All you need to do after this, is tweak a bit on the generated xml files and connect your service layer to the DAO layer. How’s that for a speed in your project.

Basically, you will need one jar file, iBator jar. It doesn’t has any dependencies. But in my case, I would need ant jar file, since I’m using ant to execute the generator.

First, I would need the abatorConfig.xml file. Below is how I set the database connection settings:

Then, there are 3 items I’m setting below. First element, is the Java objects’ settings. Second element, is the SQLs xml files’ settings. And the third element, is the DAOs settings. Here you have to specify the target locations and the package/folder names for the generated files.
Generated Files Configs

Third, is my tables settings. Here I mapped the tables to my Java objects:
Tables Configs

Lastly, I just need to execute below ant file:
iBator Ant Build

That’s it. After running above ant build, you would have the almost complete DAO layer for your application. Some extra work might be needed such as configuring your tables relationship, setting your SQLMapConfig, and connecting your DAO layer to your service layer.

As with any other generator utility, it won’t gives you the perfect generated files. Some level of tweaking is expected from you. But to kick start a project with lots of tables, for sure you could appreciate the benefits of this utility.