In this topic, we’re going to talk an interesting subject: How to create a project using Maven, JSF 1.2 RI, Richfaces, Tomahawk and Hibernate all together. Maybe in the first time, it’ll take long time to download the required JARs files, but believe me, after that you’ll be able to create a new project in less than 5 minutes.
Before get started, if you have no idea what these frameworks do, please, take a look at their official sites and make sure you understand their functionalities.
Maven 2
First of all, we need to install Maven 2. It will be the responsible for creating the project, as well as manage the JARs files. You can download Maven 2 from its official site. Basically you should unzip the downloaded file and create a M2_HOME environment variable. All the needed information are here.
Creating the project
Now we’ve got Maven installed and running, it’s time to create the project. We’re going to use an archetype for web projects, maven-archetype-webapp. To create the project, simply type the following:
mvn archetype:create -DgroupId=com.jairrillo -DartifactId=TestMavenAndOtherStuffs -DarchetypeArtifactId=maven-archetype-webapp
A new project will be created with the name TestMavenAndOtherStuffs. The project’s structure looks like below:
TestMavenAndOtherStuffs
|-- pom.xml
`-- src
`-- main
|`-- resources
|`--webapp
|`-- WEB-INF
`--web.xml
|`-- META-INF
`--MANIFEST.MF
There’s an issue with this archetype, it doesn’t create the src/main/java and src/test/java folders. We’re going to use both, so, let’s create them.
Note: If you type mvn package, Maven will package the project in a war file within the target directory. mvn clean cleans up the target directory. Fell free to run both.
POM.xml
As you should know, the pom.xml file is the core of maven. In this file, we’re going to set which frameworks/jars files we’re going to use. Maven reads this file and downloads the required JARs files.
As our project uses a lot of frameworks, there’re many entries in the POM.xml file. Hence, you can download the full POM.xml here.
WEB.xml
The web.xml must be modified. We need to put some configuration there. As we’re using many frameworks, there are many changes in the web.xml. Changing the web.xml file is boring, but I’ve already prepared it for you. Simply click here and download the proper file.
applicationContext.xml & faces-config.xml
As you should know, Spring uses an applicationContext.xml file and JSF uses a faces-config.xml file. Let’s create them.
applicationContext.xml
We’re going to use annotations to map our Service, hence, our applicationContext.xml is very simple. You must inform to spring where it will lookup for the service classes. In our example, the package is com.jairrillo. The applicationContext.xml looks like below:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.jairrillo" annotation-config="true" />
<context:annotation-config />
</beans>
faces-config.xml
The faces-config.xml for this example is simple too. We’ll only set some parameters from Spring and Facelets.
Note: In our application, Spring will control the dependency injection. Hence, we will not map the managed beans in the faces-config.xml, instead, we’re going to use annotations from Spring, tough.
<?xml version="1.0"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude" 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>
<!-- Spring Configuration -->
<variable-resolver>org.springframework.web.jsf.SpringBeanVariableResolver</variable-resolver>
<!-- Facelets configuration -->
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
</application>
</faces-config>
It’s done. Our application has been created and it is ready to be used. In the next time, it will not take more than 5 minutes.
Below, I’ll show up an example to run in this new project.
Creating the Spring’s Service
Now we’ve finished all configurations, it is time to code. Let’s get started by the Service. In our application, the Service is gonna be a simple class that receives a String and transform it to UpperCase. The code (interface and concrete class) look like below:
HelloWorldService.java
package com.jairrillo.service;
public interface HelloWorldService {
public String convertToUpperCase(String word);
}
HelloWorldServiceImpl.java
package com.jairrillo.service;
import org.springframework.stereotype.Service;
@Service
public class HelloWorldServiceImpl implements HelloWorldService {
@Override
public String convertToUpperCase(String word) {
return word.toUpperCase();
}
}
Note: Check the annotation @Service out. It is similar than the mapping bean from applicationContext.xml, but much easier to use.
Creating the Managed Bean
The Managed Bean looks like below:
package com.jairrillo.mbeans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.jairrillo.service.HelloWorldService;
@Component("helloWorld")
@Qualifier("helloWorld")
@Scope("request")
public class HelloWorldBean {
private String name;
@Autowired
private HelloWorldService helloWorldService;
public String convertNameToUpperCase() {
name = helloWorldService.convertToUpperCase(name);
return "success";
}
//getters and setters
}
Again, check the annotations out. The @Component is similar than the managed-bean from faces-config.xml. Another important annotation is the @Autowired, it injects the Service into the ManagedBean. Did you see how simple is JSF + Spring?
Creating the Unit Testing
Before move to the view, let’s create a simple Unit Testing in the managed bean. Actually, in a real application, we should create am unit test for the Service and a different one for the ManagedBean. But to let our application simpler, let’s create only one that cover both classes.
package com.jairrillo.mbeans;
import static junit.framework.Assert.*;
import org.junit.Before;
import org.junit.Test;
import com.jairrillo.service.HelloWorldService;
import com.jairrillo.service.HelloWorldServiceImpl;
public class TestHelloWorldBean {
private HelloWorldBean helloWorldBean;
private HelloWorldService helloWorldService;
@Before
public void setUp() throws Exception {
this.helloWorldService = new HelloWorldServiceImpl();
this.helloWorldBean = new HelloWorldBean();
this.helloWorldBean.setHelloWorldService(helloWorldService);
}
/**
* Test method convertNameToUpperCase() when all result are TRUE
*/
@Test
public void testConvertNameToUpperCaseSuccessfully() {
//jair
helloWorldBean.setName("jair");
helloWorldBean.convertNameToUpperCase();
assertTrue(helloWorldBean.getName().equals("JAIR"));
//molly
helloWorldBean.setName("molly");
helloWorldBean.convertNameToUpperCase();
assertTrue(helloWorldBean.getName().equals("MOLLY"));
//jully
helloWorldBean.setName("jully");
helloWorldBean.convertNameToUpperCase();
assertTrue(helloWorldBean.getName().equals("JULLY"));
}
/**
* Test method convertNameToUpperCase() when all result are FALSE
*/
@Test
public void testConvertNameToUpperCaseError() {
//jair
helloWorldBean.setName("jair");
helloWorldBean.convertNameToUpperCase();
assertFalse(helloWorldBean.getName().equals("jair"));
//molly
helloWorldBean.setName("molly");
helloWorldBean.convertNameToUpperCase();
assertFalse(helloWorldBean.getName().equals("molly"));
//jully
helloWorldBean.setName("jully");
helloWorldBean.convertNameToUpperCase();
assertFalse(helloWorldBean.getName().equals("jully"));
}
}
Note: This class must be created into the src/test/main folder previously created.
Now we have the classes created, let’s run the JUnit. To do that, in the console, type
If everything is fine, you should get a BUILD SUCCESSFULLY.
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.jairrillo.mbeans.TestHelloWorldBean
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.047 sec
Results :
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
Creating the View
We’re going to create 3 files: template.jspx, helloWorld.jspx and index.jsp. I’ll not detail all files, but keep in mind that we’re using the facelets.
template.jspx
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Test Maven and Other Stuffs</title>
<style type="text/css">
</style>
</head>
<body>
<h1><ui:insert name="title">Default Title</ui:insert></h1>
<p><ui:insert name="body" /></p>
</body>
</html>
helloWorld.jspx
<!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"
xmlns:t="http://myfaces.apache.org/tomahawk"
xmlns:rich="http://richfaces.org/rich"
xmlns:a4j="http://richfaces.org/a4j">
<body>
<ui:composition template="/template.jspx">
<ui:define name="body">
<h:form>
<rich:panel header="Richfaces Panel Example" style="width: 315px">
<t:outputText value="Type name: " />
<t:inputText value="#{helloWorld.name}" />
<a4j:commandButton value="Convert Name" reRender="nameConverted" action="#{helloWorld.convertNameToUpperCase}" />
</rich:panel>
</h:form>
<h:panelGroup id="nameConverted">
<h:outputText value="Hello, " rendered="#{not empty helloWorld.name}" />
<h:outputText value="#{helloWorld.name}" />
</h:panelGroup>
</ui:define>
</ui:composition>
</body>
</html>
As you can see above, we tried to use one component from Richfaces, Ajax4JSF, Tomahawk and JSF RI 1.2.
index.jsp
<html>
<body>
<% response.sendRedirect("helloWorld.jsf"); %>
</body>
</html>
All done.
Running the project
To deploy an application, you should first package it. With maven it is very simple, simply type mvn clean package and a war file will be created into target directory.
After that, deploy that file in your Application Server. In my case, I’m using Tomcat.
When you run the project above onto browser, you’ll get a screen like this:

IDE Integration
Even after you created a project through Maven, you can integrate it with other IDE, such as Eclipse, Netbeans and IntelliJ IDEA.
With Eclipse, simple type mvn eclipse:eclipse and then you can import the project within eclipse workspace. The same is for IntelliJ IDEA, type mvn ide:ide.
Netbeans is a little different. Firstly, you must install the Maven Plugin (within Tools | Plugins) and then
just go to File | Open Project and select the directory with the pom.xml file and we are ready to go.
Conclusion
In this article we showed how to create a project using Maven + JSF + RichFaces + Facelets + Tomahawk + Hibernate. Also, we showed a simple example.
Usually, when you try to integrate many frameworks, you’ve got many incompatibility problems, however, this topic shows you how to integrate these frameworks without headache.
I hope this topic be useful for anyone (beside me, of course). If you have any question or comment, fell free to leave your message below.