首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts2跟spring2的整合

2012-10-14 
struts2和spring2的整合?在Struts2中整合Spring的IoC支持是一件十分简单的事情。让我们一步一步来实现:1)复

struts2和spring2的整合

?

在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" ? ? ??

?? ?"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="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 ?

?

?

按照以上步骤做了,但出错、异常信息如下

Messages: springhello?

Action class [springhello] not found?

?

File: com/caucho/loader/DynamicClassLoader.java?

Line number: 1,124?


热点排行