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

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的整合完成!酷

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值