項目只用struts但tomcat公用完整SSHjar包後部署失敗問題解決辦法

背景

最近寫了一個網站接口,因爲功能簡單,我就自己搭的環境,只用了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包

發佈了44 篇原創文章 · 獲贊 17 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章