UP | HOME

Create Project

Table of Contents

1 Create new Dynamic Web project

  • Right-click in view Package Explorer (Alt-Shift N) and use the Wizard to create a new Dynamic Web project.
  • Be sure that a target runtime (tomcat 6 or 7) is set, choose Configuration java Server Faces V2.0 Project, and check the box Generate web.xml deployment descriptor.

2 Get JDBC drivers for MySQL, HSQLDB and libraries for Hibernate and Spring.

  • Right-click on your project and select Maven -> Enable dependency management, give it a name and description and finish.

    Make sure that the java Compiler Level of your project and the project facet are the same (if not you will see an error in the Problems or Marker view). To change rightclick on project, choose Properties and select java Compiler and set it accordingly.

  • In the editor select the source view (tab pom.xml in the bottom bar) and add repositories (needed for myfaces-api 2.x.x and primfaces) and dependencies from Maven pom.xml.

    Make sure you see the libraray Maven Dependencies in the Package Explorer, otherwise Right-click on your project and select Build Path -> Configure Build Path. Select Libraries, add Maven Dependencies on the build path.

  • Select Deployment Assembly from the Properties of the project (select the project in the package explorer, use key Alt-Enter to get the Properties and type deploy in the search field). Click button Add and choose java Build Path Entries and choose Maven Dependencies. Click Apply and OK.

3 Configure JSF

JSF is configured in the web.xml deployment descriptor. Add the JSF-config section to the web.xml.

To allow JSF to implicit (using annotations) access Spring managed beans add the Spring expression language (el) resolver to the faces-config.xml file in the WEB-INF directory.

<application>
  <el-resolver>
     org.springframework.web.jsf.el.SpringBeanFacesELResolver
  </el-resolver>
</application>

4 Configure the database connection of the application

Create a directory resources in the project root. Rightclick on on the directory and choose Build Path and select Use as Source folder.

Verify that the src directory is also marked as source folder.

Spring/Hibernate configuration files are placed in the resources/config, resources/database, and resources/properties directories resp.

  • The Spring context is defined in BeanLocations.xml and placed in the resources/config directory. The context is created by the application:
  • The file Hibernate.xml defines the hibernate sessionFactory and the mapped (annotated model) classes and is placed in resource/database.
  • The data source is a spring bean defined in the file DataSource.xml and placed in resources/database.
  • Customizable data is held in a properties file database.properties. This makes it save to change, e.g. from a HSQLDB to a MySQL database. Uncomment the paramter setting for the HSQL database.
    ApplicationContext appContext = 
         new ClassPathXmlApplicationContext("/config/BeanLocations.xml");
    
  • To reference the files from elswhere use path
    classpath:/config/BeanLocations.xml
    
  • A reference to a Bean, e.g. ContainerBo is obtained using the method getBean()
    appContext.getBean("ContainerBo")
    

5 Configure user authentication and authorization

We use a independent database to hold user data. The configuration is added to the web.xml and deployed at startup of the application.

The Hibernate dataSource configuration, the Beans and the access definitions (URL authorization) are defined (in separate files) in the WEB-INF directory.

6 Deploy project

Later you will export the project and deploy the war-file in your Web-server. To deploy or re-deploy your project in eclipse use the follwong steps:

  • Force Build: Select all (dependent) projects, rightclick and choose refresh, choose Clean from Project menue (eclipse top bar).
  • Publish the project: rightclick on server and choose Add and Remove and Add the project.
  • Deploy: rightclick on server and choose Start or Debug.
  • Clean: rightclick on server and choose Clean, do this before re-deploying it helps to clear the application context (most of the time).
  • Stop server

    Deploying at this time will lead to errors (see the Console view). The Beans referenced in the configurations are not yet there. You can test the configuration by removing the references.

  • Remove the local beans from applicationContext.xml
<bean id="userDao"
    class="de.botzenhart.testjsf.security.dao.UserDaoImpl"
    scope="singleton">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
 </bean>
 <bean id="userController"
    class="de.botzenhart.testjsf.security.service.UserControllerImpl">
    <constructor-arg ref="userDao" />
 </bean>

<authentication-manager>
        <!-- <authentication-provider  user-service-ref="userController">
                <password-encoder hash="sha" />
        </authentication-provider>
         -->
    <authentication-provider>
        <password-encoder hash="md5"/>
        <user-service>
            <user name="test" password="098f6bcd4621d373cade4e832627b4f6" authorities="ROLE_SUPERVISOR, ROLE_USER, ROLE_TELLER" />
            <user name="test2" password="098f6bcd4621d373cade4e832627b4f6" authorities="ROLE_TELLER" />
            </user-service>
        </authentication-provider>
</authentication-manager>
<property name="annotatedClasses">
    <list>
       <value>de.botzenhart.testjsf.security.model.User</value>
    </list>
</property>

Author: Rainer Schuler

Date: 2011-02-11 Fr

HTML generated by org-mode 7.4 in emacs 22