posts - 7,  comments - 58,  trackbacks - 0


Spring与Struts集成开发

最近喜欢将所学的东西理顺一下,且发现写blog可以达成这目的。
那就来整理一下我对Spring与Struts集成开发的一些想法。

首先确认系统的结构为三层的B/S模式结构,如下图:

在图中看出,Spring和Struts集成开发中,Spring在业务逻辑层被使用(集成)。因为Spring框架的依赖注入,AOP及可声明的事务管理方面的技术优势,使得用Spring来管理业务实体,实体之间的依赖关系,业务逻辑服务接口变得简单且可配置。至此我们要清楚:Spring和Struts集成开发中,Spring在业务逻辑层被使用(集成)。

清楚Struts和Spring在系统结构中分别充当的角色后,接下来要讨论:如何实现集成
1、使用Spring的ActionSurpert类集成Struts。
org.springframework.web.struts.ActionSurpert是一个继承org.apache.struts.action.Action的类,简要代码如下:
public abstract class ActionSurpert extends Action {
     private WebApplicationContext webApplicationContext;
     public void setServlet(ActionServlet actionServlet) {//当容器实例化此Action时被容器调用
           surper.setServlet(actionServlet);
           if(actionServlet != null) {
              this.webApplicationContext = initWebApplicationContext(actionServlet);//获取webApplicationContext
               //........
           }else{
             //.......
           }           
     } 
     
     //通过getXXX()方法获取   webApplicationContext 对象
     protected final WebApplictionContext getWebApplicationContext() {
            return this.webApplicationContext;
    }
}
通过上述代码可以看出,所有继承了ActionSupport类的Action将可以通过WebApplicationContext对象的getBean(beanKey)方法获得Spring配置文件中定义的各种Bean。
WebApplicationContext要由Web Server来加载,有两种方法:
1、通过org.springframework.web.struts.ContextLoaderPlugIn加载,ContextLoaderPlugIn是个插件,需要在Struts配置文件中配置。
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
   <set-property property = "contextConfigLocation" value="/WEB-INF/applicationContext.xml"></set-property>
</plug-in>
2、在web.xml文件中加载
<context-param>
  <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
 <servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-name>
<load-on-startup>1</load-on-startup>
</servlet>
举个简单的例子说明一下相关配置信息:
假定有ExampleAction,ExampleBean,ExampleService这几个类,它们工作流程是:
用户请求ExampleAction,ExampleAction调用ExampleService的接口方法对ExampleBean进行相关的操作。
1、applicationContext.xml中配置相关的bean信息
<beans>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName">
   <value>com.mysql.jdbc.Driver</value>
  </property>
  <property name="url">
   <value>jdbc:mysql://localhost:3306/ssh?useUnicode=true&amp;characterEncoding=UTF-8</value>
  </property>
  <property name="username">
   <value>root</value>
  </property>
  <property name="password">
   <value>root</value>
  </property>
 </bean>
<bean id="exampleBean" class="org.mypackge.beans.ExampleBean"/>
<bean id="exampleBeanService" class="org.mypackge.services.ExampleService"/>@
</beans>
通过这样配置后,在ExampleAction中可以用getWebApplicationContext() 获得webApplicationContext对象,然后通过
webApplicationContext的getBean(beanKey)方法获得相应的bean进行业务处理。标了红色的"beanKey"就是applicationContext.xml中<bean>元素定义的bean id,如:webApplicationContext.getBean("exampleBeanService")。@

当然,ExampleAction还要在stuts-config.xml配置文件中配置,这里不作介绍。
2、使用Spring的Action代理集成Struts
这种集成方式的核心思想是,将Struts的配置文件中的所有Action的type属性设为org.springframwork.web.struts.DelegationActionProxy。当用户请求Action时,就执行这代理,代理会在Spring应用上下文中找到真正的Action,然后交给它处理用户的请求。而真正用于处理用户请求的Action的配置放在了Spring的配置文件中。这样,Struts中的Action以及其依赖关系就可以用Spring容器来管理,比如将业务逻辑对象注入到Action中,供其使用。简单片段<bean name="/exampleAction" class="org.myproj.struts.actions.ExampleAction">
....
</bean>
说明:用name属性而不是用id来标识这个Bean,Spring不允许ID中出现"/",而name可以;"name"属性值要和struts-config.xml文件中相应<action>元素中的path属性值相同(<action path="/exampleAction"),这样Action代理才能找到相应的Action来处理请求。

欢迎讨论,提出宝贵意见。


posted on 2008-03-01 11:08 Sonny Li 阅读(802) 评论(2)  编辑  收藏 所属分类: 框架相关

FeedBack:
# re: Spring与Struts集成开发[未登录]
2008-03-03 11:23 | Paul Lin
不错不错,很好很强大  回复  更多评论
  
# re: Spring与Struts集成开发
2008-05-09 22:36 |
有前途。。很努力哦。。顶。。  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
<2008年3月>
2425262728291
2345678
9101112131415
16171819202122
23242526272829
303112345

常用链接

留言簿(3)

随笔分类

随笔档案

文章分类

相册

收藏夹

博客好友

搜索

  •  

最新评论

阅读排行榜

评论排行榜