JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

Embed Size (px)

Citation preview

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    1/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:[email protected]

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    2/29

    2

    Table Of Contents

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

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    3/29

    3

    Notations

    Command prompt:

    Eclipse:

    File Explorer or Windows Explorer:

    Internet Explorer:

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    4/29

    4

    Tutorial 9 JSF, Facelets, Spring, Hibernate & Maven 2

    This tutorial will put together tutorials 1-8. It will also incorporate dependenciesbetween 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 ourpom.xml

    file undersimpleWeb (c:\tutorials\jsimpleWeb).

    com.mytutorialsimple

    1.0-SNAPSHOT

    jar

    c:\tutorials\simpleWeb\pom.xml

    4.0.0

    com.mytutorialsimpleWeb

    war1.0-SNAPSHOT

    simpleWeb Maven Webapp

    http://maven.apache.org

    com.mytutorial

    simple

    1.0-SNAPSHOT

    jar

    junit

    junit3.8.1

    test

    commons-digester

    commons-digester1.8

    commons-collectionscommons-collections

    3.2

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    5/29

    5

    org.springframework

    spring2.0.6

    javax.faces

    jsf-api

    1.2

    javax.faces

    jsf-impl

    1.2_04

    com.sun.facelets

    jsf-facelets1.1.11

    javax.servlet

    jstl

    1.1.2

    javax.el

    el-api

    1.0provided

    com.sun.elel-ri

    1.0

    simpleWeb

    org.apache.maven.plugins

    maven-compiler-plugin

    2.0.2

    1.5

    1.5

    org.apache.maven.plugins

    maven-eclipse-plugin2.4

    false1.5

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    6/29

    6

    maven-repository.dev.java.netJava Dev Net Repository

    http://download.java.net/maven/2/

    true

    never

    false

    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 underC:\tutorials\simpleWeb\.settingsas shown

    below from version=2.4 to version=2.5. Every timeyou use the eclipse:clean command, you will haveto 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.

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    7/29

    7

    Since simpleWeb depends on simple maven2 has transitively brought all it dependencies into the

    simpleWebs build path. In order to build a war file inside eclipse (pom.xml file is useful for buildingwar 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 Tomcats 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/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    8/29

    8

    Step 4: Lets 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;

    publicclassApp {

    publicstaticvoid main(String[] args) {

    ApplicationContext ctx = new ClassPathXmlApplicationContext( "applicationContext-

    mytutorial.xml");

    List courses = new ArrayList(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 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;

    publicinterface CourseService {

    publicabstractvoidprocessCourse(List courses);

    publicabstract List getCourses(); //added

    }

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    9/29

    9

    CourseServiceImpl.java in simple project

    package com.mytutorial;

    import java.util.List;

    publicclass CourseServiceImpl implements CourseService {

    private CourseDao courseDao;

    public CourseDao getCourseDao() {returncourseDao;

    }

    publicvoidsetCourseDao(CourseDao courseDao) {this.courseDao = courseDao;

    }

    //ModifiedpublicvoidprocessCourse(List courses) {

    // CourseDao dao = new CourseDaoImpl();

    courseDao.create(courses);

    }

    //Addedpublic List getCourses() {

    List list = getCourseDao().findAll();return list;

    }

    }

    CourseControllerBean.java in simpleWeb project

    package com.mytutorial;

    import java.util.List;

    publicclass CourseControllerBean {

    private CourseService courseService; // injected via spring

    public List getCourses(){return getCourseService().getCourses();

    }

    public CourseService getCourseService() {returncourseService;

    }

    publicvoidsetCourseService(CourseService courseService) {this.courseService = courseService;

    }

    }

    greeting.jspx in simpleWeb project

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    10/29

    10

    greeting page

    ,

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    11/29

    11

    applicationContext.xml in simpleWeb project

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    12/29

    12

    finally the web.xml file.

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

    contextConfigLocation

    /WEB-INF/applicationContext.xmlclasspath:applicationContext-mytutorial.xml

    ]]>

    The web.xml file should look like:

    javax.faces.STATE_SAVING_METHOD

    server

    javax.faces.CONFIG_FILES

    /WEB-INF/faces-config.xml

    javax.faces.DEFAULT_SUFFIX

    .jspx

    contextConfigLocation

    /WEB-INF/applicationContext.xml

    classpath:applicationContext-mytutorial.xml]]>

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    13/29

    13

    facelets.DEVELOPMENTtrue

    com.sun.faces.validateXml

    true

    com.sun.faces.verifyObjects

    true

    org.springframework.web.context.ContextLoaderListener

    org.springframework.web.context.request.RequestContextListener

    Faces Servlet

    javax.faces.webapp.FacesServlet

    1

    Faces Servlet*.jsf

    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.

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    14/29

    14

    Make sure that your Database server is running:

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

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    15/29

    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

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    16/29

    16

    Click on link Click Me.

    Click on button Hello

    The Name & Course are empty. Now lets add some name and courses into the database either viaSQL 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 internetexplorer and click on refresh button.

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    17/29

    17

    Thats all to it.

    Please feel free to email any errors [email protected]. Also stay tuned at

    http://www.lulu.com/java-success for more tutorials and Java/J2EE interview

    resources.

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    18/29

    18

    Tutorial 10 Maven 2

    You may have noticed that there are some code repeats in pom.xml files insimple and simpleWeb projects. Maven2 child pom.xml files can inherit fromparent pom.xml file. In this tutorial lets clean up our project structures and thepom.xml files.

    Step 1: Exit out of eclipse and restructure your projects underC:\tuitorials as follows:

    Create a new folder simple-tutorial under c:\tutorials and move the simple & simpleWebunder simple-tutorial.

    Step 2: Add the parent pom.xmlunder c:\tutorials\simple-tutorial as shown below.

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

    c:\tutorials\simple-tutorial\pom.xml (parent pom.xml)

    4.0.0

    com.mytutorial

    simple-tutorialpom

    simple tutorials

    simple tutorials

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

    2007

    Arulkumaran

    ak1

    [email protected]

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    19/29

    19

    Senior Java Designer/Developer

    +10

    Sivayini

    Java Developer

    simple

    simpleWeb

    org.apache.maven.plugins

    maven-compiler-plugin

    2.0.2

    1.51.5

    org.apache.maven.plugins

    maven-eclipse-plugin

    2.4

    false

    1.5

    maven-repository.dev.java.net

    Java Dev Net Repository

    http://download.java.net/maven/2/

    true

    never

    false

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    20/29

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    21/29

    21

    simple-tutorial

    1.0

    com.mytutorialsimpleWeb

    war

    1.0-SNAPSHOTsimpleWeb Maven Webapp

    http://maven.apache.org

    com.mytutorialsimple

    1.0-SNAPSHOT

    jar

    junitjunit

    3.8.1

    test

    commons-digester

    commons-digester1.8

    commons-collections

    commons-collections

    3.2

    org.springframework

    spring

    2.0.6

    javax.faces

    jsf-api

    1.2

    javax.faces

    jsf-impl1.2_04

    com.sun.faceletsjsf-facelets

    1.1.11

    javax.servlet

    jstl

    1.1.2

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    22/29

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    23/29

    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 underC:\tutorials\simple-

    tutorial\simpleWeb\.settingsas shown below from version=2.4 to version=2.5. Every timeyou 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 ascreen as follows:

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    24/29

    24

    Delete the simple & simpleWeb (select Do not delete contents and pres OK) projects and

    import them again.

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    25/29

    25

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    26/29

    26

    Step 5: Publish the war file inside eclipse.

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    27/29

    27

    After publishing, run the Tomcat Server.

    Step 6: Run the HSQLDB server in a command prompt and also open the DatabaseManager inanother 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:

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    28/29

    28

    Open an internet browser and type the URL

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

    Thats all to it. Also try deploying outside Tomcat. Stop the Tomcat server inside eclipse.

  • 8/8/2019 JSF, Facelets, Spring, Hibernate, Maven2 & Eclipse - Putting it All together

    29/29

    29

    If you already have a simpleWeb folder underC:\java\Tomcat6.0\webappsdelete it first. Then

    copy the simpleWeb.war into Tomcats C:\java\Tomcat6.0\webappsfolder. Double click ontomcat6.exeto 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 underEmerging technologies/framework section in Java/J2EE Job Interview Companion at

    http://www.lulu.com/content/192463

    Please feel free to email any errors [email protected]. Also stay tuned at

    http://www.lulu.com/java-success for more tutorials and Java/J2EE interview

    resources.