論壇中用Struts+Spring的人不少,以前的帖子也有問(wèn)過(guò)Struts+Spring的整合方式。前面的帖子中ReadOnly老大曾經(jīng)提到過(guò)Spring2.0新增加的一個(gè)整合方式。今天簡(jiǎn)單把這幾種整合方式小結(jié)一下。
在這之前,別忘了用一下Google大法,一般早有人會(huì)對(duì)類似的問(wèn)題做過(guò)回答,果然,在ibm developworks上有一篇文章,一下子涵蓋了三種整合方式,有興趣的xdjm可以參考下面的鏈接:http://www-128.ibm.com/developerworks/cn/java/j-sr2.html。
下面著重談一下Spring2.0新增的一個(gè)整個(gè)方式,我感覺(jué)挺不錯(cuò),可以完全將Struts的配置和Spring的配置分離。具體步驟如下:
1. 編寫Spring的配置文件applicationContext.xml,簡(jiǎn)單起見(jiàn),僅僅定義一個(gè)Service對(duì)象。
引用
<?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配置文件沒(méi)有任何分別。
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;
}
看上去你好像啥都沒(méi)做,而事實(shí)上,注入工作已經(jīng)由AutowiringRequestProcessor自動(dòng)完成。
4. 編寫web.xml進(jìn)行測(cè)試。
引用
?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>
最后,啟動(dòng)Jetty進(jìn)行測(cè)試,順利運(yùn)行通過(guò)!
看上去如此簡(jiǎn)單,配置起來(lái)也沒(méi)有什么很特別的地方,只是按照常規(guī)來(lái)寫你的Spring和Struts的配置文件就好了。
不過(guò)在這里還是說(shuō)一下其中的要注意兩個(gè)問(wèn)題:
1. 這種autowire的注入支持兩種不同的方式,分別是byName和byType,默認(rèn)是byType。我想這對(duì)于絕大多數(shù)開(kāi)發(fā)者來(lái)說(shuō)是夠了。
2. 鑒于在http://www.javaeye.com/topic/15057中所提到的OpenSessionInView模式的失效的問(wèn)題。我仔細(xì)看了一下Spring的源碼。對(duì)于這種autowire的整合方式,不推薦在struts-config.xml文件中配置ContextLoaderPlugIn,而是采用web.xml中的ContextLoaderListener來(lái)加載Spring的初始化配置。否則,你的OpenSessionInView模式可能會(huì)失效。