低耦合的Struts集成Spring的实例

系统 1550 0

我们在集成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"/>,写上是为了解决我们上面提到的“不够直观的”的问题



低耦合的Struts集成Spring的实例


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论