背景
最近写了一个网站接口,因为功能简单,我就自己搭的环境,只用了Struts框架,连接数据库用的jdbc,本地启动正常,但是部署linux服务器就启动不了,查看了tomcat日志,发现好多报错信息,为了分清哪些是我的项目的错误,我删了自己本地项目的jar包,把tomcat共享jar包放自己项目本地启动,然后查看了控制台的报错信息,下面是部分报错,就逐个解决
解决
PS.公司用的Struts+Spring
- 错误信息1
Looks like the Spring listener was not configured for your web app!
Nothing will work until WebApplicationContextUtils returns a valid ApplicationContext.
You might need to add the following to web.xml:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
- Dispatcher initialization failed
这是因为有struts2-spring-plugin-xxx.jar包,需要配置监听,直接配在web.xml文件中即可
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
- 错误信息2
- Loading XML bean definitions from ServletContext resource [/WEB-INF/applicationContext.xml]
- Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
这个错误提示是说的读取不到applicationContext.xml,当使用了spring后需要applicationContext.xml配置数据源事务aop之类的,很显眼,我是没有用到这些,但是这个文件依然需要存在,然后我就新建了一个,但是不做任何配置
applicationContext.xml文件信息
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" xmlns:context="http://www.springframework.org/schema/context">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" ></bean>
<bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init="true"></bean>
</beans>
接下来就需要在web.xml中配置applicationContext.xml文件
在web.xml中添加一下配置信息
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
然后重启项目,发现已经能正常启动
PS.还有一个错误信息,是在不知不觉中改好的,这里也提示一下
严重: Exception starting filter struts2
Class: com.opensymphony.xwork2.spring.SpringObjectFactory
File: SpringObjectFactory.java
Method: getClassInstance
Line: 230 - com/opensymphony/xwork2/spring/SpringObjectFactory.java:230:-1
这是因为当有Spring时,action是用spring生成的,之所以报错就是因为没有配置ContextLoaderListener,就是文中第一步时配置的监听,当监听和applicationContext.xml都配置完毕后,这个错误自然而然就消失了
总结
jar使用不当会出现很多问题,所以做项目时一定要好好选jar包,也要注意服务器中的公用jar包