JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse: Putting It All Together
JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse: Putting It All Together
JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse: Putting It All Together
The author has made every effort in the preparation of this book to ensure the accuracy of the information.
However, information in this book is sold without warranty either expressed or implied. The author will not be
held liable for any damages caused or alleged to be caused either directly or indirectly by this book.
by
K. Arulkumaran
&
A. Sivayini
Website: http://www.lulu.com/java-success
Table Of Contents
Notations ..................................................................................................................... 3
Tutorial 9 – JSF, Facelets, Spring, Hibernate & Maven 2 ............................... 4
Tutorial 10 – Maven 2 ............................................................................................. 18
3
Notations
Command prompt:
Eclipse:
Internet Explorer:
4
Tutorial 9 – JSF, Facelets, Spring, Hibernate & Maven 2
This tutorial will put together tutorials 1-8. It will also incorporate dependencies
between the simpleWeb (c:\tutorials\jsimpleWeb) and simple
(c:\tutorials\jsimple) projects as it would be in a real application.
Step 1:
Firstly create a dependency between simpleWeb and simple. In fact simpleWeb depends on simple
for business service functions and data access and storage. So we need to define this in our pom.xml
file under simpleWeb (c:\tutorials\jsimpleWeb).
<dependency>
<groupId>com.mytutorial</groupId>
<artifactId>simple</artifactId>
<version>1.0-SNAPSHOT</version>
<type>jar</type>
</dependency>
c:\tutorials\simpleWeb\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/maven-
v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mytutorial</groupId>
<artifactId>simpleWeb</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>simpleWeb Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.mytutorial</groupId>
<artifactId>simple</artifactId>
<version>1.0-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-digester</groupId>
<artifactId>commons-digester</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2</version>
</dependency>
5
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.0.6</version>
</dependency>
</dependencies>
<build>
<finalName>simpleWeb</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.4</version>
<configuration>
<downloadSources>false</downloadSources>
<wtpversion>1.5</wtpversion>
</configuration>
</plugin>
</plugins>
6
</pluginManagement>
</build>
<repositories>
<repository>
<id>maven-repository.dev.java.net</id>
<name>Java Dev Net Repository</name>
<url>http://download.java.net/maven/2/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
Step 2: Exit out of eclipse and run the following maven command in a command
prompt:
STEP: WorkAround
The JSF 1.2 requires eclipse web facet 2.5. You need to open the file
“org.eclipse.wst.common.project.facet.core.xml” under C:\tutorials\simpleWeb\.settings as shown
below from version=2.4 to version=2.5. Every time you use the eclipse:clean command, you will have
to manually fix this up as shown below.
Step 3: Get back into eclipse and refresh the simpleWeb project by highlighting it and pressing “F5”.
Now right click and select “Properties”.
7
Since simpleWeb depends on simple maven2 has transitively brought all it dependencies into the
simpleWeb’s build path. In order to build a war file inside eclipse (pom.xml file is useful for building
war file and deploying externally) you need to define the J2EE module dependencies inside eclipse by
right clicking on simpleWeb and then selecting “Properties” and then “J2EE module depndencies”.
We need most jar files except for the el-api-1.0.jar & servlet-api-2.3.jar. Make sure that these 2 jars
are not ticked because they are available under Tomcat’s lib directory. All the ticked jar files end up in
“/WEB-INF/lib” directory when packaged inside Eclipse.
When you package it outside eclipse using mvn command “mvn clean package” pom.xml file will be
used for packaging these dependency jar files under “/WEB-INF/lib” inside the war file
8
Step 4: Let’s make the following changes for this tutorial
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
courses.add(c1);
courses.add(c2);
service.processCourse(courses);
}
}
import java.util.List;
import java.util.List;
//Modified
public void processCourse(List<Course> courses) {
// CourseDao dao = new CourseDaoImpl();
courseDao.create(courses);
}
//Added
public List<Course> getCourses() {
List<Course> list = getCourseDao().findAll();
return list;
}
}
import java.util.List;
<ui:composition>
<html>
<head>
<title>greeting page</title>
</head>
<body>
<f:loadBundle basename="com.mytutorial.messages" var="msg" />
<h3><h:outputText value="#{msg.greeting_text}" />, <h:outputText
value="#{personBean.personName}" /> <h:outputText
value="#{msg.sign}" /></h3>
<h:column>
<f:facet name="header">
<h:outputText value="Course" />
</f:facet>
<h:outputText value="#{course.course}" />
</h:column>
</h:dataTable>
</body>
</html>
</ui:composition>
</jsp:root>
11
</beans>
12
<param-value>
<![CDATA[
/WEB-INF/applicationContext.xml
classpath:applicationContext-mytutorial.xml
]]>
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
<![CDATA[
/WEB-INF/applicationContext.xml
classpath:applicationContext-mytutorial.xml
]]>
</param-value>
</context-param>
13
<!-- Special Debug Output for Development -->
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>true</param-value>
</context-param>
<!-- Optional JSF-RI Parameters to Help Debug -->
<context-param>
<param-name>com.sun.faces.validateXml</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.verifyObjects</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
</web-app>
Step 5: Now install the “simple” project into maven repository with the new changes by:
http://localhost:8080/simpleWeb/index.jsf
16
The Name & Course are empty. Now let’s add some name and courses into the database either via
SQL statements in “DatabaseManager” or by running the App.java. Make sure that your HSQLDB
database server is running. To run App.java inside eclipse right click on “App.java” and then select
“Run As” and then select “Java Application”. After it has finished running go back to your internet
explorer and click on refresh button.
17
Please feel free to email any errors to java-interview@hotmail.com. Also stay tuned at
http://www.lulu.com/java-success for more tutorials and Java/J2EE interview
resources.
18
Tutorial 10 – Maven 2
You may have noticed that there are some code repeats in pom.xml files in
simple and simpleWeb projects. Maven2 child pom.xml files can inherit from
parent pom.xml file. In this tutorial let’s clean up our project structures and the
pom.xml files.
Step 1: Exit out of eclipse and restructure your projects under C:\tuitorials as follows:
Create a new folder “simple-tutorial” under “c:\tutorials” and move the “simple” & “simpleWeb”
under “simple-tutorial”.
<modelVersion>4.0.0</modelVersion>
<groupId>com.mytutorial</groupId>
<artifactId>simple-tutorial</artifactId>
<packaging>pom</packaging>
<name>simple tutorials</name>
<description>simple tutorials</description>
<version>1.0</version>
<url>http://www.lulu.com/java-success</url>
<inceptionYear>2007</inceptionYear>
<developers>
<developer>
<name>Arulkumaran</name>
<id>ak1</id>
<email>java-interview@hotmail.com</email>
<organization></organization>
19
<roles>
<role>Senior Java Designer/Developer</role>
</roles>
<timezone>+10</timezone>
</developer>
</developers>
<contributors>
<contributor>
<name>Sivayini</name>
<email></email>
<organization></organization>
<roles>
<role>Java Developer</role>
</roles>
</contributor>
</contributors>
<modules>
<module>simple</module>
<module>simpleWeb</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.4</version>
<configuration>
<downloadSources>false</downloadSources>
<wtpversion>1.5</wtpversion>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
<repository>
<id>maven-repository.dev.java.net</id>
<name>Java Dev Net Repository</name>
<url>http://download.java.net/maven/2/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
20
<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>
<parent>
<groupId>com.mytutorial</groupId>
<artifactId>simple-tutorial</artifactId>
<version>1.0</version>
</parent>
<groupId>com.mytutorial</groupId>
<artifactId>simple</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.4.ga</version>
</dependency>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.0.6</version>
</dependency>
</dependencies>
</project>
As you can see above this pom.xml file extends the parent pom.xml. All the repositories and plugins
are shared from the parent pom.xml file.
<groupId>com.mytutorial</groupId>
<artifactId>simpleWeb</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>simpleWeb Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.mytutorial</groupId>
<artifactId>simple</artifactId>
<version>1.0-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-digester</groupId>
<artifactId>commons-digester</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.0.6</version>
</dependency>
</dependencies>
<build>
<finalName>simpleWeb</finalName>
<pluginManagement/>
</build>
</project>
Step 3: Run mvn commands. This time you can run from “C:\tutorials\simple-tutorial” to build both
“simple” & “simpleWeb”. This is possible due to <modules> definition in parent pom.xml file.
<modules>
<module>simple</module>
<module>simpleWeb</module>
</modules>
The JSF 1.2 requires eclipse web facet 2.5. You need to open the file
“org.eclipse.wst.common.project.facet.core.xml” under C:\tutorials\simple-
tutorial\simpleWeb\.settings as shown below from version=2.4 to version=2.5. Every time you use
the eclipse:clean command, you will have to manually fix this up as shown below.
Delete the “simple” & “simpleWeb” (select “Do not delete contents” and pres OK ) projects and
import them again.
25
26
Step 6: Run the HSQLDB server in a command prompt and also open the “DatabaseManager” in
another command prompt.
http://localhost:8080/simpleWeb/index.jsf
That’s all to it. Also try deploying outside Tomcat. Stop the Tomcat server inside eclipse.
29
If you already have a “simpleWeb” folder under “C:\java\Tomcat6.0\webapps” delete it first. Then
copy the “simpleWeb.war” into Tomcat’s “C:\java\Tomcat6.0\webapps” folder. Double click on
“tomcat6.exe” to start the server.
http://localhost:8080/simpleWeb/index.jsf
You can find some fundamental Questions & Answers relating to Hibernate/Spring under
“Emerging technologies/framework” section in Java/J2EE Job Interview Companion at
http://www.lulu.com/content/192463
Please feel free to email any errors to java-interview@hotmail.com. Also stay tuned at
http://www.lulu.com/java-success for more tutorials and Java/J2EE interview
resources.