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

通过Spring的ActionSupport类来整合Spring与Struts1。

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>

3.配置struts.xml,在其中导入spirng的配置文件,从而使struts中的Action可以通过ActionSupport的getWebApplicationContext()方法,获取spring的ApplicationContext对象。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>

	

	<!-- 导入spring配置信息 -->

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

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

	</plug-in>

</struts-config>

 4.导入spring.xml配置文件到WEB-INF下,我试过将其导入到其他的classpath下,结果报错,原因未知。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>

</beans>

 5.修改创建Action时,试Action继承于类ActionSupport,示例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.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 ActionSupport{



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

    

    @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");

        ApplicationContext ctx = getWebApplicationContext();

        HelloWorldService service = (HelloWorldService)ctx.getBean("helloWorldService");

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

        request.setAttribute("helloWorld", msg);

        return mapping.findForward("show");

    }



}
HelloWorldAction

 

注:该整合方法方便,但Struts的Action与Spring的耦合在一起,并且Struts的Action不在Spring的控制之中,这样如果更换别的框架或者想使用Spring的AOP都是比较困难的,而且如果是多个动作在一个Action中,在这种方式就无能为力了。

你可能感兴趣的:(struts1)