Spring与Struts1整合方式之通过Spring的DelegatingRequestProcessor类

  在Struts中,单人控制器角色的是ActionServlet,当有请求发送至ActionServlet时,ActionServlet的doGet()或doPost()方法会执行RequestProcessor的process()的方法,

  Spring提供了DelegatingRequestProcessor类,用来代替Struts的RequestProcessor类,从而把Struts的Action与Spring分离开,并把Struts的动作置于Spring框架的控制之下。

  1.创建web工程,将需要的资源包导入工程的WEB-INF/lib下,所需要的资源包包括:commons-beanutils- 1.8.0.jar、commons-chain-1.2.jar、commons-digester-1.8.jar、commons- logging-1.0.4.jar、log4j-1.2.14.jar、spring.jar、struts-core-1.3.10.jar、 struts-extras-1.3.10.jar。

  2.配置web.xml,采用struts1分配请求连接,配置如下:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0"

    xmlns="http://java.sun.com/xml/ns/javaee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <!-- 定义Struts1请求处理对象 -->

    <servlet>

        <servlet-name>actionServlet</servlet-name>

        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

         

        <!-- 初始化配置文档 -->

        <init-param>

            <param-name>config</param-name>

            <param-value>struts.xml</param-value>

        </init-param>

        <load-on-startup>1</load-on-startup>

    </servlet>

    <!-- 分配请求,将匹配*.do的请求交给actionServlet处理 -->

    <servlet-mapping>

        <servlet-name>actionServlet</servlet-name>

        <url-pattern>*.do</url-pattern>

    </servlet-mapping>

     

    <display-name></display-name>  

    <welcome-file-list>

        <welcome-file>index.jsp</welcome-file>

    </welcome-file-list>

</web-app>
web.xml

  3.在classpath中导入struts.xml配置文件,文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration1.2//EN"

 "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>

    <form-beans>

        <form-bean name="helloWorld" type="com.luxl.action.HelloWorld"></form-bean>

    </form-beans>

    

    <!-- 配置请求连接 -->

    <action-mappings>

        <action path="/helloWorld" type="com.luxl.action.HelloWorldAction" name="helloWorld">

            <forward name="show" path="/JSP/index.jsp"/>

        </action>

        <!-- 请求的URL对应于<action>中的path属性 -->

        <action path="/newHelloWorld"

                type="org.apache.struts.actions.ForwardAction"

                parameter="/JSP/newHelloWorld.jsp" />

    </action-mappings>

    

    <!-- 定义代理请求处理器,注册ContextLoaderPlugIn插件 -->

    <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />

    <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">

        <set-property property="contextConfigLocation" value="/WEB-INF/spring.xml"></set-property>

    </plug-in>

</struts-config>
struts.xml

  4.在WEB-INF/lib下导入spring.xml配置文件,文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

 "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    <bean id="helloWorldService" class="com.luxl.service.impl.HelloWorldServiceImpl">

    </bean>

    

    <!-- 定义对应于struts.xml中的action的bean,name与action的path对应 -->

    <bean name="/helloWorld" class="com.luxl.action.HelloWorldAction">

        <property name="service">

            <ref bean="helloWorldService" />

        </property>

    </bean>

</beans>
spring.xml

  5.定义Struts的Action类时,使其继承Struts的Action类,实例Action类如下:

package com.luxl.action;



import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;



import org.apache.log4j.Level;

import org.apache.log4j.Logger;

//import org.apache.struts.action.Action;

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 org.apache.struts.actions.DispatchAction;

//import org.springframework.context.ApplicationContext;

//import org.springframework.web.struts.ActionSupport;



import com.luxl.service.HelloWorldService;



public class HelloWorldAction extends Action{



    private Logger logger = Logger.getLogger(this.getClass());

    

    private HelloWorldService service;

    

    public HelloWorldService getService() {

        return service;

    }



    public void setService(HelloWorldService service) {

        this.service = service;

    }



    @Override

    public ActionForward execute(ActionMapping mapping, ActionForm form,

            HttpServletRequest request, HttpServletResponse response)

            throws Exception {

        // TODO Auto-generated method stub

        //通过ApplicationContext获取配置文件

        logger.log(Level.INFO, "receive request");

        String msg = service.addMsg((HelloWorld)form);

        request.setAttribute("helloWorld", msg);

        return mapping.findForward("show");

    }



}
HelloWorldAction.java

 

  注:这种方法的优势在于Action中不再有Spring的代码,这样如果要更换为别的IoC容器,可以很方便的转换而不需要修改代码。但缺点是如果开发人员自定义RequestProcessor,没有使用Struts默认的RequestProcessor,则需要手工整合Spirng和Struts了。

你可能感兴趣的:(struts1)