struts2整合spring的两种方式

struts2整合spring有两种方式

1、采用自动装配方式,即不在spring中注入action;

好处在于:不必在struts.xml中写了配置文件后,又在spring的配置文件中再写一遍配置

如:

在struts.xml中写

<action name="loginAction" class="com.lk.loginAction" />

 就可以了。

可以写一个BaseAction 里面放入所有的service接口,其他Action继承它就可以根据自动装备的方式

自动注入自己需要的service.

 

2、在spring中注入action

缺点在于:

在struts.xml中写

<action name="loginAction" class="loginAction" />

同时在spring配置文件中需要写

<bean id="loginAction" class="com.lk.loginAction" >

     <property ref="service" />

</bean>

这样看来配置文件要比第一种要繁琐一些。

但是据称这种方式。适合配置AOP的内容。而且struts2官方文档上也是采用的这种配置


贴上一个应用的部分代码(第二种方式):

struts.xml文件

<span style="white-space:pre">	</span><package name="task_mgr" namespace="/taskmgr" extends="CheckLogin">
		...
		<action name="ShowFileResult" class="FileResultManager" method="findFiles">
		 	<result name="error">/Error.html</result>
			<result name="success">/pages/task/fileResult.jsp</result>
		</action>
		...	
	</package>

添加struts2-spring整合的插件:struts2-spring-plugin-2.0.12.jar,如果不使用这个插件,则需要在struts.xml里面配置:

<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />

applicationContext.xml文件

<span style="white-space:pre">	</span><bean id="FileResultManager" class="com.zhanghaipeng.struts.action.File_FileResultManageAction" scope="prototype">
		<property name="fileResultDao" ref="fileResultDao"/>
<span style="white-space:pre">	</span></bean>
fileResult.jsp文件
<form id="form_searchresult" action="/webcmp/taskmgr/ShowFileResult.action">
		<h1 style="width:98%;min-width:400px;" >
	  		<span><img src="/webcmp/images/search.gif" align="middle" alt="" /></span>
	  		<span>检测文件名
	  			<input id="ipt_srcname" type="text" name="searchFile" value="${searchFile}" class="text"/>
	  		</span>
	  		<span>
	  			<input id="ipt_searchSim" type="text" name="searchSim" value="${searchSim}" class="text" style="width: 45px;"/>
	  		</span>
	  		<span>
	  			<input id="ipt_taskid" type="text" name="taskid" value="${taskid}" style="display: none"/>
	  			<input type="button" class="button" value="查询" id="search" name="search" οnclick="search_result()"/>
	  		</span>
		</h1>
</form>



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值