`
huxiaojun_198213
  • 浏览: 104282 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

JSF 2 + Spring 3 integration example

    博客分类:
  • JSF
阅读更多
In this tutorial, we will show you how to integrate JSF 2.0 with Spring 3 using :
1.JSF XML faces-config.xml
2.Spring annotations
3.JSR-330 standard injection

Tools and technologies used :
1.JSF 2.1.13
2.Spring 3.1.2.RELEASE
3.Maven 3
4.Eclipse 4.2
5.Tomcat 6 or 7

1. Directory Structure

A standard Maven project for demonstration.



2. Project Dependencies

Declares JSF 2, Spring 3, JSR-330 inject, and Tomcat’s dependencies.

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.mkyong.common</groupId>
	<artifactId>JavaServerFaces</artifactId>
	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>JavaServerFaces Maven Webapp</name>
	<url>http://maven.apache.org</url>
 
	<dependencies>
 
		<!-- Spring framework -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>3.1.2.RELEASE</version>
		</dependency>
 
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>3.1.2.RELEASE</version>
		</dependency>
 
		<!-- JSR-330 -->
		<dependency>
			<groupId>javax.inject</groupId>
			<artifactId>javax.inject</artifactId>
			<version>1</version>
		</dependency>
 
		<!-- JSF -->
		<dependency>
			<groupId>com.sun.faces</groupId>
			<artifactId>jsf-api</artifactId>
			<version>2.1.13</version>
		</dependency>
		<dependency>
			<groupId>com.sun.faces</groupId>
			<artifactId>jsf-impl</artifactId>
			<version>2.1.13</version>
		</dependency>
 
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
 
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
		</dependency>
 
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.1</version>
		</dependency>
 
		<!-- EL -->
		<dependency>
			<groupId>org.glassfish.web</groupId>
			<artifactId>el-impl</artifactId>
			<version>2.2</version>
		</dependency>
 
		<!-- Tomcat 6 need this -->
		<dependency>
			<groupId>com.sun.el</groupId>
			<artifactId>el-ri</artifactId>
			<version>1.0</version>
		</dependency>
 
	</dependencies>
 
	<build>
	<finalName>JavaServerFaces</finalName>
 
	<plugins>
	   <plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-compiler-plugin</artifactId>
		<version>2.3.1</version>
		<configuration>
			<source>1.6</source>
			<target>1.6</target>
		</configuration>
	   </plugin>
	</plugins>
	</build>
</project>


3. JSF 2 + Spring Integration

Spring’s bean in Spring Ioc context, and JSF’s managed bean in JSF Ioc context, how to make both working together? The solution is defined Spring’s SpringBeanFacesELResolver in faces-config.xml. Check this official Spring guide.

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_1.xsd"
	version="2.1">
 
	<application>
		<el-resolver>
    		    org.springframework.web.jsf.el.SpringBeanFacesELResolver
		</el-resolver>
  	</application>
 
</faces-config>


See following 3 examples to inject Spring’s bean in JSF managed bean.

3.1. XML Schema Example

Many developers still prefer to use XML to manage beans. With SpringBeanFacesELResolver, just uses EL ${userBo} to inject Spring’s bean into JSF’s managed bean.

UserBo.java
package com.mkyong.user.bo;
 
public interface UserBo{
 
  public String getMessage();
 
}


UserBoImpl.java

package com.mkyong.user.bo.impl;
 
import com.mkyong.user.bo.UserBo;
 
public class UserBoImpl implements UserBo{
 
	public String getMessage() {
 
		return "JSF 2 + Spring Integration";
 
	}
 
}


UserBean.java – JSF backing bean

package com.mkyong;
 
import java.io.Serializable;
import com.mkyong.user.bo.UserBo;
 
public class UserBean{
 
        //later inject in faces-config.xml
	UserBo userBo;
 
	public void setUserBo(UserBo userBo) {
		this.userBo = userBo;
	}
 
	public String printMsgFromSpring() {
 
		return userBo.getMessage();
 
	}
 
}


applicationContext.xml – Declares userBo bean

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
	<bean id="userBo" class="com.mkyong.user.bo.impl.UserBoImpl"></bean>
 
</beans>


faces-config.xml – Declares managed bean and inject userBo

<?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"
    version="2.0">
 
	<managed-bean>
		<managed-bean-name>user</managed-bean-name>
		<managed-bean-class>com.mkyong.UserBean</managed-bean-class>
		<managed-bean-scope>session</managed-bean-scope>
		<managed-property>
			<property-name>userBo</property-name>
			<value>#{userBo}</value>
		</managed-property>
	</managed-bean>
 
</faces-config>


3.2. Spring Annotations – Auto Scan

This example is using Spring annotations. Injects like a normal bean with @ManagedBean, @Autowired and @Component, it just works as expected.

UserBoImpl.java

package com.mkyong.user.bo.impl;
 
import org.springframework.stereotype.Service;
import com.mkyong.user.bo.UserBo;
 
@Service
public class UserBoImpl implements UserBo{
 
	public String getMessage() {
 
		return "JSF 2 + Spring Integration";
 
	}
 
}


UserBean.java


package com.mkyong;
 
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.mkyong.user.bo.UserBo;
 
@Component
@ManagedBean
@SessionScoped
public class UserBean{
 
	@Autowired
	UserBo userBo;
 
	public void setUserBo(UserBo userBo) {
		this.userBo = userBo;
	}
 
	public String printMsgFromSpring() {
		return userBo.getMessage();
	}
 
}


applicationContext.xml – Enable the component auto scan

<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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.1.xsd">
 
	<context:component-scan base-package="com.mkyong" />
 
</beans>


Mixed use of both JSF and Spring annotations are working fine, but it look weird and duplicated – @Component and @ManagedBean together. Actually, you can just uses a single @Component, see following new version, it’s pure Spring, and it works!

UserBean.java

package com.mkyong;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
 
import com.mkyong.user.bo.UserBo;
 
@Component
@Scope("session")
public class UserBean{
 
	@Autowired
	UserBo userBo;
 
	public void setUserBo(UserBo userBo) {
		this.userBo = userBo;
	}
 
	public String printMsgFromSpring() {
		return userBo.getMessage();
	}
 
}


3.3. JSR-330 Annotation

Since Spring 3.0, Spring offer supports for JSR-330 injection standard. Now, you can uses @Inject to replace for @Autowired and @Named for @Component. This is recommended to solution, follow JSR-330 standard make the application more portable to other environments, and it works fine in Spring framework.

UserBoImpl.java

package com.mkyong.user.bo.impl;
 
import javax.inject.Named;
import com.mkyong.user.bo.UserBo;
 
@Named
public class UserBoImpl implements UserBo{
 
	public String getMessage() {
 
		return "JSF 2 + Spring Integration";
 
	}
 
}


UserBean.java

package com.mkyong;
 
import javax.inject.Inject;
import javax.inject.Named;
import org.springframework.context.annotation.Scope;
import com.mkyong.user.bo.UserBo;
 
@Named
@Scope("session") //need this, JSR-330 in Spring context is singleton by default
public class UserBean {
 
	@Inject
	UserBo userBo;
 
	public void setUserBo(UserBo userBo) {
		this.userBo = userBo;
	}
 
	public String printMsgFromSpring() {
		return userBo.getMessage();
	}
 
}


applicationContext.xml – Need component auto scan also

<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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.1.xsd">
 
	<context:component-scan base-package="com.mkyong" />
 
</beans>


4. Demo

Example in 3.1, 3.2 and 3.3 are doing exactly the thing – Inject userBo into JSF bean, just different implementation. Now, create a simple JSF page to show the the result.

default.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      >
 
    <h:body>
 
    	<h1>JSF 2.0 + Spring Example</h1>
 
 	#{userBean.printMsgFromSpring()}
 
    </h:body>
 
</html>



web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
	id="WebApp_ID" version="2.5">
 
  <display-name>JavaServerFaces</display-name>
 
  <!-- Add Support for Spring -->
  <listener>
	<listener-class>
		org.springframework.web.context.ContextLoaderListener
	</listener-class>
  </listener>
  <listener>
	<listener-class>
		org.springframework.web.context.request.RequestContextListener
	</listener-class>
  </listener>
 
  <!-- Change to "Production" when you are ready to deploy -->
  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>
 
  <!-- Welcome page -->
  <welcome-file-list>
    <welcome-file>default.jsf</welcome-file>
  </welcome-file-list>
 
  <!-- JSF Mapping -->
  <servlet>
    <servlet-name>facesServlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>facesServlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>facesServlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>
 
</web-app>


Done, see output : http://localhost:8080/JavaServerFaces/default.jsf




References
1.Spring reference – SpringBeanFacesELResolver
2.Spring how to do dependency injection in your session listener
3.http://www.mkyong.com/spring3/spring-3-and-jsr-330-inject-and-named-example

From:http://www.mkyong.com/jsf2/jsf-2-0-spring-integration-example/

JSF 2.0Tutorials
  • 大小: 6.4 KB
  • 大小: 13.4 KB
分享到:
评论

相关推荐

    JSF+Spring+Ibatis示例

    JSF+Spring+Ibatis示例,对学习JAVA企业应用开发有巨大的帮助!

    JSF2.2.6+Spring + Hibernate整合可运行

    2. **Spring配置**:Spring的配置文件(如applicationContext.xml)定义了bean的声明和依赖注入,包括服务层和DAO层的实现。 3. **Hibernate配置**:Hibernate的配置文件(如hibernate.cfg.xml)设置了数据库连接...

    jsf+jpa+spring整合过程

    2. 完成向导后,需要在`faces-config.xml`文件中添加以下配置,以使Spring与JSF协同工作: ```xml &lt;variable-resolver&gt;org.springframework.web.jsf.DelegatingVariableResolver ``` 这使得JSF能够使用...

    JSF2 + Primefaces3 + Spring3 & Hibernate4 Integration Project

    **JSF2 + Primefaces3 + Spring3 & Hibernate4 集成项目详解** JavaScript Framework (JSF) 是Java平台上的一种用于构建用户界面的MVC(Model-View-Controller)框架,而Primefaces是一个基于JSF的开源UI组件库,...

    jsf1.2+Spring3.0.5+Mybatis

    2. `web.xml` - Web应用的部署描述符,配置了JSF和Spring的初始化参数。 3. `spring-context.xml` - Spring的配置文件,包含Bean定义、数据源、事务管理器等配置。 4. `mybatis-config.xml` - Mybatis的全局配置文件...

    jsf2+spring sample

    3. **数据绑定**:JSF2的EL表达式可以直接引用Spring Beans中的属性,实现视图与模型的数据绑定。 4. **服务层和持久层的整合**:Spring负责业务逻辑和服务层的实现,而JSF2负责视图的展示和用户交互。持久层可以...

    jsf+spring+hibernate

    【JSF+Spring+Hibernate整合开发】 JSF (JavaServer Faces)、Spring 和 Hibernate 是 Java 开发中的三大核心技术,常用于构建企业级的 Web 应用程序。它们各自扮演着不同的角色,共同构建了一个强大的后端架构。 1...

    JSF2.0+Spring+Hibernate实例代码

    3. **JSF Managed Beans**:使用JSF 2.0的注解定义,作为视图和模型之间的桥梁,通常存储用户输入的数据和业务逻辑。 4. **Spring配置**:可能包含Spring的XML配置文件,定义了bean及其依赖关系,也可能使用Java配置...

    JSF+Spring+Hibernate小例子

    **JSF+Spring+Hibernate整合应用详解** 在Java Web开发中,JSF(JavaServer Faces)、Spring和Hibernate是三个常用的技术栈,它们分别负责视图层、业务逻辑层和服务数据持久化层。这个"JSF+Spring+Hibernate小例子...

    JSF+Spring+Hibernate(框架整合)详细过程

    以下是对"JSF+Spring+Hibernate"整合的详细过程的阐述: 1. **JavaServer Faces (JSF)**:JSF是一种基于组件的MVC(模型-视图-控制器)框架,主要用于构建企业级的Web应用程序。它提供了一套预定义的UI组件,使得...

    jsf2+primefaces+spring+hibernate案例

    【标题】"jsf2+primefaces+spring+hibernate案例"揭示了一个基于Java的全栈Web开发项目,它整合了几个关键的技术框架。JSF(JavaServer Faces)是Oracle公司推出的Java Web应用程序开发框架,用于构建用户界面。...

    JSF+Spring+Hibernate jar lib

    JSF+Spring+Hibernate jar文件压缩包,hibernate最小配置,Spring 2.0 jar, richfaces

    jsf+hibernate+spring集成案例

    2. **JSF与Spring的集成**:通过Spring JSF Integration或PrimeFaces的Spring Support,我们可以将Spring的依赖注入引入JSF的Managed Beans,使得JSF组件可以方便地使用Spring管理的bean。 3. **Hibernate的使用**...

    jsf+spring2.5+jpa(hibernate)的jar包

    这是jsf+spring2.5+jpa(hibernate)的jar包,很多人为了jsj环境而配置半天,在此提供jar包共享。注:除了ajax4jsf和tomahawk-1.1.3.jar,因为csdn只让我上传20mb,大家自己可以下一下自己试试。

    JSF+Spring+Hibernate的实例讲解.doc

    2. **集成 Spring**:通过在 JSF 的 Managed Beans 中注入 Spring 的 Bean,可以利用 Spring 的依赖注入特性。这需要配置 Spring 的上下文文件,声明 Bean 及其依赖,并启用 JSF-Spring 桥接器。 3. **整合 ...

    JSF+Spring+Hibernate 分页显示

    总的来说,JSF+Spring+Hibernate的组合提供了强大的功能,用于构建高效的分页显示系统。在实际应用中,我们需要根据项目规模和性能需求,灵活运用这些技术和策略,以实现最优的用户体验和系统性能。

    ajax+jsf+spring+hibernate

    3. **Spring的配置和MVC** - 检视Spring的配置文件(如applicationContext.xml、web.xml),理解Spring的依赖注入和Spring MVC的路由设置。 4. **Hibernate的实体和映射** - 查看Hibernate的实体类(Java POJOs)和...

    jsf1.2+spring3.0+hibernate3.3例子-单表增删改查

    使用jsf1.2+spring3.0+hibernate3.3实现集成,利用annotation实现自动对象管理注入,用户表的登录增删改查操作,包括验证码ajax集成,消息机制,国际化处理,自定义转换器,自定义验证器等 qq:38732796 欢迎讨论

Global site tag (gtag.js) - Google Analytics