Struts与Spring整合的几种方法

xiaoxiao2022-06-12  34

【转】struts与spring 2.0新增整合方式 论坛中用 StrutsSpring的人不少,以前的帖子也有问过 StrutsSpring的整合方式。前面的帖子中ReadOnly老大曾经提到过Spring2.0新增加的一个整合方式。今天简单把这几种整合方式小结一下。 在这之前,别忘了用一下Google大法,一般早有人会对类似的问题做过回答,果然,在ibm developworks上有一篇文章,一下子涵盖了三种整合方式,有兴趣的xdjm可以参考下面的链接:http://www-128.ibm.com/developerworks/cn/java/j-sr2.html。 下面着重谈一下Spring2.0新增的一个整个方式,我感觉挺不错,可以完全将 Struts的配置和 Spring的配置分离。具体步骤如下: 1. 编写 Spring的配置文件applicationContext.xml,简单起见,仅仅定义一个Service对象。 引用 <?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="userService" class="com.bearingpoint.gdc.zero.service.impl.UserServiceImpl" /> </beans> 这看上去就和普通的 Spring配置文件没有任何分别。 2. 编写 Struts的配置文件 struts-config.xml,注意其中的controller的配置,用到了Spring2.0的新特性。 引用 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/ struts/dtds/ struts-config_1_2.dtd" > < struts-config> <action-mappings> <action path="/addUser" type="com.bearingpoint.gdc.zero.action.user.AddUser" scope="request" > <forward name="success" path="/index.jsp" /> </action> </action-mappings> <controller processorClass="org.springframework.web. struts.AutowiringRequestProcessor" /> </ struts-config> 3. 然后为你的 Struts的Action注入你需要的Service 引用 private UserService userService; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { User user = new User(); userService.addUser(user); return mapping.findForward("success"); } /** * @param userService *            The userService to set. */ public void setUserService(UserService userService) { this.userService = userService; } 看上去你好像啥都没做,而事实上,注入工作已经由AutowiringRequestProcessor自动完成。 4. 编写web.xml进行测试。 引用 ?xml version="1.0" ?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app>     <context-param>         <param-name>contextConfigLocation</param-name>         <param-value>/WEB-INF/classes/applicationContext.xml</param-value>     </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name> struts</servlet-name> <servlet-class>org.apache. struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/classes/ struts-config.xml</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>validate</param-name> <param-value>true</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name> struts</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>     <welcome-file-list>         <welcome-file>index.jsp</welcome-file>     </welcome-file-list> </web-app> 最后,启动Jetty进行测试,顺利运行通过! 看上去如此简单,配置起来也没有什么很特别的地方,只是按照常规来写你的 SpringStruts的配置文件就好了。 不过在这里还是说一下其中的要注意两个问题: 1. 这种autowire的注入支持两种不同的方式,分别是byName和byType,默认是byType。我想这对于绝大多数开发者来说是够了。 2. 鉴于在http://www.iteye.com/topic/15057中所提到的OpenSessionInView模式的失效的问题。我仔细看了一下 Spring的源码。对于这种autowire的整合方式,不推荐在 struts-config.xml文件中配置ContextLoaderPlugIn,而是采用web.xml中的ContextLoaderListener来加载 Spring的初始化配置。否则,你的OpenSessionInView模式可能会失效。 相关资源:敏捷开发V1.0.pptx
转载请注明原文地址: https://www.6miu.com/read-4932941.html

最新回复(0)