`

Struts与Spring整合的第四种方法

阅读更多
论坛中用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>
<!--如果你采用byName方式,去掉下面的注释--->
 <!--context-param>
      <param-name>autowire</param-name>
      <param-value>byName</param-value>
  </context-param-->
<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。我想这对于绝大多数开发者来说是够了。 对于byName的形式,就在Web.xml中添加
<context-param>
      <param-name>autowire</param-name>
      <param-value>byName</param-value>
  </context-param>
即可
2. 鉴于在http://www.iteye.com/topic/15057中所提到的OpenSessionInView模式的失效的问题。我仔细看了一下Spring的源码。对于这种autowire的整合方式,不推荐在struts-config.xml文件中配置ContextLoaderPlugIn,而是采用web.xml中的ContextLoaderListener来加载Spring的初始化配置。否则,你的OpenSessionInView模式可能会失效。
3.可以创建一个BaseAction类...把Setter都放到里面去
4.对于Aciton 的execute方法以外的方法调用,spring将不能完成自动注入。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics