Struts1.x/2.x 与 Spring集成方式

struts1.x与spring集成方式

方式一:Action交给spring管理,将业务类注入action


struts配置:

<action path="/searchAction"type="org.springframework.web.struts.DelegatingActionProxy" parameter="method"

forward name="success"path="/result.jsp"/>

/action spring配置:

!--这里SearchAction继承了DelegatingActionProxy--

<bean name="/searchAction"class="com.myproject.action.SearchAction"scope="prototype"

<property name="searchService"ref="searchService"/>

</bean>

注意:保持spring配置的bean name和struts配置的action path一致


方式二:请求首先交给ActionServlet,然后给org.springframework.web.struts.DelegatingRequestProcessor,由这个请求处理器根据请求路径从spring容器获取action对象

struts-config.xml action path="/searchAction"!--注意到没有?这里没有配置type属性--

forward name="success"path="/result.jsp"/

/action controller set-property <property="processorClass"value="org.springframework.web.struts.DelegatingRequestProcessor"/>

/controller spring配置:

<bean name="/searchAction"class="com.myproject.action.SearchAction"scope="prototype">

<property name="searchService"ref="searchService"/>

</bean>


方式三:Action不交给spring管理,直接在Action中实例化应用上下文,然后通过getBean("xxx"),获取相应业务对象

struts配置遵照传统方式

action path="/searchAction"type="com.myproject.action.SearchAction"

forward name="success"path="/result.jsp"/

/action private ApplicationContext

 context=WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());

SearchService searchService=(SearchService)context.getBean("searchService");


struts2.x与spring集成

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

Xml代码   收藏代码
  1. <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />  

struts.properties配置

struts.objectFactory=spring#指定struts的action类实例由spring生成

struts-config.xml配置

<action name="search"class="searchAction"method="search">

<result name="success">/result.jsp</result>

 <result name="error">/error.jsp</result>

/action spring配置

<bean id="searchAction"class="com.myproject.action.SearchAction"scope="prototype">

<property name="searchService"ref="searchService"/>

</bean>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值