标签:style blog http color io os 使用 java ar
Spring提供了DelegatingActionProxy类,用来代理Struts中的Action,负责在Spring配置文档中查找对应的Action映射,从而把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>
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="org.springframework.web.struts.DelegatingActionProxy" 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插件 --> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/spring.xml"></set-property> </plug-in> </struts-config>
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>
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.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; 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"); } }
注:DelegatingActionProxy类比DelegatingRequestProcessor的优势在于,它代理的是Action,而不是RequestProcessor,即使用户自定义了RequestProcessor也无所谓。
Spring与Struts1整合方式之通过Spring的DelegatingActionProxy类
标签:style blog http color io os 使用 java ar
原文地址:http://www.cnblogs.com/ScorchingSun/p/3991089.html