Integrate Spring into JSF 2

There are many ways to integrate the Spring Framework into a JSF application. In this post I write about a tight integration in which almost all configuration is done with Spring annotations without loosing JSF bean handling in the front end. I have created an application template to show this approach. The project is hosted on GitHub (spring-jsf). In the end of the article you find a guide how to build and run this application.

JSF

The main piece of integration of Spring into JSF is done in JSF configuration. We want to use Spring managed classes as JSF beans. To achieve this you have to set org.springframework.web.jsf.el.SpringBeanFacesELResolver as a el-resolver in your JSF configuration.

WEB-INF/faces-config.xml

<?xml version='1.0' encoding='UTF-8'?>	
<faces-config
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"	 	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" version="2.0">	 
  <application>	 
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
  </application>	 	
</faces-config>

You can now configure a Spring class as a JSF managed bean by annotations:

@Component("user")
@Scope("session")
public class UserBean implements Serializable

In this example class UserBean is configured as session scoped JSF Bean with name user. Use it in your templates as usual:

<h:outputLabel value="User: #{user.user.login}" rendered="#{user.ok}"/>

Spring

Lets have a lock a the Spring configuration file. What you see has nothing to do with JSF. It is a standard file if you use annotations as much as possible:

WEB-INF/applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:context="http://www.springframework.org/schema/context" 
  xmlns:tx="http://www.springframework.org/schema/tx" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
  
  <context:annotation-config/>
  <context:component-scan base-package="org.n2.app.beans"/>
  <bean>
    <property name="location">
      <value>/WEB-INF/classes/database.properties</value>
    </property>
  </bean>
  <bean id="dataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="username" value="${jdbc.username}"/>
  </bean>
  <bean id="sessionFactory">
    <property name="dataSource">
      <ref bean="dataSource"/>
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hibernate.show_sql">false</prop>
      </props>
    </property>
    <property name="annotatedClasses">
      <list>
        <value>org.n2.chess.beans.hibernate.User</value>
      </list>
    </property>
  </bean>
  <bean id="transactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>
  <tx:annotation-driven/>
</beans>
  • Line 10: Bean configuration by annotations is activated
  • 11: Classes from package org.n2.app.beans are scanned for annotations.
  • 12-22: Database configuration. At runtime you will need a file database.properties in the same folder to set properties jdbc.driverClassName, jdbc.url, jdbc.username and jdbc.password.
  • 23-41: Hibernate configuration
  • 42: Transaction configuration by annotations is activated

Hibernate

In the application template you find one entity class and one dao to handle the user login. The entity class org.n2.app.beans.hibernate.User is configured by annotations. The dao org.n2.app.beans.hibernate.UserDao is a kind of „POJO-Dao“. Unlike many other daos in Spring application it does not extend or use any Spring class. Instead of this, the Hibernate SessionFactory is injected and you do all database operations with a pure Hibernate session. However, this is not the focus of this article.

Build and run

I created a application template to show how all the stuff works in practice. The application provides a register and login form. Login uses a save hashing function described in this guide from the OWASP Foundation. Fell free to use my application template to start with your own application, fork it on GitHub!

Short guide to build and run my application template:

Clone Git repository

git clone https://github.com/murygin/spring-jsf.git
cd spring-jsf

Create database

mysql -u <USER> -p
create database springjsf;
exit;
mysql -u <USER> -p springjsf < src/main/sql/spring-jsf-schema.sql

Set database properties
To set the database properties edit the file src/main/resources/database.properties.

Maven build

cd spring-jsf
mvn package

Maven compiles the code and creates the War file target/spring-jsf.war. Copy the War file to your Tomcat wepapps directory. Start Tomcat and load
http://localhost:8080/spring-jsf.

    • Amine
    • 28. November 2013

    Thank’s man

  1. You may also want to read my article http://www.beyondjava.net/blog/integrate-jsf-2-spring-3-nicely/. Among other things, it mentions @ViewScope (which is pretty problematic because it doesn’t match any Spring scope), and it has a couple of links to simple Spring integration projects on GitHub.

  1. 26. Oktober 2013
    Trackback from : JSF and Spring | hfontis

Schreibe einen Kommentar

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s

%d Bloggern gefällt das: