Struts2+Hibernate+Spring 整合(支持struts、applicationContext多配置文件)

本文详细介绍如何整合Struts2、Spring及Hibernate三个框架,包括所需jar包、web.xml及struts.xml配置更新,以及多配置文件的使用方法。
[i][b]一、导包[/b][/i]
struts2 需要用到的包:
commons-fileupload.jar、commons-io.jar、commons-logging.jar、freemarker.jar、ognl.jar、struts2-core.jar、xwork-core.jar
[i][b]二、项目引入spring 功能、hibernate 功能[/b][/i]
[i][b]三、改写配置文件[/b][/i]
[color=red]a. web.xml[/color]
由于spring采用注入式管理对象方式,我们也必须将struts2中自己编写的action交给spring管理。而spring通过解析applicationContext.xml文件控制反转实例化所有的bean。所以为了能够在应用初始化时对applicationContext.xml进行解析,修改web.xml配置文件,加入spring监听器。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 配置spring的监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<!-- 开启监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置OpenSessionInViewFilter,必须在struts2监听之前 -->
<filter>
<filter-name>lazyLoadingFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>lazyLoadingFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- 设置监听加载上下文 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
[color=red]b.struts.xml[/color]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.objectFactory" value="spring" />
<include file="/com/time/ssh/action/struts.xml"></include>
</struts>

struts.xml配置需要注意几点:1.文件存放位置 必须是src目录下 2.加入<constant name="struts.objectFactory" value="spring"/> 表示实例化对象交给spring管理 3.多配置文件:开发过程中struts配置文件是经常改动的,为了便于团队开发,有时候我们必须对不同的模块建立不同的struts配置文件。struts2中多配置文件使用非常简单使用<include file="子配置文件路径"></include>
下面看个具体的struts.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="cs" namespace="/test" extends="struts-default">
<action name="login" class="login">
<result name="success">/success.jsp</result>
<result name="false">/fail.jsp</result>
</action>
</package>
</struts>
这里需要注意的是 由于现在action实例是交给spring管理 所以在<action name="login" class="[color=green]login[/color]"/> 这里不能再给该类路径了 应该是给 在applicationContext.xml中对应配置的bean的id ,为了便于理解在这里将spring配置文件中的代码也一起贴出来:
<bean id="[color=green]login[/color]" class="com.time.ssh.action.Login">

[color=red] c applicationContext.xml[/color]
关于applicationContext.xml配置问题不是很大 因为就是一些简单的bean配置 只是要注意其中的一些引用,下面介绍一下关于spring多配置文件的写法:spring多配置文件写法大致有两种:一种是在 web.xml配置中 使用
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/context1.xml,
/WEB-INF/classes/context2.xml,
/WEB-INF/classes/context3.xml
</param-value>
</context-param>
利用上下文参数,其中分隔符可以是",",也可以是" "等 ,也可以用通配符application-*,这样配置的要求是,你的Spring配置文件必须是applicationContext-*****.xml这样的形式存在,*号代表通配符,具体就不说了。
第二种方法是在一个application.xml中配置其他多个使用:
<import resource="其它文件路径"/>

这样就可以实现多个struts和spring配置文件的SSH整合了!
标题基于Python的自主学习系统后端设计与实现AI更换标题第1章引言介绍自主学习系统的研究背景、意义、现状以及本文的研究方法和创新点。1.1研究背景与意义阐述自主学习系统在教育技术领域的重要性和应用价值。1.2国内外研究现状分析国内外在自主学习系统后端技术方面的研究进展。1.3研究方法与创新点概述本文采用Python技术栈的设计方法和系统创新点。第2章相关理论与技术总结自主学习系统后端开发的相关理论和技术基础。2.1自主学习系统理论阐述自主学习系统的定义、特征和理论基础。2.2Python后端技术栈介绍DjangoFlask等Python后端框架及其适用场景。2.3数据库技术讨论关系型和非关系型数据库在系统中的应用方案。第3章系统设计与实现详细介绍自主学习系统后端的设计方案和实现过程。3.1系统架构设计提出基于微服务的系统架构设计方案。3.2核心模块设计详细说明用户管理、学习资源管理、进度跟踪等核心模块设计。3.3关键技术实现阐述个性化推荐算法、学习行为分析等关键技术的实现。第4章系统测试与评估对系统进行功能测试和性能评估。4.1测试环境与方法介绍测试环境置和采用的测试方法。4.2功能测试结果展示各功能模块的测试结果和问题修复情况。4.3性能评估分析分析系统在高并发等场景下的性能表现。第5章结论与展望总结研究成果并提出未来改进方向。5.1研究结论概括系统设计的主要成果和技术创新。5.2未来展望指出系统局限性并提出后续优化方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值