Professional Documents
Culture Documents
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 7 – JSF, Facelets, Maven & Eclipse...................................................... 4
Tutorial 8 – JSF, Facelets, Spring, Maven & Eclipse..................................... 19
3
Notations
Command prompt:
Eclipse:
Internet Explorer:
4
Tutorial 7 – JSF, Facelets, Maven & Eclipse
This tutorial will guide you through re-building tutorial-3 for simpleWeb with
facelets. It assumes that you have read tutorials 1-3.
Artifact el-ri (Expression Language – reference implementation) can be downloaded from the
following repository and all the other jars except jsf-impl (Sun JSF RI ) should be available from the
maven-2 default repository http://repo1.maven.org/maven2/.
<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>
5
Step1: Download JSF 1.2_04 P02 from the site https://javaserverfaces.dev.java.net/download.html
into say your download directory. Create a new folder maven_lib under your c:\java folder for the
special library files which you can’t download from any maven repositories. Copy the jsf-impl.jar to
your “c:\java\maven_lib” folder and rename it to “jsf-impl-1.2_04.jar”.
Now you need to install this JSF implementation jar into your local maven repository in
c:\java\.m2\repository. To do this run the following command in a command prompt:
After running the above command, you can check for the presence of the jsf-impl-1.2_04.jar file in
your local maven 2 repository “c:\java\.m2\repository\javax”.
6
Now the revised pom.xml file under c:\tutorials\simpleWeb should look like:
<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>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>
7
<!-- 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> Java compiler
<groupId>org.apache.maven.plugins</groupId> JDK 1.5
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin> Using WTP 2.4 but
<groupId>org.apache.maven.plugins</groupId> JSF 1.2 requires 2.5,
<artifactId>maven-eclipse-plugin</artifactId> which is not yet
<version>2.4</version> available. So refer
<configuration> Step: WorkAround
<downloadSources>false</downloadSources> to manually change
<wtpversion>1.5</wtpversion> this value.
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
<repository>
8
<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> By default maven uses
<enabled>false</enabled> http://repo1.maven.org/maven2/
</snapshots> and your local repository in
</repository> c:\java\.m2\repository. Any other
</repositories> repositories need to be defined in
the pom.xml file. el-i-1.0.jar can
</project> be found at this repository.
Step-2: If you are already inside eclipse, exit out of it and run the following maven command from
c:\tutorials\simpleWeb to generate eclipse build path (i.e. classpath).
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: Now get back into eclipse and click “F5” for refresh on the “simpleWeb” project. The
required files need to be completed as shown below:
9
PersonBean.java (Model)
package com.mytutorial;
PersonBeanController.java (Controller)
package com.mytutorial;
inputname_header=JSF Tutorial
prompt=Tell me your name:
greeting_text=Welcome to JSF
button_text=Hello
sign=!
greeting.jspx (page)
Note: The recommended extension for pages by facelets is the “.xhtml”. Since eclipse does not
recognize this and would not provide you code assist when you press ctrl-Space. So the work around is
to use the extension .jspx to solve this problem. These .jspx files can be opened using the webpage
editor provided by eclipse 3.3 WTP. To open the greeting.jspx in a webpage editor right click on it and
select “other” and then “WebPage Editor”
11
<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> Defined in faces-
config.xml
</body>
</html>
</ui:composition>
</jsp:root>
12
inputname.jspx
<ui:composition>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<f:view>
<f:loadBundle basename="com.mytutorial.messages" var="msg" />
<h3>
<h:form id="helloForm">
<h:outputText value="#{msg.prompt}" />
<h:inputText value="#{personBean.personName}" />
faces-config.xml
<faces-config>
<navigation-rule>
<from-view-id>/pages/inputname.jspx</from-view-id>
<navigation-case>
<from-outcome>greeting</from-outcome>
<to-view-id>/pages/greeting.jspx</to-view-id>
</navigation-case>
</navigation-rule>
Used in JSF pages
<managed-bean>
<managed-bean-name>personBean</managed-bean-name>
<managed-bean-class>
com.mytutorial.PersonControllerBean
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<application>
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
</application>
</faces-config>
14
web.xml
<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>
<ui:composition>
<html>
<body>
<f:view>
<a href="pages/inputname.jsf">Click Me</a>
<br />
</f:view>
</body>
16
</html>
</ui:composition>
</jsp:root>
You can now try to build it and deploy it to tomcat as discussed in tutorial-3. Try deploying it from
both inside eclipse & outside. You can check your deployed war file from inside eclipse under
following folder (check if it is properly packaged):
Important!!
17
It is important to note that, if you are deploying to Tomcat inside eclipse, remember to exclude the jar
files which are already available under Tomcat’s lib directory. So you need to remove the el-api-
1.0.jar file from getting packaged. You could do this inside eclipse by right clicking on simpleWeb
and then selecting “properties”. Under J2EE module dependencies make sure that el-api-1.0.jar is
unticked. Only the ticked files make it to the WEB-IN\lib folder. If you build the war file outside
eclipse then maven pom.xml file will take care of this, since its scope is declared as “provided”.
The URL to use after you deploy/publish and start the Tomcat server:
http://localhost:8080/simpleWeb/index.jsf
18
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.
19
Tutorial 8 – JSF, Facelets, Spring, Maven & Eclipse
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.0.6</version>
</dependency>
After adding, save it and exit out of eclipse and run the following mvn (Maven) command
from the command line.
mvn eclipse:eclipse
20
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 show
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.
You can now open eclipse and refresh (i.e. F5) simpleWeb project. After this if you check
your eclipse build path, it should look like below with spring-2.0.6.jar.
21
Also check your project facet to make sure that dynamic web module is 2.5. If not repeat step marked
“Work Around” above after exiting eclipse.
To use Spring make the following changes to the existing artifacts and also add the
“applicationContext.xml” file under WEB-INF.
PersonControllerBean.java
package com.mytutorial;
</beans>
Now make the required changes to the descriptor files web.xml & faces-config.xml.
faces.-config.xml
Remove the <managed-bean> declaration and add the <variable-resolver> under <application>.
<faces-config>
<navigation-rule>
<from-view-id>/pages/inputname.jspx</from-view-id>
<navigation-case>
<from-outcome>greeting</from-outcome>
<to-view-id>/pages/greeting.jspx</to-view-id>
</navigation-case>
</navigation-rule>
-->
Added
<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
</application>
</faces-config>
web.xml:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Added
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
The URL to use after you deploy/publish and start the Tomcat server:
http://localhost:8080/simpleWeb/index.jsf
Run the application as before and you should see the same output.
This time with Spring’s dependency injection.
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.