关键字: struts2 spring
在Struts2中整合Spring的IoC支持是一件十分简单的事情。让我们一步一步来实现:
1)复制struts2-spring-plugin-x-x-x.jar和相应的spring.jar到/WEB-INF/lib目录下。
2)在struts.properties中设置struts.objectFactory属性值
struts.properties
-
struts.objectFactory = spring
或者是在XML文件中进行常量配置
struts.xml
-
<struts>
-
<constant name="struts.objectFactory" value="spring" />
- </struts>
3)配置Spring监听器
web.xml
-
<listener>
-
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
通过Spring配置来注册对象
applicationContext.xml
-
<?xml version="1.0" encoding="UTF-8"?>
-
<!DOCTYPE beans PUBLIC
-
"-//SPRING//DTD BEAN//EN"
-
"http://www.springframework.org/dtd/spring-beans.dtd">
- <beans default-autowire="autodetect">
-
<bean id="hello" class="hpfyeah.struts2.spring.HelloWorldAction"/>
- </beans>
当然你也可以拥有更多的Spring配置文件。在web.xml中进行下列设置,从而使Spring的ApplicationContext通过匹配所给定模式的文件来初始化对象
web.xml
-
- <context-param>
-
<param-name>contextConfigLocation</param-name>
-
<param-value>
-
/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml
-
</param-value>
- </context-param>
4)修改你的Struts配置文件
struts.xml
-
<!DOCTYPE struts PUBLIC
-
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
-
"http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
-
<include file="struts-default.xml"/>
-
<package name="default" extends="struts-default">
-
<action name="pureStruts" class="hpfyeah.struts2.spring.HelloWorldAction">
-
<result>hello.jsp</result>
-
</action>
-
<action name="springStruts" class="hello">
-
<result>hello.jsp</result>
-
</action>
- </struts>
默认情况下,Spring从上面显示的applicationContext.xml文件中寻找为hello所做的配置
5)好了,现在你的Struts2和Spring就能正常的一起工作了。有几个配置技术点需要详细说明下:
装配模式。你可以通过设置修改struts.properties中下列属性的值来改变装配模式。
name |
按照你的action的属性的名字和Spring里的bean的名字匹配,如果匹配就自动装配。这是缺省的
|
type |
按照你的action的属性的类型,在Spring注册的bean中查找,如果相同就自动装配。这需要你在Spring中仅注册了一个此类型的bean |
auto |
Spring会试图自动监测来找到最好的方法自动装配你的action |
constructor |
Spring会自动装配bean的构造函数的参数 |
是否使用类缓存。你可以通过设置修改struts.properties中下列属性的值来改变是否使用Spring自身的类缓存机制。可以设定的值为true或false,默认为true。
struts.properties
-
struts.objectFactory.spring.useClassCache = false
分享到:
相关推荐
整合Spring与Struts的几种方法
spring和struts2.x的整合篇spring和struts2.x的整合篇
spring与struts2整合的小示例,适合初学者,具体说明在博客里
只使用spring和struts进行整合
spring 和 struts整合 spring 和 struts整合 spring 和 struts整合
里面有spring+struts2+ibatis整合的jar包,
SSH+Redis整合
整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6。 此外,还有:log4j、slf4j、junit4、ehcache等知识点。 项目...
Struts2整合Spring3.0,Spring虽然有自己的MVC,但是由于繁琐的配置,并不理想,Struts2可以和Spring完美结合,来完成MVC的实现。
Spring 整合 Struts,很强大
struts2和spring和Hibernate的整合包,在MyEclipse中自动导入老是有冲突包。
spring4 struts2 mybatis3 maven3 整合 测试正常 无误
maven整合spring+hibernate+struts2
ibatis+Spring+struts2整合实例
SSM(Spring+Struts2+Mybatis)整合步骤 纯手写
spring和struts整合的三种方案.
详细介绍:细谈Spring(十一)深入理解spring+struts2整合(附源码)http://blog.csdn.net/csh624366188/article/details/7666280
Eclipse Java_EE整合spring+ struts2最简洁的项目代码,项目进行分层,保证能运行。
使用mysql数据库的struts2 spring hibernate整合简单示例,也可参考我的:“struts2 spring hibernate整合要点、注意点”这篇文档来配置
1. 导入的包 <br>l struts2的五个基础包:commons-logging-1.1.jar; <br>freemarker-2.3.8.jar; <br>ognl-2.6.9.jar; <br>struts-core-2.0.6.jar; <br>xwork-2.0.0.jar. <br>l spring的核心包:...