开发者社区> 问答> 正文

Struts+spring 应该怎么配置事物

我看过网上很多spring 事物都必须用hibernate3.jar 例如:

我没有用hibernate3.jar,属于struts2 + spring +JDBC 的框架,这样的框架怎么配置事物啊?请知道的高手详细介绍一下,谢谢

展开
收起
小旋风柴进 2016-03-03 13:08:00 2538 0
1 条回答
写回答
取消 提交回答
  • pirng和Struts结合,主要有三种方式:
    1,使用ActionSupport类
    2,覆盖RequestProcessor
    3,将Action委托给Spring
    文章<使用 Spring更好地处理Struts动作>中,对这三种方法都分析得比较详细,我在这里归纳一下.
    使用第一种方法,是最简单的,不需要其他任何配置,只需要在把继承Action,改成继承ActionSupport,带来的问题就是Struts与Spring,紧耦合,以后不使用Spring配置时,需要修改代码.但其实,我目前觉得使用此方法,有一个好处是可以方便的得到WebApplicationContext对象,不然,就需要使用ClassPathXmlApplicaiton("...")来取得Context对象,不是很方便.其实,看DelegationActionUtils的源码,applicationContext对象也只是,这样子通过sturts的plugin取得的.
    actionServlet.getServletContext().getAttribute(

                    ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX + modulePrefix)
    

    第二种方法,所有Action的分发都是通过ActionServlet的,而实际上的操作是由RequestProcessor 完成的,因此,可以把它覆盖,引入Spring
    The RequestProcessor is where the majority of the core processing occurs for each request. Since version 1.3, the default Request Processor (ComposableRequestProcessor) is composed using Jakarta Commons Chain, which is an implementation of the Chain of Responsibility pattern (CoR).

    The element allows you to configure the ActionServlet. Many of the controller parameters were previously defined by servlet initialization parameters in your web.xml file but have been moved to this section of struts-config.xml in order to allow different modules in the same web application to be configured differently.

    第三种是最推荐的方法
    顺便说一下,plugin在struts的概念,来自UserGuide:
    The PlugIn interface extends Action and so that applications can easily hook into the ActionServlet lifecycle. This interface defines two methods, init() and destroy(), which are called at application startup and shutdown, respectively. A common use of a Plugin Action is to configure or load application-specific data as the web application is starting up.
    其中Spring中的ContextLoaderPlugIn就是继承此PlugIn ,从而引入了Spring

    2019-07-17 18:51:14
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多