Spring与Struts1整合的三种方法总结

1.使用Spring 的 ActionSupport 。
2.使用Spring 的 DelegatingRequestProcessor 类。
3.全权委托。

无论用那种方法来整合第一步就是要为struts来装载spring的应用环境。 就是在> <set-property></plug-in>

spring 的配置文件被作为参数配置进来。这样可以省略对web.xml 文件中的配置。确保你的applicationContext.xml 在WEB-INF目录下面

1,使用Spring的ActionSupport .
Spring 的ActionSupport 继承至>

  1. public class LoginAction extends> (LoginForm)> this.getWebApplicationContext();
  2. LoginDao> (LoginDao)> new User();
  3. u.setName(loginForm.getName());
  4. u.setPwd(loginForm.getPwd());
  5. if(dao.checkLogin(u)){
  6. return> DelegatingRequestProcessor 类
    DelegatingRequestProcessor  继承自><controller>
       
       
    1. public class LoginAction extends Action {
    2. //利用spring来注入这个对象。
    3. private LoginDao>> (LoginForm)>
    4. //LoginDao>
    5. User> new User();
    6. > <property><ref local=”loginDao”/>
      </property>
      </bean>
      </beans>

      这里name=”/login”与struts中的path匹配class=”com.cao.struts.action.LoginAction”与struts中的type匹配还要为LoginAction提供必要的setXXX方法。获得ApplicationCotext和依赖注入的工作都在DelegatingRequestProcessor中完成。
      3,全权委托:
      Action 的创建和对象的依赖注入全部由IOC容器来完成。使用Spring的DelegatingAcionProxy来帮助实现代理的工作
      org.springframework.web.struts.DelegatingActiongProxy继承于org.apache.struts.action.Action.

      全权委托的配置方式同 方式 2 类似 (applcationContext.xml文件的配置和 Action类的实现方式相同)。

      1, <action>中> name=”loginForm”
      path=”/login”
      scope=”request”
      type=”org.springframework.web.struts.DelegatingActionProxy” >
      <forward><forward> </action>
      </action-mappings>
      <message-resources><plug-in> <set-property></plug-in>
      </struts-config>

      2, 去掉struts-config.xml中 <controller >

      三种整和方式中我们优先选用 全权委托的方式。
      理由:
      1,第一种使得过多的耦合了Spring和Action .
      2,RequestProcessor类已经被代理 如果要再实现自己的实现方式(如:编码处理)怕有点麻烦。

      总结一下:
      整合工作中的步骤:
      1.修改struts-config.xml
      2. 配置applicationContext.xml
      3.为Action添加get/set方法 来获得依赖注入的功能。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值