SlideShare a Scribd company logo
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:tutorialsjsimpleWeb) and simple
(c:tutorialsjsimple) 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:tutorialsjsimpleWeb).


    <dependency>
        <groupId>com.mytutorial</groupId>
        <artifactId>simple</artifactId>
        <version>1.0-SNAPSHOT</version>
        <type>jar</type>
    </dependency>


c:tutorialssimpleWebpom.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:tutorialssimpleWeb>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:tutorialssimpleWeb.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:tutorialssimple>mvn clean install

This should install the “simple-1.0-SNAPSHOT.jar” under
“C:java.m2repositorycommytutorialsimple1.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:javahsqldb>java -cp ./lib/hsqldb.jar org.hsqldb.Server
15




Also preferably run the “DatabaseManeger”


C:javahsqldb>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:tutorialssimple-tutorial” as shown below.
The parent pom.xml file will be shared by both the pom.xml files under
“c:tutorialssimple-tutorialsimple” and “c:tutorialssimple-tutorialsimpleWeb”.


c:tutorialssimple-tutorialpom.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:tutorialssimple-tutorialsimplepom.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:tutorialssimple-tutorialsimpleWebpom.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:tutorialssimple-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:tutorialssimple-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:tutorialssimple-
tutorialsimpleWeb.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:tutorialssimple-tutorial>mvn clean install




Step 4: Open up your eclipse workspace at “C:javaeclipse-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:javahsqldb>java -cp ./lib/hsqldb.jar org.hsqldb.Server




Also preferably run the “DatabaseManeger”

C:javahsqldb>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:javaTomcat6.0webapps” delete it first. Then
copy the “simpleWeb.war” into Tomcat’s “C:javaTomcat6.0webapps” 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.

More Related Content

What's hot (20)

KEY
Tycho - good, bad or ugly ?
Max Andersen
 
PDF
JavaDo#09 Spring boot入門ハンズオン
haruki ueno
 
KEY
How to be effective with JBoss Developer Studio
Max Andersen
 
KEY
Enterprise Build And Test In The Cloud
Carlos Sanchez
 
PDF
Java EE 6 = Less Code + More Power
Arun Gupta
 
PDF
Java EE 6 and GlassFish v3: Paving the path for future
Arun Gupta
 
PDF
Hibernate, Spring, Eclipse, HSQL Database & Maven tutorial
Raghavan Mohan
 
PDF
Deep Dive Hands-on in Java EE 6 - Oredev 2010
Arun Gupta
 
PDF
Spring boot入門ハンズオン第二回
haruki ueno
 
PDF
Java EE 6 & GlassFish = Less Code + More Power at CEJUG
Arun Gupta
 
PDF
Java EE 6 - Deep Dive - Indic Threads, Pune - 2010
Jagadish Prasath
 
