struts2中集成spring

17 篇文章 0 订阅
5 篇文章 0 订阅

struts的包就不列了,只列下spring中用到的包

spring:spring-asm-3.0.5.RELEASE.jar,spring-beans-3.0.5.RELEASE.jar,spring-context-3.0.5.RELEASE.jar,spring-core-3.0.5.RELEASE.jar,spring-expression-3.0.5.RELEASE.jar,spring-web-3.0.5.RELEASE.jar,struts2-spring-plugin-2.3.16.3.jar

sturts.xml文件只需添加

<constant name="struts.objectFactory" value="spring" />

,其它不用改。

web.xml中只需添加

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
	</context-param>
其它不用改。

struts.xml中包含的一个文件<include file="testAjax.xml"/>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<package name="ajax" namespace="/ajax" extends="json-default">
		<action name="TestAjax_*" method="{1}" class="a">
			<result name="success" type="json">
				<param name="data">result</param>
			</result>
		</action>
	</package>
</struts>
applicationContext.xml

<pre name="code" class="html"><?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="a" class="example.TestAjax"/>
</beans>

 

需要注意的地方是testAjax.xml文件中action中的class属性跟applicationContext.xml文件中的id属性是配对的,如果applicationContext.xml中没有对应的就让struts去创建,有对应的就由spring创建。

当创建一个对象的时候,它会用Struts2配置文件中的class属性去和Spring配置文件中的id属性进行关联,如果能找到,则由Spring创建,否则由Struts 2框架自身创建,然后由Spring来装配。

个人觉得struts中为了用spring多加了包,多加了配置,单独使用struts的话就少配置一些内容,特别感觉struts.xml和applicationContext.xml很多重复的。

spring-plugin.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值