### Struts、Spring与Hibernate整合的四种方法 在企业级应用开发中,Struts、Spring与Hibernate这三种框架经常被组合使用,以构建强大的、可维护的Java Web应用程序。本文将详细介绍这三种技术如何整合,并重点阐述四种常用的整合方式。 #### 一、通过ContextLoaderListener进行整合 **1. web.xml配置:** 在`web.xml`中添加一个`context-param`来指定Spring的配置文件位置,并注册`ContextLoaderListener`监听器。 ```xml <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/springhiberante.xml </param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> ``` 这种方式适用于较简单的应用场景,其中`springhiberante.xml`是Spring的配置文件,用于管理Bean的生命周期和依赖注入。 **2. Struts Action继承Spring ActionSupport:** 接下来,在Struts的Action类中,我们通常让Action继承自`Spring ActionSupport`类,这样可以利用Spring提供的事务管理和数据验证等功能。 ```java public class MyAction extends ActionSupport { // ... } ``` 这种方式简单明了,但不适用于更复杂的应用场景,尤其是当需要更细粒度地控制Spring容器的初始化时。 #### 二、使用ContextLoaderPlugIn插件进行整合 **1. web.xml配置:** 在`web.xml`中,我们需要定义一个Struts插件——`ContextLoaderPlugIn`,该插件负责加载Spring的配置文件。 ```xml <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/springhiberante.xml"/> </plug-in> ``` **2. 在struts-config.xml中指定路径:** 接下来,在`struts-config.xml`中指定Spring配置文件的路径。 ```xml <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/springhiberante.xml"/> </plug-in> ``` **3. 在struts-config.xml中配置Action类型:** 还需要在`struts-config.xml`中为Action配置特定类型,例如使用`DelegatingActionProxy`。 ```xml <action path="/example" type="org.springframework.web.struts.DelegatingActionProxy"> <forward name="success" path="/success.jsp"/> </action> ``` 这种方式使得Struts能够更好地与Spring集成,特别是在需要使用Spring管理的Bean作为Action处理逻辑的情况下更为适用。 #### 三、使用ControllerPlug-In进行整合 **1. web.xml配置:** 在`web.xml`中,我们同样需要指定Spring配置文件的位置。 **2. struts-config.xml配置ControllerPlug-In:** 接着,在`struts-config.xml`中使用`ControllerPlug-In`配置。 ```xml <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/springhiberante.xml"/> </plug-in> ``` **3. 在struts-config.xml中配置Action类型:** 与第二种方法类似,也需要配置`DelegatingActionProxy`作为Action的类型。 这种方式的优点在于它提供了更多的灵活性,可以更细致地控制请求处理流程。 #### 四、手动创建ApplicationContext进行整合 **1. web.xml配置:** 在`web.xml`中指定Spring配置文件的位置。 **2. struts-config.xml配置:** 接着,在`struts-config.xml`中进行必要的配置。 **3. 手动创建ApplicationContext:** 在Action中手动创建`ApplicationContext`,并从中获取所需的Bean。 ```java public class MyAction implements Action { private ApplicationContext ac = new ClassPathXmlApplicationContext("com/lxm/ssh/applicationContext.xml"); public String execute() throws Exception { // 从ApplicationContext中获取Bean // ... } } ``` 这种方式提供了最大的灵活性,但在代码层面需要更多手动管理,可能会增加维护成本。 ### 总结 Struts、Spring与Hibernate三种框架的整合有多种方式可供选择。每种方式都有其特点和适用场景,开发者可以根据具体项目的需求灵活选择最适合的整合策略。无论是通过监听器、插件还是手动创建ApplicationContext,关键是要确保各框架之间的协调工作,从而构建出高效、稳定的Web应用系统。







注意:applicationContext.xml文件不需放在WEB-INF文件夹下
<servlet>
<servlet-name>action</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>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
第一种:
1:web.xml
<context-param>


- 粉丝: 2
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- soybean-admin-Typescript资源
- 2025年基本公共卫生服务项目培训题库(含答案).doc
- 2025年空调与制冷作业考试题及答案.doc
- 2025年垃圾分类知识竞赛题库与答案.doc
- 2025年空调与制冷作业考试题库及答案.doc
- 2025年垃圾分类知识试题及答案.doc
- 2025年信息安全题库及答案.doc
- (完整版)软件测试转正报告PPT.ppt
- 2025最新健康管理师三级《专业能力》考核试卷及答案.doc
- 物联网与互联网的区别.doc
- 2025最新健康管理师三级专业能力考核试卷及答案.doc
- 2025最新结构力学期末考试题及答案.doc
- 2025年环境卫生管理中心面试题及答案(通用版).docx
- 2025年机电标准化考试题库及答案.docx
- 2025年机动车检测站授权签字人试题库(含答案).docx
- 系统安全评价技术.pptx