ODP
Java EE 6 = Less Code + More Power (Tutorial) [5th IndicThreads Conference O...
IndicThreads
 
PPTX
Apache Maven for AT/QC
Volodymyr Ostapiv
 
PPT
Maven, Eclipse And OSGi Working Together
Carlos Sanchez
 
PDF
Enterprise Maven Repository BOF
Max Andersen
 
KEY
Enterprise Build And Test In The Cloud
Carlos Sanchez
 
PDF
ICEfaces EE - Enterprise-ready JSF Ajax Framework
ICEsoftTech
 
PPTX
Simple module Development in Joomla! 2.5
Vishwash Gaur
 
PPTX
Spring boot for buidling microservices
Nilanjan Roy
 
PDF
Maven, Eclipse and OSGi Working Together - Carlos Sanchez
mfrancis
 
Tycho - good, bad or ugly ?
Max Andersen
 
JavaDo#09 Spring boot入門ハンズオン
haruki ueno
 
How to be effective with JBoss Developer Studio
Max Andersen
 
Enterprise Build And Test In The Cloud
Carlos Sanchez
 
Java EE 6 = Less Code + More Power
Arun Gupta
 
Java EE 6 and GlassFish v3: Paving the path for future
Arun Gupta
 
Hibernate, Spring, Eclipse, HSQL Database & Maven tutorial
Raghavan Mohan
 
Deep Dive Hands-on in Java EE 6 - Oredev 2010
Arun Gupta
 
Spring boot入門ハンズオン第二回
haruki ueno
 
Java EE 6 & GlassFish = Less Code + More Power at CEJUG
Arun Gupta
 
Java EE 6 - Deep Dive - Indic Threads, Pune - 2010
Jagadish Prasath
 
Java EE 6 = Less Code + More Power (Tutorial) [5th IndicThreads Conference O...
IndicThreads
 
Apache Maven for AT/QC
Volodymyr Ostapiv
 
Maven, Eclipse And OSGi Working Together
Carlos Sanchez
 
Enterprise Maven Repository BOF
Max Andersen
 
Enterprise Build And Test In The Cloud
Carlos Sanchez
 
ICEfaces EE - Enterprise-ready JSF Ajax Framework
ICEsoftTech
 
Simple module Development in Joomla! 2.5
Vishwash Gaur
 
Spring boot for buidling microservices
Nilanjan Roy
 
Maven, Eclipse and OSGi Working Together - Carlos Sanchez
mfrancis
 

Similar to Jsf, facelets, spring, hibernate, maven2 (20)

PDF
Spring Web Service, Spring JMS, Eclipse & Maven tutorials
Raghavan Mohan
 
PDF
Hands On with Maven
Sid Anand
 
KEY
企业级软件的组件化和动态化开发实践
Jacky Chi
 
PDF
Apache Maven - eXo TN presentation
Arnaud Héritier
 
PDF
Boston 2011 OTN Developer Days - Java EE 6
Arun Gupta
 
PDF
Riviera JUG (20th April, 2010) - Maven
Arnaud Héritier
 
PDF
Lausanne Jug (08th April, 2010) - Maven
Arnaud Héritier
 
PDF
Lorraine JUG (1st June, 2010) - Maven
Arnaud Héritier
 
PDF
Spring Lab
Leo Nguyen
 
PDF
Java EE 6 & GlassFish = Less Code + More Power @ DevIgnition
Arun Gupta
 
PDF
Spark IT 2011 - Java EE 6 Workshop
Arun Gupta
 
PDF
Arun Gupta: London Java Community: Java EE 6 and GlassFish 3
Skills Matter
 
PDF
Java EE 6 & GlassFish 3: Light-weight, Extensible, and Powerful @ JAX London ...
Arun Gupta
 
PDF
Ant, Maven and Jenkins
Kenu, GwangNam Heo
 
PDF
Alpes Jug (29th March, 2010) - Apache Maven
Arnaud Héritier
 
PDF
Gradle talk, Javarsovia 2010
Tomek Kaczanowski
 
PDF
Geneva Jug (30th March, 2010) - Maven
Arnaud Héritier
 
PDF
Apache Maven at GenevaJUG by Arnaud Héritier
GenevaJUG
 
PDF
Java EE 6 : Paving The Path For The Future
IndicThreads
 
PDF
Maven2交流
ChangQi Lin
 
Spring Web Service, Spring JMS, Eclipse & Maven tutorials
Raghavan Mohan
 
Hands On with Maven
Sid Anand
 
企业级软件的组件化和动态化开发实践
Jacky Chi
 
Apache Maven - eXo TN presentation
Arnaud Héritier
 
Boston 2011 OTN Developer Days - Java EE 6
Arun Gupta
 
Riviera JUG (20th April, 2010) - Maven
Arnaud Héritier
 
Lausanne Jug (08th April, 2010) - Maven
Arnaud Héritier
 
Lorraine JUG (1st June, 2010) - Maven
Arnaud Héritier
 
Spring Lab
Leo Nguyen
 
Java EE 6 & GlassFish = Less Code + More Power @ DevIgnition
Arun Gupta
 
Spark IT 2011 - Java EE 6 Workshop
Arun Gupta
 
Arun Gupta: London Java Community: Java EE 6 and GlassFish 3
Skills Matter
 
Java EE 6 & GlassFish 3: Light-weight, Extensible, and Powerful @ JAX London ...
Arun Gupta
 
Ant, Maven and Jenkins
Kenu, GwangNam Heo
 
Alpes Jug (29th March, 2010) - Apache Maven
Arnaud Héritier
 
Gradle talk, Javarsovia 2010
Tomek Kaczanowski
 
Geneva Jug (30th March, 2010) - Maven
Arnaud Héritier
 
Apache Maven at GenevaJUG by Arnaud Héritier
GenevaJUG
 
Java EE 6 : Paving The Path For The Future
IndicThreads
 
Maven2交流
ChangQi Lin
 
Ad

More from Raghavan Mohan (12)

PPTX
Accelerate with BIRT and Actuate11
Raghavan Mohan
 
PPTX
Who is BIRT
Raghavan Mohan
 
PDF
Introduction to BIRT
Raghavan Mohan
 
PDF
Sachin Tendulkar Resume
Raghavan Mohan
 
PDF
Manmohan Singh Resume
Raghavan Mohan
 
PDF
Senator Barrack Obama Resume
Raghavan Mohan
 
PDF
Java/J2EE CV Guide
Raghavan Mohan
 
PDF
Java/J2EE Companion
Raghavan Mohan
 
PDF
Fast Track to Ajax.
Raghavan Mohan
 
PDF
23617968 digit-fast-track-jan-2009-php
Raghavan Mohan
 
PPT
Quality - Douglas Crockford
Raghavan Mohan
 
PPT
The JavaScript Programming Language
Raghavan Mohan
 
Accelerate with BIRT and Actuate11
Raghavan Mohan
 
Who is BIRT
Raghavan Mohan
 
Introduction to BIRT
Raghavan Mohan
 
Sachin Tendulkar Resume
Raghavan Mohan
 
Manmohan Singh Resume
Raghavan Mohan
 
Senator Barrack Obama Resume
Raghavan Mohan
 
Java/J2EE CV Guide
Raghavan Mohan
 
Java/J2EE Companion
Raghavan Mohan
 
Fast Track to Ajax.
Raghavan Mohan
 
23617968 digit-fast-track-jan-2009-php
Raghavan Mohan
 
Quality - Douglas Crockford
Raghavan Mohan
 
The JavaScript Programming Language
Raghavan Mohan
 
Ad

Recently uploaded (20)

PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Advancing WebDriver BiDi support in WebKit
Igalia
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Advancing WebDriver BiDi support in WebKit
Igalia
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Biography of Daniel Podor.pdf
Daniel Podor
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 

Jsf, facelets, spring, hibernate, maven2

  • 1. 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: [email protected]
  • 2. 2 Table Of Contents Notations ..................................................................................................................... 3 Tutorial 9 – JSF, Facelets, Spring, Hibernate & Maven 2 ............................... 4 Tutorial 10 – Maven 2 ............................................................................................. 18
  • 3. 3 Notations Command prompt: Eclipse: File Explorer or Windows Explorer: Internet Explorer:
  • 4. 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:tutorialsjsimpleWeb) and simple (c:tutorialsjsimple) 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:tutorialsjsimpleWeb). <dependency> <groupId>com.mytutorial</groupId> <artifactId>simple</artifactId> <version>1.0-SNAPSHOT</version> <type>jar</type> </dependency> c:tutorialssimpleWebpom.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. 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. 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:tutorialssimpleWeb>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:tutorialssimpleWeb.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. 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. 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. 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. 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. 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. 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. 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:tutorialssimple>mvn clean install This should install the “simple-1.0-SNAPSHOT.jar” under “C:java.m2repositorycommytutorialsimple1.0-SNAPSHOT”. Refresh the simpleWeb project inside eclipse. Now Republish/package the simpleWeb project inside eclipse and start the Tomcat Server.
  • 14. 14 Make sure that your Database server is running: C:javahsqldb>java -cp ./lib/hsqldb.jar org.hsqldb.Server
  • 15. 15 Also preferably run the “DatabaseManeger” C:javahsqldb>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. 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. 17 That’s all to it. Please feel free to email any errors to [email protected]. Also stay tuned at http://www.lulu.com/java-success for more tutorials and Java/J2EE interview resources.
  • 18. 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:tutorialssimple-tutorial” as shown below. The parent pom.xml file will be shared by both the pom.xml files under “c:tutorialssimple-tutorialsimple” and “c:tutorialssimple-tutorialsimpleWeb”. c:tutorialssimple-tutorialpom.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>[email protected]</email> <organization></organization>
  • 19. 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. 20 c:tutorialssimple-tutorialsimplepom.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:tutorialssimple-tutorialsimpleWebpom.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. 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. 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:tutorialssimple-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:tutorialssimple-tutorial>mvn eclipse:clean eclipse:eclipse
  • 23. 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:tutorialssimple- tutorialsimpleWeb.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:tutorialssimple-tutorial>mvn clean install Step 4: Open up your eclipse workspace at “C:javaeclipse-tutorial-workspace”. You will get a screen as follows:
  • 24. 24 Delete the “simple” & “simpleWeb” (select “Do not delete contents” and pres OK ) projects and import them again.
  • 25. 25
  • 26. 26 Step 5: Publish the war file inside eclipse.
  • 27. 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:javahsqldb>java -cp ./lib/hsqldb.jar org.hsqldb.Server Also preferably run the “DatabaseManeger” C:javahsqldb>java -cp ./lib/hsqldb.jar org.hsqldb.util.DatabaseManager This will spawn a new window as shown:
  • 28. 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. 29 If you already have a “simpleWeb” folder under “C:javaTomcat6.0webapps” delete it first. Then copy the “simpleWeb.war” into Tomcat’s “C:javaTomcat6.0webapps” 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 [email protected]. Also stay tuned at http://www.lulu.com/java-success for more tutorials and Java/J2EE interview resources.