我们在集成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
           * Generated by MyEclipse Struts
           * Template path: templates/java/JavaClass.vtl
           * Template path: templates/java/JavaClass.vtl
           */
           
        
        
          */
        
      
      
         package
      
      
         action;
      
      
        package
      
      
         action;
         
         import
      
      
         javax.servlet.http.HttpServletRequest;
      
      
        import
      
      
         javax.servlet.http.HttpServletRequest;
         import
      
      
         javax.servlet.http.HttpServletResponse;
      
      
        import
      
      
         javax.servlet.http.HttpServletResponse;
         
         import
      
      
         org.apache.struts.action.Action;
      
      
        import
      
      
         org.apache.struts.action.Action;
         import
      
      
         org.apache.struts.action.ActionForm;
      
      
        import
      
      
         org.apache.struts.action.ActionForm;
         import
      
      
         org.apache.struts.action.ActionForward;
      
      
        import
      
      
         org.apache.struts.action.ActionForward;
         import
      
      
         org.apache.struts.action.ActionMapping;
      
      
        import
      
      
         org.apache.struts.action.ActionMapping;
         
         import
      
      
         Service.StudentService;
      
      
        import
      
      
         Service.StudentService;
         
         
         public
      
      
         
      
      
        class
      
      
         ListStudentActionAction 
      
      
        extends
      
      
         Action 
      
      
      
        
          {
        
      
      
        public
      
      
         
      
      
        class
      
      
         ListStudentActionAction 
      
      
        extends
      
      
         Action 
      
      
      
        
          {
           private
        
        
           StudentService studentService;
              
        
        
          private
        
        
           StudentService studentService;
           public
        
        
           ActionForward execute(ActionMapping mapping, ActionForm form,
              
        
        
          public
        
        
           ActionForward execute(ActionMapping mapping, ActionForm form,
           HttpServletRequest request, HttpServletResponse response) 
        
        
        
          
            {
          
                      HttpServletRequest request, HttpServletResponse response) 
        
        
        
          
            {
             
             System.out.println(
          
          
            this
          
          
            .studentService.getStudent().getName()
          
          
            +
          
          
            "
          
          
            --
          
          
            "
          
          
            +
          
          
            this
          
          
            .studentService.getStudent().getSex());
                    System.out.println(
          
          
            this
          
          
            .studentService.getStudent().getName()
          
          
            +
          
          
            "
          
          
            --
          
          
            "
          
          
            +
          
          
            this
          
          
            .studentService.getStudent().getSex());
             return
          
          
             
          
          
            null
          
          
            ;
                    
          
          
            return
          
          
             
          
          
            null
          
          
            ;
             }
                }
          
        
        
           //
        
        
          通过spring注入service
              
        
        
          //
        
        
          通过spring注入service
        
        
           public
        
        
           
        
        
          void
        
        
           setStudentService(StudentService studentService) 
        
        
        
          
            {
          
        
        
              
        
        
          public
        
        
           
        
        
          void
        
        
           setStudentService(StudentService studentService) 
        
        
        
          
            {
             this
          
          
            .studentService 
          
          
            =
          
          
             studentService;
                    
          
          
            this
          
          
            .studentService 
          
          
            =
          
          
             studentService;
             }
                }
          
        
        
           
              
           
              
           }
          }
        
      
    
Service:
 package Service;
      
        package Service;
         
         import Model.Student;
        import Model.Student;
         
         public class StudentService {
        public class StudentService {
         
         public Student getStudent(){
          public Student getStudent(){
         return new Student("name","sex");
             return new Student("name","sex");  
         }
          }
         }
        }
         
      
    
applicationContext.xml
配置真正的strutsAction,并把service类注入
 <?
      
      
        xml version="1.0" encoding="UTF-8"
      
      
        ?>
      
        <?
      
      
        xml version="1.0" encoding="UTF-8"
      
      
        ?>
      
      
         <
      
      
        beans
      
      
        <
      
      
        beans
         xmlns
      
      
        ="http://www.springframework.org/schema/beans"
            
      
      
        xmlns
      
      
        ="http://www.springframework.org/schema/beans"
      
      
         xmlns:xsi
      
      
        ="http://www.w3.org/2001/XMLSchema-instance"
            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"
      
      
        >
            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"
      
      
        >
      
      
        <
      
      
        bean 
      
      
        name
      
      
        ="/listStudentAction"
      
      
         class
      
      
        ="action.ListStudentActionAction"
      
      
        >
      
      
         <
      
      
        property 
      
      
        name
      
      
        ="studentService"
      
      
        >
          
      
      
        <
      
      
        property 
      
      
        name
      
      
        ="studentService"
      
      
        >
      
      
         <
      
      
        bean 
      
      
        class
      
      
        ="Service.StudentService"
      
      
        />
            
      
      
        <
      
      
        bean 
      
      
        class
      
      
        ="Service.StudentService"
      
      
        />
      
      
         </
      
      
        property
      
      
        >
          
      
      
        </
      
      
        property
      
      
        >
      
      
         </
      
      
        bean
      
      
        >
      
      
        </
      
      
        bean
      
      
        >
      
      
         </
      
      
        beans
      
      
        >
      
      
        </
      
      
        beans
      
      
        >
      
    
