Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > Spring , Struts整合方法
【标  题】:Spring , Struts整合方法
【关键字】:Spring,Struts
【来  源】:http://blog.csdn.net/yixia/archive/2006/09/11/1208728.aspx

Spring , Struts整合方法

Your Ad Here  
关于Spring , Struts结合学习。
一、前言
刚刚接触了日本一个项目,用的框架名称是Northland Framework,主要用到以下部分
Struts、Spring、iBATISVelocityStrutsSpring如何结合在一起?
二、Spring提供了三种整合Struts的方法:
使用 Spring ActionSupport 类整合 Structs
使用 Spring DelegatingRequestProcessor 覆盖 Struts RequestProcessor
Struts Action 管理委托给 Spring 框架
(参见Get a better handle on Struts actions, with Spring
http://www-128.ibm.com/developerworks/java/library/j-sr2.html?ca=drs-tp4105
对应还有译文:
http://gocom.primeton.com/modules/techresource/article504.htm?utm_campaign=searchengine&utm_source=baidu&utm_medium=jjpm&utm_term=Spring+Struts)
三、我只关心第三种整合方法:
这种方法通过Spring提供的两个和Struts相关类来实现:org.springframework.web.struts. DelegatingActionProxyorg.springframework.web.struts. ContextLoaderPlugIn
ContextLoaderPlugIn实现StrutsPlugIn接口,只要在struts-config.xml中有如下配置:
 
<action    path="/searchSubmit">
type="ca.nexcel.books.actions.DelegatingActionProxy"
             input="/searchEntry.do"
               validate="true"
               name="searchForm">
              <forward name="success" path="/WEB-INF/pages/detail.jsp"/>
              <forward name="failure" path="/WEB-INF/pages/search.jsp"/>
</action>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation" value="/WEB-INF/beans.xml"/>
 </plug-in>
ActionServlet装载的时候就可以顺便装载和Spring相关的beans.xml,和beans.xml中相关的一个东西叫做WebApplicationContext , (Spring里关键就是取得WebApplicationContext,取得这个也就可以用Spring管理业务),在ContextLoaderPlugIn中是这样保存WebApplicationContext
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);
再看DelegatingActionProxy,它继承于StrutsAction,以后struts-config.xml中所有的
Action-mapping都要指向它,只是每个Action-mappingpath不同,将来也是用这个path来区分究竟需要执行beans.xml中的那个类。如下代码:
public ActionForward execute(){
                Action delegateAction = getDelegateAction(mapping);
                return delegateAction.execute(mapping, form, request, response);
        }
这里的delegateAction就是beans.xml中一个相关的类(beans.xml也要求类继承于StrutsAction) 去看看怎么得到delegateAction
protected Action getDelegateAction(ActionMapping mapping) throws BeansException {
WebApplicationContext wac = getWebApplicationContext(getServlet(),
    mapping.getModuleConfig());
String beanName = determineActionBeanName(mapping);
return (Action) wac.getBean(beanName, Action.class);
}
 
是如何取得WebApplicationContext呢:
wac=(WebApplicationContext)actionServlet.getServletContext().getAttribute(        ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX);
 
SERVLET_CONTEXT_PREFIX 正是 前边提到的ContextLoaderPlugInattrName
现在这个原理一目了然,ContextLoaderPlugInactionServlet初始化过程中保存
起来留到后面供DelegatingActionProxy用。
 
四、在另一篇文章中提到在上面的方法中OpenSessionInView Filter不能用
 (参照http://wyyhzc.itpub.net/),这个东西我也不熟悉,是不是有不少Spring的东西在这种方式中都不能用呢? 这就说到另一种取得Spring WebApplicationContext的方法:
web.xml中配置ContextLoaderListener
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      /WEB-INF/beans.xml
    </param-value>
 </context-param>
 <listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
 </listener>
 <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
对应的beans.xml和前边那个一样,Log4jConfigListener先不用管,去查看相关文档。
Web服务启动的时候,我们去看看ContextLoaderListener作了什么:
WebApplicationContext = createWebApplicationContext(servletContext, parent);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
同样是保存WebApplicationContext,但是keyROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
 
怎么才能不用ContextLoaderPlugIn而只用ContextLoaderListener下面我修改
org.springframework.web.struts. DelegatingActionProxy 把它修改成
ca.nexcel.books.actions. DelegatingActionProxy并且修改一下代码:
修改getWebApplicationContext方法
Return DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet,
 moduleConfig); 换成下边方法 
 
ServletContext sc = actionServlet.getServletContext();
WebApplicationContext wac = null;
wac = WebApplicationContextUtils.getWebApplicationContext(sc);
return wac;
并且在struts-config.xml中将actiontype指向自己的
ca.nexcel.books.actions. DelegatingActionProxyPlugIn删除web.xml加上刚才提到的Listener,启动tomcat运行一切正常。
 
五、我把northland的配置文件贴出来。
Struts-config.xml
<action-mappings>
    <action
        path="/list"
        input="/list.jsp"
        name="_list"
        scope="request"
        type="jp.co.nec.hnes.northland.web.struts.FlowAction"
        >
      <display-name>一覧画面</display-name>
    </action>
    <action
        path="/register"
        input="/register.jsp"
        name="_register"
        scope="request"
        type="jp.co.nec.hnes.northland.web.struts.FlowAction"
        >
      <display-name>登録画面</display-name>
    </action>
 
Web.xml:
 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      classpath:flowConfig.xml,
      classpath:viewConfig.xml,
      classpath:applicationContext.xml,
     classpath:applicationContext-extra.xml
    </param-value>
 </context-param> 
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
 </listener>
 <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <servlet>
    <servlet-name>ActionServlet</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
 </servlet>
 
从中可以看到
其中的jp.co.nec.hnes.northland.web.struts.FlowAction
ca.nexcel.books.actions. DelegatingActionProxy的功能差不多。
 
调用系统的环境变量——java实现(转贴):【上一篇】
环境变量设置:【下一篇】
【相关文章】
  • Spring学习笔记
  • Spring从菜鸟到高手(二)AOP的真正实现
  • 浅谈Spring(一)
  • struts html标签
  • Struts+hibernate+Spring 的结构介绍
  • Spring and OSGi相关信息
  • 从分页来体验Struts的诟病
  • Spring | Semantics based
  • Spring in Action 学习笔记—第二章 装配Bean
  • iBATIS和Spring在WEB开发中的应用(一)
  • 【随机文章】
  • SUN系统的基本安全配置(关闭不用的服务)
  • 一个莫名奇妙问题的解决,数据大了后form不提交了
  • 初学设计模式
  • Linux的硬件配置
  • 《世界·领主》开发日志[01]
  • Windows XP下检测您是否处在工作组中?
  • 创建移动Web应用程序(4)
  • C#中的cookie编程
  • 老坚很冒火,后果很严重
  • yum install php mysql -y
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.