Feb 05

I spent almost 2 days putting these three together to generate a test application. The beauty about this app is it dose not have any xml configuration file except of course the applicationcontext.xml for spring. And in that file also there is just the datasource defined. I thought of putting it down here in order to save somebody stumbling with the same some time.

What Does the app do?

well its just a test application. It shows how to wire everything together just using the annotations. and it also shows the Ajax functionality and the templating functionality of JSF 2.0. and it also inserts some test entity into the Database

Lets get directly dirty with the code

Lets start with the applicationContext.xml file ..

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"
        p:driverClassName="${jdbc.driverClass}" p:password="${jdbc.password}" p:url="${jdbc.url}"
        p:username="${jdbc.username}"> </bean>
    <bean class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
        id="sessionFactory" p:dataSource-ref="dataSource">

        <property name="annotatedClasses">
        <property name="hibernateProperties">
            <value> hibernate.dialect=org.hibernate.dialect.MySQLDialect
                hibernate.show_sql=${jdbc.showSql} </value>

    <bean class="org.springframework.orm.hibernate3.HibernateTransactionManager"
        id="transactionManager" p:sessionFactory-ref="sessionFactory"/>
    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        id="entityManagerFactory" lazy-init="false" p:dataSource-ref="dataSource">
        <property name="persistenceUnitName" value="testsource"/>

    <!-- Activates annotation-based bean configuration -->

    <!-- needed is for @Configurable -->
    <context:component-scan base-package="com.test"/>

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties"/>

As you see all you have done is defined your datasource and wired it with entityManagerFactory which will be used by hibernate. All the connection properties are comming from the file jdbc.properties which is looked up from the classpath so it has to be placed inside the classes directory within WEB-INF.  The next thing to do is to add the persistance.xml in META-INF folder which looks like this

<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="testsource" transaction-type="RESOURCE_LOCAL">
             | The SQL dialect to use for this data source, use MySQL InnoDB as
             | we only use InnoDB tables for the entity beans.
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
             | Specify the second level query cache for hibernate.
             | Enable query caching availability.
            <!-- <property name="hibernate.cache.use_query_cache" value="true"/> -->


As you must have noticed here we only declare the persistence-unit name which is used by spring.
And the last xml file is the faces-config.xml. i know i said just one configuration :P . here we dont define any beans or navaigation rules here we just have our resource bundle and the reslover for faces beans.

<?xml version="1.0"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"

so now its all configured. Lets start with the entity class. It looks like this

public class Car implements Serializable{

 @GeneratedValue(strategy = GenerationType.AUTO)
 private Long id;
 private String carName;

next is the DAOimpl class

public class CarDaoImpl implements CarDao{

 private EntityManager entityManagerFactory;

 void setEntityManager(EntityManager entityManager) {
 this.entityManagerFactory = entityManager;

 public void addCar(Car car){



The @Repository annotation is yet another stereotype that was introduced in Spring 2.0 itself. This annotation is used to indicate that a class functions as a repository.

then we have the spring service classes

public class CarServiceImpl implements CarService{

 private final CarDao carDao;

 CarServiceImpl(CarDao carDao) {
 this.carDao = carDao;
 } ...

and finally the backing bean

public class CarSession {
 private Car car;

 public Car getCar() {
 return car;
 } ..

we use annotations for defining managed beans which otherwise would have been declared in ur config file so this significantly reduces your XML, but you can get rid of virtually all of it with JSF 2 through either annotations, as I’m doing for managed beans, or with convention, as is the case for JSF 2’s simplified navigation handling.
so now we have everything in palce .. the only files left are the xhtml files

<ui:composition xmlns="http://www.w3.org/1999/xhtml"

  <ui:define name="menuLeft">
    <!-- <ui:include src="/sections/login/menuLeft.xhtml"/>-->

  <ui:define name="content">
     <ui:include src="/sections/welcome.xhtml"/>


and the file sections/welcome.xhtml looks like this.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="http://www.w3.org/1999/xhtml"

	<h:form id="form" prependId="false" >
    <h:outputText value="#{i18n.prefs_country}"/>
            <h:selectOneListbox id="company" value="#{car.company}" size="1">
              <f:selectItems value="#{facesSupport.carModels}"/>
              <f:ajax render="model"/>
            <h:outputText value="#{i18n.prefs_region}"/>
            <h:selectOneListbox id="model" value="#{car.model}" style="width: 180px;" size="1">
              <f:selectItems value="#{car.modelOptions}"/>
     <h:commandButton id="loginButton" value="save" action="#{car.saveCar}"/>

as you see how ajax is used to render the model drop box when ever the company box is changed.

the last thing left is the web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"


      <param-value>/WEB-INF/web-application-config.xml </param-value>


    <servlet-name>Faces Servlet</servlet-name>

    <servlet-name>Faces Servlet</servlet-name>




Thats it .. everything is in place. soon i will zip my test application and upload it so that its easier to go through the code..

Well if there are any questions or suggestions feel free to leave a comment

Finally got some time to create a sample application .. you can download it from here have also included spring security 3.0 into it .. so enjoy ;) ..u can just change from .zip to .war and deploy the application ..but before that dont forget to change the properties file inside /WEB-INF/config add your database connection details there... if there are any questions just write a comment ... and ya .. the source is included inside the war file ... the war file has been tested with tomcat 6 just place it on webapps .. and point ur browser to http://localhost:8080/sample/index.html ..

Tagged with:
preload preload preload