struts-config.xml
 <?
      
      
        xml version="1.0" encoding="UTF-8"
      
      
        ?>
      
        <?
      
      
        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"
      
      
        >
      
      
        <!
      
      
        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
      
      
        >
      
      
        <
      
      
        struts-config
      
      
        >
      
      
         <
      
      
        data-sources 
      
      
        />
          
      
      
        <
      
      
        data-sources 
      
      
        />
      
      
         <
      
      
        form-beans 
      
      
        />
          
      
      
        <
      
      
        form-beans 
      
      
        />
      
      
         <
      
      
        global-exceptions 
      
      
        />
          
      
      
        <
      
      
        global-exceptions 
      
      
        />
      
      
         <
      
      
        global-forwards 
      
      
        />
          
      
      
        <
      
      
        global-forwards 
      
      
        />
      
      
         <
      
      
        action-mappings 
      
      
        >
          
      
      
        <
      
      
        action-mappings 
      
      
        >
      
      
         <!--
      
      
         不再真正的action,而注册spring的代理action
           
      
      
        <!--
      
      
         不再真正的action,而注册spring的代理action
         <action path="/listStudentAction" type="action.ListStudentActionAction" />
            <action path="/listStudentAction" type="action.ListStudentActionAction" />
         -->
              
      
      
        -->
      
      
         <
      
      
        action 
      
      
        path
      
      
        ="/listStudentAction"
      
      
         type
      
      
        ="org.springframework.web.struts.DelegatingActionProxy"
      
      
         
      
      
        />
            
      
      
        <
      
      
        action 
      
      
        path
      
      
        ="/listStudentAction"
      
      
         type
      
      
        ="org.springframework.web.struts.DelegatingActionProxy"
      
      
         
      
      
        />
      
      
         
         
         </
      
      
        action-mappings
      
      
        >
          
      
      
        </
      
      
        action-mappings
      
      
        >
      
      
         
         <
      
      
        message-resources 
      
      
        parameter
      
      
        ="ApplicationResources"
      
      
         
      
      
        />
           
      
      
        <
      
      
        message-resources 
      
      
        parameter
      
      
        ="ApplicationResources"
      
      
         
      
      
        />
      
      
         
         
         
         
         
         <
      
      
        plug-in 
      
      
        className
      
      
        ="org.springframework.web.struts.ContextLoaderPlugIn"
      
      
        >
           
      
      
        <
      
      
        plug-in 
      
      
        className
      
      
        ="org.springframework.web.struts.ContextLoaderPlugIn"
      
      
        >
      
      
         <
      
      
        set-property 
      
      
        property
      
      
        ="contextConfigLocation"
                
      
      
        <
      
      
        set-property 
      
      
        property
      
      
        ="contextConfigLocation"
      
      
         value
      
      
        ="/WEB-INF/classes/applicationContext-service.xml"
      
      
        />
                    value
      
      
        ="/WEB-INF/classes/applicationContext-service.xml"
      
      
        />
      
      
         </
      
      
        plug-in
      
      
        >
            
      
      
        </
      
      
        plug-in
      
      
        >
      
      
         </
      
      
        struts-config
      
      
        >
      
      
        </
      
      
        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 
      
      
        processorClass
      
      
        ="org.springframework.web.struts.DelegatingRequestProcessor"
      
      
        >
      
      
         </
      
      
        controller
      
      
        >
         
      
      
        </
      
      
        controller
      
      
        >
      
    
然后,修改我们的path定义位 <action path="/listStudentAction" type="action.ListStudentActionAction"/>
这样,又和我们单独使用struts的时候一样了,但内部还是让spring取代理我们的真正的action
需要说明的是,这里的type其实是个摆设,完全可以使用 <action path="/listStudentAction"/>,写上是为了解决我们上面提到的“不够直观的”的问题


 
       HttpServletRequest request, HttpServletResponse response)
                      HttpServletRequest request, HttpServletResponse response) 
         
					 
					