Vous êtes sur la page 1sur 29

1

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.

JSF, Facelets, Spring, Hibernate, Maven2 &


Eclipse

Putting it All together

by

K. Arulkumaran

&

A. Sivayini

Website: http://www.lulu.com/java-success

Feedback email: java-interview@hotmail.com


2

Table Of Contents

Notations ..................................................................................................................... 3
Tutorial 9 – JSF, Facelets, Spring, Hibernate & Maven 2 ............................... 4
Tutorial 10 – Maven 2 ............................................................................................. 18
3
Notations

Command prompt:

Eclipse:

File Explorer or Windows Explorer:

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>

<!-- JSF/JSTL/Facelets -->


<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>1.2_04</version>
</dependency>
<dependency>
<groupId>com.sun.facelets</groupId>
<artifactId>jsf-facelets</artifactId>
<version>1.1.11</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.el</groupId>
<artifactId>el-ri</artifactId>
<version>1.0</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:

C:\tutorials\simpleWeb>mvn eclipse:clean eclipse:eclipse

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

App.java in simple project


package com.mytutorial;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

public static void main(String[] args) {

ApplicationContext ctx = new ClassPathXmlApplicationContext( "applicationContext-


mytutorial.xml");

List<Course> courses = new ArrayList<Course>(10);

Course c1 = new Course();


c1.setName("John");
c1.setCourse("Java");

courses.add(c1);

Course c2 = new Course();


c2.setName("Peter");
c2.setCourse("Hibernate");

courses.add(c2);

//CourseService service = new CourseServiceImpl();


CourseService service = (CourseService) ctx.getBean("courseService");

service.processCourse(courses);

List<Course> listCourses = service.getCourses(); //added

System.out.println("Retrieved courses and names are " + listCourses); //added

}
}

CourseService.java in simple project


package com.mytutorial;

import java.util.List;

public interface CourseService {

public abstract void processCourse(List<Course> courses);

public abstract List<Course> getCourses(); //added


}
9
CourseServiceImpl.java in simple project
package com.mytutorial;

import java.util.List;

public class CourseServiceImpl implements CourseService {

private CourseDao courseDao;

public CourseDao getCourseDao() {


return courseDao;
}

public void setCourseDao(CourseDao courseDao) {


this.courseDao = courseDao;
}

//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;
}
}

CourseControllerBean.java in simpleWeb project


package com.mytutorial;

import java.util.List;

public class CourseControllerBean {

private CourseService courseService; // injected via spring

public List<Course> getCourses(){


return getCourseService().getCourses();
}

public CourseService getCourseService() {


return courseService;
}

public void setCourseService(CourseService courseService) {


this.courseService = courseService;
}
}

greeting.jspx in simpleWeb project


<?xml version="1.0" encoding="ISO-8859-1" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets" version="2.0">
10

<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:dataTable id="courses" value="#{courseBean.courses}" var="course"


border="1">
<h:column>
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{course.name}" />
</h:column>

<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

applicationContext.xml in simpleWeb project


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<bean id="person" class="com.mytutorial.PersonBean" />

<bean id="personBean" class="com.mytutorial.PersonControllerBean" scope="session">


<property name="person" ref="person" />
</bean>

<bean id="courseBean" class="com.mytutorial.CourseControllerBean" scope="session">


<property name="courseService" ref="courseService" />
</bean>

</beans>
12

finally the web.xml file.

web.xml in simpleWeb project: find both spring applicationContext files.


<context-param>
<param-name>contextConfigLocation</param-name>

<param-value>
<![CDATA[
/WEB-INF/applicationContext.xml
classpath:applicationContext-mytutorial.xml
]]>
</param-value>
</context-param>

The web.xml file should look like:

<!DOCTYPE web-app PUBLIC


"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.jspx</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>

<!-- Faces Servlet -->


<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- Faces Servlet Mapping -->


<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>

</web-app>

Step 5: Now install the “simple” project into maven repository with the new changes by:

C:\tutorials\simple>mvn clean install

This should install the “simple-1.0-SNAPSHOT.jar” under


“C:\java\.m2\repository\com\mytutorial\simple\1.0-SNAPSHOT”. Refresh the simpleWeb project inside
eclipse. Now Republish/package the simpleWeb project inside eclipse and start the Tomcat Server.
14

Make sure that your Database server is running:

C:\java\hsqldb>java -cp ./lib/hsqldb.jar org.hsqldb.Server


15

Also preferably run the “DatabaseManeger”

C:\java\hsqldb>java -cp ./lib/hsqldb.jar org.hsqldb.util.DatabaseManager

This will spawn a new window as shown:

Now open internet browser and type the following URL.

http://localhost:8080/simpleWeb/index.jsf
16

Click on link “Click Me”.

Click on button “Hello”

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

That’s all to it.

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”.

Step 2: Add the parent pom.xml under “c:\tutorials\simple-tutorial” as shown below.


The parent pom.xml file will be shared by both the pom.xml files under
“c:\tutorials\simple-tutorial\simple” and “c:\tutorials\simple-tutorial\simpleWeb”.

c:\tutorials\simple-tutorial\pom.xml (parent 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>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

c:\tutorials\simple-tutorial\simple\pom.xml (child 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>
<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.

c:\tutorials\simple-tutorial\simpleWeb\pom.xml (child 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>
<parent>
<groupId>com.mytutorial</groupId>
21
<artifactId>simple-tutorial</artifactId>
<version>1.0</version>
</parent>

<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>

<!-- JSF/JSTL/Facelets -->


<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>1.2_04</version>
</dependency>
<dependency>
<groupId>com.sun.facelets</groupId>
<artifactId>jsf-facelets</artifactId>
<version>1.1.11</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
22
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.el</groupId>
<artifactId>el-ri</artifactId>
<version>1.0</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>

Now run the following command for eclipse:

C:\tutorials\simple-tutorial>mvn eclipse:clean eclipse:eclipse


23
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\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.

Run the following command:

C:\tutorials\simple-tutorial>mvn clean install

Step 4: Open up your eclipse workspace at “C:\java\eclipse-tutorial-workspace”. You will get a


screen as follows:
24

Delete the “simple” & “simpleWeb” (select “Do not delete contents” and pres OK ) projects and
import them again.
25
26

Step 5: Publish the war file inside eclipse.


27

After publishing, run the Tomcat Server.

Step 6: Run the HSQLDB server in a command prompt and also open the “DatabaseManager” in
another command prompt.

Make sure that your Database server is running:

C:\java\hsqldb>java -cp ./lib/hsqldb.jar org.hsqldb.Server

Also preferably run the “DatabaseManeger”

C:\java\hsqldb>java -cp ./lib/hsqldb.jar org.hsqldb.util.DatabaseManager

This will spawn a new window as shown:


28

Open an internet browser and type the URL

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.

Open an internet browser and type the URL

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.

Vous aimerez peut-être aussi