浏览 13344 次
锁定老帖子 主题:Struts与Spring整合的几种方法
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2006-09-12
在这之前,别忘了用一下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进行测试,顺利运行通过! 看上去如此简单,配置起来也没有什么很特别的地方,只是按照常规来写你的Spring和Struts的配置文件就好了。 不过在这里还是说一下其中的要注意两个问题: 1. 这种autowire的注入支持两种不同的方式,分别是byName和byType,默认是byType。我想这对于绝大多数开发者来说是够了。 2. 鉴于在http://www.iteye.com/topic/15057中所提到的OpenSessionInView模式的失效的问题。我仔细看了一下Spring的源码。对于这种autowire的整合方式,不推荐在struts-config.xml文件中配置ContextLoaderPlugIn,而是采用web.xml中的ContextLoaderListener来加载Spring的初始化配置。否则,你的OpenSessionInView模式可能会失效。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2006-09-12
帖子的代码格式有问题啊,Robbin老大何时能修复啊。
|
|
返回顶楼 | |
发表时间:2006-09-12
好东西,不愧拥有两个钻石,先代表大家谢谢你了!
|
|
返回顶楼 | |
发表时间:2006-09-12
ibm那个网页引用已经没用了~~
|
|
返回顶楼 | |
发表时间:2006-09-12
ibm的网站有问题,你要多刷新两次。我当时是从ibm developworks进去的。
|
|
返回顶楼 | |
发表时间:2006-09-12
spring对struts的支持越来越强大,简单了许多。
但好像还是习惯在配置文件里注入一下或在action里自己得到service实例更踏实一些, boysean 写道 ibm那个网页引用已经没用了~~
什么意思? 那个连接后面还有个.html呢, |
|
返回顶楼 | |
发表时间:2006-09-12
用的是Autowrie。然后覆盖了Struts默认的RequestProcessor。WebWork2+Spring的时候也用的这种方式。
这种方式限制就在于名字遵循约定/同一个接口只能有一个实现。但是能够减少配置。项目很大了可能是需要重新度量优势/劣势。 |
|
返回顶楼 | |
发表时间:2006-09-12
好东西,不过是2.0的,看样子要把这个src单独拿出来用在项目中了,谢谢楼主咯
|
|
返回顶楼 | |
发表时间:2006-09-12
可以创建一个BaseAction类...把Setter都放到里面去
btw:顺便试试新版本的推精功能hoho |
|
返回顶楼 | |