`
ruilin521314
  • 浏览: 883592 次
文章分类
社区版块
存档分类
最新评论

Spring 整合struts 详解

 
阅读更多

原文:http://www.javaeedev.com/blog/article.jspx?articleId=ff8080811ec61704011ece9640f10003

然现在J2EE开发中,对于struts2(webwork)框架已经出现有一段时间了,但是对于struts1.0系列,在当前java开源框架组合中依然可能是首选。在项目中采用ssh的组合(ssh2还有一段路要走),可是说是当前的一种主流,对于spring与struts或者是hibernate的整合,对于我们来说,还是要有必要搞清楚,这样,对于整合框架提高开发效率来说,我们会少走很多弯路!下面请随阿堂一起来学习!

Spring虽然也提供了自已的mvc组件,但一来spring的mvc组件过于繁琐,二来是Struts的用户众多,,因此,很多项目还是选择使用spring整合struts框架,而且spring可以无缝的整合strtus框架,二者结合成一个更实际的j2ee开发平台
使用spring的web应用时,不用手动创建spring容器,而是通过配置文件声明式地创建spring容器,因此,在web应用中创建spring容器有如下两种方式
1.直接在web.xml文件中配置spring容器
2.利用第三方MVC框架的扩展点,创建spring容器

一.直接在web.xml文件中配置spring容器
这种创建spring容器的方式更加常见。为了让spring容器随web的应用的启动而自动启动,有如下两种方法
1.利用ServletContextListener实现
2.采用load-on-startup Servlet实现

对于利用ServletContextListener实现方式,操作及说明如下
spring提供ServletContextListener的一个实现类ContextLoadListener,该类可以作为Listener使用,会在创建时自动查找web-inf/applicationContext.xml文件,因此,如果只有一个配置文件,并且名为applicationContext.xml,只需要在web.xml文件中加入如下配置即可
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderLister
</listener-class>
</listener>

如果有多个配置文件需要载入,则考虑用<context-param>元素来确定配置文件的文件名。ContextLoadListenter加载时,会查找名为contextConfigLocation的参数。因此,配置context-param时,参数名应该是contextConfigLocation

带多个配置文件的web.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="
http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


<context-param>
<!-- 参数名为contextConfigLocation -->
<param-name>contextConfigLocation</param-name>
<!-- 配置多个文件之间,以","隔开 -->
<param-value>/WEB-INF/actionContext.xml,/WEB-INF/appContext.xml,/WEB-INF/daoContext.xml</param-value>
</context-param>


<!-- 采用listener创建ApplicationContext实例 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderLister
</listener-class>
</listener>

</web-app>

如是没有通过contextConfigLocation指定配置文件,spring会自动查找applicationContext.xml文件;如果有contextConfigLocation,则利用该参数确定的配置文件,如果无法找到合适的配置文件,spring将无法正常初始化
spring根据bean定义创建WebApplicationContext对象,并将其保存在web应用的ServletContext中。大部分情况下,应用的Bean无须感受到ApplicationContext的存在,只要利用ApplicationContext的IOC容器
如果需要在应用中获取ApplicationContext实例,可以通过如下代码获取
//获取当前web应用的spring的容器
WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(ServletContext);


对于利用load-on-startup Servlet实现方式,操作及说明如下
spring提供了load-on-startup Servlet的一个特殊Servlet类ContextLoadServlet该Servlet在启动时,会在创建时自动查找web-inf/applicationContext.xml文件
当然,为了让ContextLoadServlet随应用的启动而启动,应将Servlet配置成load-on-startup的Servlet,load-on-startup的值小一点比较合适,这样可以保证ApplicationContext更快的初始化
如果只有一个配置文件,并且名为ApplictionContext.xml文件,在web.xml文件中增加如下一段即可
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet
该Servlet用于提供“后台”服务,主要用于创建spring容器,无须响应客户请求,因此无须配置servlet-mapping
如果有多个配置文件,一样使用<context-poaram>

<context-param>
<!-- 参数名为contextConfigLocation -->
<param-name>contextConfigLocation</param-name>
<!-- 配置多个文件之间,以","隔开 -->
<param-value>/WEB-INF/actionContext.xml,/WEB-INF/appContext.xml,/WEB-INF/daoContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet

事实上,不管是ContextLoaderServlet还是ContextLoadListener,都依赖于ContextLoader创建 ApplicationContext.之间的区别不是它们本身引起的,而是由于Servlet规范,Listener比Servlet优先加载。因此,Listener比Servlet创建 ContextLoadListener的时机更早

到底需要使用Listener,还是使用load-on-startup Servlet来创建Spring容器呢?通常推荐使用Listener来创建spring容器。但Listener是Servlet2.3以上才支持的标准,因此必须web容器支持Listener才可使用Listener

二.利用第三方MVC框架的扩展点,创建spring容器
Struts有一个扩展点PlugIn,spring正是利用了PlugIn这个扩展点,从而提供了与Struts的整合。。spring提供了PlugIn的实现类org.springframework.web.struts.ContextLoadPlugIn,这个实现类可作为struts的PlugIn配置,Struts框架启动时,将自动创建Spring容器
为了利用struts的PlugIn创建Spring容器,只需要在struts配置文件struts-config.xml中增加如下片段即可
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/actionContext.xml,/WEB-INF/appContext.xml,/WEB-INF/daoContext.xml" />
</plug-in>

其中,指定contextConfigLocation属性值时,即可以指定一个spring配置文件的位置,可以指定多个spring配置文件的位置

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics