`

如何将Spring和Struts2进行整合及加入监听器后抛异常的解决方法

SSH 
阅读更多

1、添加一个jar包
    struts2-spring-plugin-2.3.24.1.jar

 

除了添加jar包以外,整合还需要做哪些操作?

1.将原先的struts.xml中的action属性写成bean的名称,并且在TestAction.java中添加annotation注释

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

	<!-- 配置扩展名为action -->
	<constant name="struts.action.extension" value="action"></constant>

	<!-- 如果这个选项的value为true则代表可以使用动态方法调用即可以使用类似于u!load.action?id=1 -->
	<constant name="struts.enable.DynamicMethodInvocation" value="false" />

	<!-- 要是value为true表示配置为开发模式从而代码可以热部署 -->
	<constant name="struts.devMode" value="true" />



	<package name="default" namespace="/" extends="struts-default">

		<!-- 配置测试用的action,当与spring整合后,class属性写的就是bean的名称 -->
		<action name="test" class="testAction">
			<result name="success">/test.jsp</result>
		</action>
	</package>

</struts>

 

package com.jxust.oa.test;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class TestAction extends ActionSupport {

	@Override
	public String execute() throws Exception{
		System.out.print("--------->TestAction execute()");
		return "success";
	}
}

 

2.在web.xml中配置spring的监听器用于初始化ApplicationContext对象

<?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的监听器用于初始化ApplicationContext对象 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext*.xml</param-value>
	</context-param>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<!-- 配置struts2的主过滤器 -->
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>



</web-app>

 

 做完以上操作以后,和预期效果不一样,抛出了BeanInitialzationException异常

严重: Context initialization failed
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/jdbc.properties]
	at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78)
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:553)
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:527)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:362)
	at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5017)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5531)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
	at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1263)
	at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1948)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
	at java.util.concurrent.FutureTask.run(FutureTask.java:262)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/jdbc.properties]
	at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:117)
	at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:182)
	at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:161)
	at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:69)
	... 19 more

 

解决此异常的方法:由于在applicationContext.xml中加载外部properties配置文件中location的值为jdbc.properties从而引发了这个异常

 

所以解决这个问题的关键就在于将applicationContext.xml的中加载外部properties配置文件中location的值改成classpath:jdbc.properties即可

<?xml version="1.0" encoding="UTF-8"?>
<!-- - Middle tier application context definition for the image database. -->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


	<!-- 自动扫描与装配bean -->
	<context:component-scan base-package="com.jxust.oa"></context:component-scan>

	<!-- 加载外部的properties配置文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>

	<!-- 配置数据库连接池(c3p0) -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 基本信息 -->
		<property name="jdbcUrl" value="${jdbcUrl}"></property>
		<property name="driverClass" value="${driverClass}"></property>
		<property name="user" value="${user}"></property>
		<property name="password" value="${password}"></property>

		<!-- 其他配置 -->
		<!-- 初始化时获得三个连接,取值应在minPoolSize与maxPoolSize之间,Default:3 -->
		<property name="initialPoolSize" value="3"></property>
		<!-- 连接池中保留的是最小连接数,Default:3 -->
		<property name="minPoolSize" value="3"></property>
		<!-- 连接池中保留的是最大连接数,Default:15 -->
		<property name="maxPoolSize" value="15"></property>
		<!-- 当连接池中的连接耗尽的时候c3p0一次同时获得的连接数,Default:3 -->
		<property name="acquireIncrement" value="3"></property>
		<!-- 控制数据源内加载的PreparedStatements,如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0 -->
		<property name="maxStatements" value="0"></property>
		<!-- maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statement数,Default:0 -->
		<property name="maxStatementsPerConnection" value="0"></property>
		<!-- 最大空F闲时间,1800秒内未使用则连接被丢弃,若为0则永不丢弃,Default:0 -->
		<property name="maxIdleTime" value="1800"></property>

	</bean>

	<!-- 配置SessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
	</bean>
	
	<!-- 配置声明式事务管理(采用基于注解的方式) -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

 

 重新部署tomcat,得到预期效果,Struts2和spring的整合完成!酷

 

分享到:
评论

相关推荐

    常用1.SchLib

    常用1.SchLib

    tokenizers-0.26.0.jar中文文档.zip

    # 【tokenizers-***.jar***文档.zip】 中包含: ***文档:【tokenizers-***-javadoc-API文档-中文(简体)版.zip】 jar包下载地址:【tokenizers-***.jar下载地址(官方地址+国内镜像地址).txt】 Maven依赖:【tokenizers-***.jar Maven依赖信息(可用于项目pom.xml).txt】 Gradle依赖:【tokenizers-***.jar Gradle依赖信息(可用于项目build.gradle).txt】 源代码下载地址:【tokenizers-***-sources.jar下载地址(官方地址+国内镜像地址).txt】 # 本文件关键字: tokenizers-***.jar***文档.zip,java,tokenizers-***.jar,ai.djl.huggingface,tokenizers,***,ai.djl.engine.rust,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,djl,huggingface,中文API文档,手册,开发手册,使用手册,参考手册 # 使用方法: 解压 【tokenizers-***.jar***文档.zip】,再解压其中的 【tokenizers-***-javadoc-API文档-中文(简体)版.zip】,双击 【index.html】 文件,即可用浏览器打开、进行查看。 # 特殊说明: ·本文档为人性化翻译,精心制作,请放心使用。 ·只翻译了该翻译的内容,如:注释、说明、描述、用法讲解 等; ·不该翻译的内容保持原样,如:类名、方法名、包名、类型、关键字、代码 等。 # 温馨提示: (1)为了防止解压后路径太长导致浏览器无法打开,推荐在解压时选择“解压到当前文件夹”(放心,自带文件夹,文件不会散落一地); (2)有时,一套Java组件会有多个jar,所以在下载前,请仔细阅读本篇描述,以确保这就是你需要的文件; # Maven依赖: ``` <dependency> <groupId>ai.djl.huggingface</groupId> <artifactId>tokenizers</artifactId> <version>***</version> </dependency> ``` # Gradle依赖: ``` Gradle: implementation group: 'ai.djl.huggingface', name: 'tokenizers', version: '***' Gradle (Short): implementation 'ai.djl.huggingface:tokenizers:***' Gradle (Kotlin): implementation("ai.djl.huggingface:tokenizers:***") ``` # 含有的 Java package(包): ``` ai.djl.engine.rust ai.djl.engine.rust.zoo ai.djl.huggingface.tokenizers ai.djl.huggingface.tokenizers.jni ai.djl.huggingface.translator ai.djl.huggingface.zoo ``` # 含有的 Java class(类): ``` ai.djl.engine.rust.RsEngine ai.djl.engine.rust.RsEngineProvider ai.djl.engine.rust.RsModel ai.djl.engine.rust.RsNDArray ai.djl.engine.rust.RsNDArrayEx ai.djl.engine.rust.RsNDArrayIndexer ai.djl.engine.rust.RsNDManager ai.djl.engine.rust.RsSymbolBlock ai.djl.engine.rust.RustLibrary ai.djl.engine.rust.zoo.RsModelZoo ai.djl.engine.rust.zoo.RsZooProvider ai.djl.huggingface.tokenizers.Encoding ai.djl.huggingface.tokenizers.HuggingFaceTokenizer ai.djl.huggingface.tokenizers.HuggingFaceTokenizer.Builder ai.djl.hu

    电力系统PMU优化配置研究——基于MATLAB的多种算法实现与性能比较

    内容概要:本文详细探讨了电力系统中PMU(相量测量单元)的优化配置问题,旨在确保系统完全可观测的同时尽量减少PMU的数量。作者介绍了六种不同的算法,包括模拟退火、图论方法、递归安全N算法等,并通过MATLAB实现了这些算法。通过对IEEE标准测试系统的实验,展示了各种算法在不同规模系统中的表现。文中不仅提供了具体的MATLAB代码实现,还分享了许多实用的经验技巧,如邻域解生成、退火速率设置、拓扑排序等。 适合人群:从事电力系统研究的技术人员、研究生以及对组合优化感兴趣的科研工作者。 使用场景及目标:适用于电力系统状态估计、故障诊断等领域,帮助研究人员和工程师找到最优的PMU配置方案,提高系统的可靠性和经济性。 其他说明:文章强调了在实际应用中需要注意的问题,如变压器支路的影响、节点编号不连续等问题,并推荐了几篇相关领域的经典文献供进一步学习。此外,还提到了一些有趣的发现,如某些中间节点装PMU反而能减少总数。

    spring-ai-mistral-ai-1.0.0-M5.jar中文文档.zip

    # 压缩文件中包含: 中文文档 jar包下载地址 Maven依赖 Gradle依赖 源代码下载地址 # 本文件关键字: jar中文文档.zip,java,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,中文API文档,手册,开发手册,使用手册,参考手册 # 使用方法: 解压最外层zip,再解压其中的zip包,双击 【index.html】 文件,即可用浏览器打开、进行查看。 # 特殊说明: ·本文档为人性化翻译,精心制作,请放心使用。 ·只翻译了该翻译的内容,如:注释、说明、描述、用法讲解 等; ·不该翻译的内容保持原样,如:类名、方法名、包名、类型、关键字、代码 等。 # 温馨提示: (1)为了防止解压后路径太长导致浏览器无法打开,推荐在解压时选择“解压到当前文件夹”(放心,自带文件夹,文件不会散落一地); (2)有时,一套Java组件会有多个jar,所以在下载前,请仔细阅读本篇描述,以确保这就是你需要的文件;

    三菱FX1s与台达MS300变频器基于Modbus RTU通讯的实战指南

    内容概要:本文详细介绍了三菱FX1s PLC与台达MS300变频器通过Modbus RTU协议实现通讯的方法。首先,文中列举了所需的硬件设备及其连接方法,确保PLC与变频器能够正常通信。接下来,针对频率设定、频率读取及正反转启停控制三大主要功能进行了详细的编程讲解,提供了具体的梯形图代码示例并解释了每一步的作用。此外,还涉及到了触摸屏(MCGS和威纶通)的配置步骤,使用户可以通过触摸屏方便地操作变频器的各项功能。最后,作者分享了一些实用的小技巧和常见错误避免方法,帮助使用者快速解决问题,提高工作效率。 适合人群:从事自动化控制系统集成的技术人员,尤其是那些需要将三菱PLC与台达变频器进行互联的工程师。 使用场景及目标:适用于工业自动化领域的项目实施过程中,旨在帮助技术人员掌握三菱FX1s与台达MS300变频器之间的高效通信技术,从而更好地完成系统集成任务。 其他说明:文中不仅包含了详细的理论知识和技术要点,还有丰富的实践经验分享,有助于读者全面理解和应用相关技术。同时,提供的完整工程文件可以直接应用于实际项目中,极大地节省了开发时间和成本。

    winrar免费版压缩工具

    winrar免费版压缩工具

    基于CEC21测试函数的灰狼、鲸鱼、人工蜂群优化算法性能对比及Matlab实现

    内容概要:本文详细介绍了灰狼算法(GWO)、鲸鱼算法(WOA)和人工蜂群算法(ABC)在CEC21标准测试函数集上的性能对比。通过设定相同的实验条件(种群数量50,迭代次数500次,30维问题空间),分别探讨了各算法的关键参数调整及其对不同类型函数(单峰、多峰、复合)的影响。文中提供了每个算法的核心代码片段,并针对具体函数给出了优化建议。最终结果显示,GWO在单峰函数上有优势,WOA擅长处理旋转和平移问题,而ABC在高维复杂环境中表现出色。 适合人群:从事优化算法研究的科研人员、研究生以及对智能优化算法感兴趣的开发者。 使用场景及目标:适用于需要评估和比较不同优化算法性能的研究项目,特别是那些涉及高维、多峰、旋转平移等问题的实际应用场景。目标是帮助研究人员选择最适合特定任务的优化算法,并提供参数调优的经验。 其他说明:文章不仅提供了理论分析,还分享了许多实践经验,如参数调整技巧、初始化方法等。此外,所有实验均基于Matlab平台完成,附带完整的代码实现,方便读者复现实验结果。

    电控开关.SchLib

    电控开关.SchLib

    spring-ai-autoconfigure-model-openai-1.0.0-M7.jar中文-英文对照文档.zip

    # 【spring-ai-autoconfigure-model-openai-1.0.0-M7.jar中文-英文对照文档.zip】 中包含: 中文-英文对照文档:【spring-ai-autoconfigure-model-openai-1.0.0-M7-javadoc-API文档-中文(简体)-英语-对照版.zip】 jar包下载地址:【spring-ai-autoconfigure-model-openai-1.0.0-M7.jar下载地址(官方地址+国内镜像地址).txt】 Maven依赖:【spring-ai-autoconfigure-model-openai-1.0.0-M7.jar Maven依赖信息(可用于项目pom.xml).txt】 Gradle依赖:【spring-ai-autoconfigure-model-openai-1.0.0-M7.jar Gradle依赖信息(可用于项目build.gradle).txt】 源代码下载地址:【spring-ai-autoconfigure-model-openai-1.0.0-M7-sources.jar下载地址(官方地址+国内镜像地址).txt】 # 本文件关键字: spring-ai-autoconfigure-model-openai-1.0.0-M7.jar中文-英文对照文档.zip,java,spring-ai-autoconfigure-model-openai-1.0.0-M7.jar,org.springframework.ai,spring-ai-autoconfigure-model-openai,1.0.0-M7,org.springframework.ai.model.openai.autoconfigure,jar包,Maven,第三方jar包,组件,开源组件,第三方

    c++复习题.doc

    c++复习题.doc

    附件3:本科毕业设计(论文)中期检查报告(3)(1)(1).docx

    本科毕业设计(论文)中期检查报告

    【信号调制】使用不同的分类器(逻辑回归分类器、决策树、随机森林、全连接密集层和CNN)来训练模型,以预测不同信噪比值下信号的调制类型附Python代码.rar

    1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。

    weixin248食堂订餐小程序ssm(文档+源码)_kaic

    weixin248食堂订餐小程序ssm(文档+源码)_kaic

    基于粒子群优化算法的微型燃气轮机冷热电联供系统优化调度附Matlab代码.rar

    1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。

    e1e90185ca2f1eda312e7f604d38195c_b4125f83523abcb38acd9dc0deebd500.png

    e1e90185ca2f1eda312e7f604d38195c_b4125f83523abcb38acd9dc0deebd500

    spring-ai-autoconfigure-mcp-client-1.0.0-M7.jar中文-英文对照文档.zip

    # 【spring-ai-autoconfigure-mcp-client-1.0.0-M7.jar中文-英文对照文档.zip】 中包含: 中文-英文对照文档:【spring-ai-autoconfigure-mcp-client-1.0.0-M7-javadoc-API文档-中文(简体)-英语-对照版.zip】 jar包下载地址:【spring-ai-autoconfigure-mcp-client-1.0.0-M7.jar下载地址(官方地址+国内镜像地址).txt】 Maven依赖:【spring-ai-autoconfigure-mcp-client-1.0.0-M7.jar Maven依赖信息(可用于项目pom.xml).txt】 Gradle依赖:【spring-ai-autoconfigure-mcp-client-1.0.0-M7.jar Gradle依赖信息(可用于项目build.gradle).txt】 源代码下载地址:【spring-ai-autoconfigure-mcp-client-1.0.0-M7-sources.jar下载地址(官方地址+国内镜像地址).txt】 # 本文件关键字: spring-ai-autoconfigure-mcp-client-1.0.0-M7.jar中文-英文对照文档.zip,java,spring-ai-autoconfigure-mcp-client-1.0.0-M7.jar,org.springframework.ai,spring-ai-autoconfigure-mcp-client,1.0.0-M7,org.springframework.ai.mcp.client.autoconfigure,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,springfram

    基于 OpenCV 的图像颜色与形状识别项目(含完整 Python 源码)

    该项目使用 OpenCV 实现图像中红色目标的识别与轮廓框选,适用于图像处理、颜色追踪与形状检测等场景。项目无需深度学习框架,适合图像识别技术入门学习。附带测试图像与运行说明,支持一键运行。

    爱威6-8电脑调音软件是专为音响爱好者和专业人士设计的一款强大工具,喜欢的话,直接下载吧

    爱威6-8电脑调音软件是专为音响爱好者和专业人士设计的一款强大工具,喜欢的话,直接下载吧

    spring-ai-vertex-ai-0.8.0.jar中文-英文对照文档.zip

    # 【spring-ai-vertex-ai-0.8.0.jar中文-英文对照文档.zip】 中包含: 中文-英文对照文档:【spring-ai-vertex-ai-0.8.0-javadoc-API文档-中文(简体)-英语-对照版.zip】 jar包下载地址:【spring-ai-vertex-ai-0.8.0.jar下载地址(官方地址+国内镜像地址).txt】 Maven依赖:【spring-ai-vertex-ai-0.8.0.jar Maven依赖信息(可用于项目pom.xml).txt】 Gradle依赖:【spring-ai-vertex-ai-0.8.0.jar Gradle依赖信息(可用于项目build.gradle).txt】 源代码下载地址:【spring-ai-vertex-ai-0.8.0-sources.jar下载地址(官方地址+国内镜像地址).txt】 # 本文件关键字: spring-ai-vertex-ai-0.8.0.jar中文-英文对照文档.zip,java,spring-ai-vertex-ai-0.8.0.jar,org.springframework.ai,spring-ai-vertex-ai,0.8.0,org.springframework.ai.vertex,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,springframework,spring,ai,vertex,中文-英文对照API文档,手册,开发手册,使用手册,参考手册 # 使用方法: 解压 【spring-ai-vertex-ai-0.8.0.jar中文-英文对照文档.zip】,再解压其中的 【spring-ai-vertex-ai-0.8.0-javadoc-API文档-中文(简体)-英语-对照版.zip】,双

    电源.SCHLIB

    电源.SCHLIB

Global site tag (gtag.js) - Google Analytics