低耦合的Struts集成Spring的实例

我们在集成Spring和struts的时候,往往习惯于使用spring提供的ActionSupport,然后使用getWebApplicationContext()方法获得spring的bean,这样固然方便,但有一个弊端,就是我们的struts action依赖了spring的api,增加了耦合,现在什么都流行高内聚,低耦合,spring为我们提供了代理的Struts action,这样,我们在struts-config.xml不再为path设置真正的action,而是设计spring的代理Action,然后由spring的代理action,去寻找在spring bean 容器中的真正的action,这样,我们的action是一个完全没有依赖于spring的action ,具体实现请看以下代码:

 

Action:

 

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 
*/

package  action;

import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;

import  org.apache.struts.action.Action;
import  org.apache.struts.action.ActionForm;
import  org.apache.struts.action.ActionForward;
import  org.apache.struts.action.ActionMapping;

import  Service.StudentService;


public   class  ListStudentActionAction  extends  Action  {
    
private StudentService studentService;
    
public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) 
{

        System.out.println(
this.studentService.getStudent().getName()+"--"+this.studentService.getStudent().getSex());
        
return null;
    }

    
//通过spring注入service
    public void setStudentService(StudentService studentService) {
        
this.studentService = studentService;
    }

    
    
}

 

Service:

 

package Service;

import Model.Student;

public class StudentService {

  public Student getStudent(){
     return new Student("name","sex");  
  }
}

 

applicationContext.xml

配置真正的strutsAction,并把service类注入

<? xml version="1.0" encoding="UTF-8" ?>
< beans
    
xmlns ="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >

< bean  name ="/listStudentAction"  class ="action.ListStudentActionAction" >
  
< property  name ="studentService" >
    
< bean  class ="Service.StudentService" />
  
</ property >
</ bean >
</ beans >

 

struts-config.xml

 

<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd" >

< struts-config >
  
< data-sources  />
  
< form-beans  />
  
< global-exceptions  />
  
< global-forwards  />
  
< action-mappings  >
   
<!--  不再真正的action,而注册spring的代理action
    <action path="/listStudentAction" type="action.ListStudentActionAction" />
      
-->
    
< action  path ="/listStudentAction"  type ="org.springframework.web.struts.DelegatingActionProxy"   />


  
</ action-mappings >

   
< message-resources  parameter ="ApplicationResources"   />


 

   
< plug-in  className ="org.springframework.web.struts.ContextLoaderPlugIn" >
        
< set-property  property ="contextConfigLocation"
            value
="/WEB-INF/classes/applicationContext-service.xml" />
    
</ plug-in >
</ struts-config >

需要说明的是,由于spring dtd规定id不能有"/",所以我们用name定义path,并且,spring bean的name要和struts-config.xml中的path一致

使用DelegatingActionProxy的好处就在于你可以用不用任何spring特定的类编写Struts Action,这个方法也有不足之处,就是不太直观,因为所有路径都映射到同一个类了

对于这种情况,spring也有解决方法,就是使用请求委托

首先,为struts-config.xml增加controller

 

   <!--  使用请求委托  -->
 
< controller  processorClass ="org.springframework.web.struts.DelegatingRequestProcessor" >
 
</ controller >

 

然后,修改我们的path定义位 <action path="/listStudentAction" type="action.ListStudentActionAction"/>

这样,又和我们单独使用struts的时候一样了,但内部还是让spring取代理我们的真正的action

需要说明的是,这里的type其实是个摆设,完全可以使用 <action path="/listStudentAction"/>,写上是为了解决我们上面提到的“不够直观的”的问题

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以直接运行,并对整合springstruts2步骤及需要注意的事项进行类总结 整合springstruts2总结 1.将struts2和spring中的库文件复制到项目下(可以查看WEB-INF\lib目录下的文件) 注意:struts2-spring-plugin-2.3.8.jar和commons-logging.jar文件可以在 struts框架中的找到而spring.jar 在spring框架中 2. 配置struts.objectFactory属性值: 在struts.xml文件中添加 注意:如果有了struts2-spring-plugin-2.3.8.jar文件可以不用配置(本源代码中是没有配置的) 3.配置spring的监听器: 在web.xml中添加 org.springframework.web.context.ContextLoaderListener 4.配置spring的配置文件(可以查看WEB-INF里面的applicationContext.xml文件) 注意:如果是默认,一定要放在WEB-INF中并文件名为applicationContext.xml 5.配置struts中的配置文件 注意:如果让spring来创建对象可以将action中的class属性中的值为springbean 中的id值,如果想让struts来创建可以直接指定类 附加: 整合原理 : Struts2与Spring集成要用到Spring插件包struts2-spring-plugin-x-x-x.jar,这个包是同Struts2一起发布的。Spring插件是通过覆盖(override)Struts2的ObjectFactory来增强核心框架对象的创建。当创建一个对象的时候,它会用Struts2配置文件中的class属性去和Spring配置文件中的id属性进行关联,如果能找到,则由Spring创建,否则由Struts 2框架自身创建,然后由Spring来装配。Spring插件具体有如下几个作用: — 允许Spring创建Action、Interceptror和Result。 — 由Struts创建的对象能够被Spring装配。 — 如果没有使用Spring ObjectFactory,提供了2个拦截器来自动装配action

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值