原文位置:http://downpour.javaeye.com/blog/24239
論壇中用Struts+Spring的人不少,以前的帖子也有問過Struts+Spring的整合方式。前面的帖子中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進行測試,順利運行通過!
看上去如此簡單,配置起來也沒有什么很特別的地方,只是按照常規來寫你的Spring和Struts的配置文件就好了。
不過在這里還是說一下其中的要注意兩個問題:
1. 這種autowire的注入支持兩種不同的方式,分別是byName和byType,默認是byType。我想這對于絕大多數開發者來說是夠了。
2. 鑒于在http://www.javaeye.com/topic/15057中所提到的OpenSessionInView模式的失效的問題。我仔細看了一下Spring的源碼。對于這種autowire的整合方式,不推薦在struts-config.xml文件中配置ContextLoaderPlugIn,而是采用web.xml中的ContextLoaderListener來加載Spring的初始化配置。否則,你的OpenSessionInView模式可能會失效。
posted on 2008-12-22 13:55
一葉笑天 閱讀(853)
評論(1) 編輯 收藏 所屬分類:
開源技術