在这的前别忘了用下Google大法般早有人会对类似问题做过回答果然在ibm developworks上有篇文章下子涵盖了 3种整合方式有兴趣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" ="com.bearingpo.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.bearingpo.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. 然后为你StrutsAction注入你需要Service
引用
private UserService userService;
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
User user = User;
userService.addUser(user);
mapping.findForward("success");
}
/**
* @param userService
* The userService to .
*/
public void UserService(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/es/applicationContext.xml</param-value>
</context-param>
<listener>
<listener->org.springframework.web.context.ContextLoaderListener</listener->
</listener>
<servlet>
<servlet-name>struts</servlet-name>
<servlet->org.apache.struts.action.ActionServlet</servlet->
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/es/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模式可能会失效
最新评论