1. 程式人生 > >同一個tomcat下面部署了兩個專案,有一個訪問不了404,web專案配置webAppRootKey

同一個tomcat下面部署了兩個專案,有一個訪問不了404,web專案配置webAppRootKey

場景:剛接手了同事建立的專案,準備部署到伺服器上(此tomcat下已經有了一個專案),部署好之後發現原有專案可以正常訪問,但是新專案報404,怎麼也訪問不了。

問題排查:原來是同事拿之前的專案直接拷貝,然後在拷貝的專案上進行修改的。 解決方案:
	<!-- 應用路徑 -->
	<context-param>
		<param-name>webAppRootKey</param-name>
		<param-value><span style="color:#ff0000;">webapp.root1</span></param-value>
	</context-param>

	<!-- 專案根目錄Listener -->
	<listener>
		<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
	</listener>

注意:

1、在應用的web.xml檔案中,配置不同的webAppRootKey,<param-value>值可以自定義,只要相同tomcat下的各個工程保持不同即可。 2、專案根目錄listener,WebAppRootListener要在ApplicationContext的ContextLoaderListener之前,否則ApplicationContext的bean注入根目錄值時會發生無法注入異常。 3、如果在web.xml中已經配置了 org.springframework.web.util.Log4jConfigListener 這個監聽器,則不需要配置WebAppRootListener了。因為Log4jConfigListener已經包含了WebAppRootListener的功能。 4、根據以上修改以及1-3點注意之後,仍然無法訪問404的,可能:(1)專案本身有問題。(2)因為是拷貝的專案,很多配置檔案或者檔名、目錄都是以前的,沒有修改完全,特別是配置檔案,logs目錄等,仔細檢查。(3)本人使用的spring-mvc+mybatis,如果你也是,可能是mybatis/mapper下檔案為空,還沒有對映但是你配置了。(4)以上無法解決,請檢視tomcat下的logs檔案中的異常資訊,或者聯絡我,我們共同學習討論。 參考資料:
log4j和web.xml配置webAppRootKey 的問題

1 在web.xml配置 

<context-param>
  <param-name>webAppRootKey</param-name>
  <param-value>web.sample.root</param-value>
</context-param>

可以用System.getProperty("web.sample.root")來獲取屬性值。在Eclipse除錯Web專案時,專案的路徑是一個臨時路徑,不在真正的路徑下,可以通過上述語句打印出屬性值,來看看臨時專案路徑在哪裡

如:System.out.println("web.root:"+ System.getProperty("web.root"));

    輸出:web.root:D:\apache-tomcat-6.0.30\webapps\wangzun\

2、Spring通過 org.springframework.web.util.WebAppRootListener 這個監聽器來壓入專案路徑。但是如果在web.xml中已經配置了 org.springframework.web.util.Log4jConfigListener 這個監聽器,則不需要配置WebAppRootListener了。因為Log4jConfigListener已經包含了WebAppRootListener的功能.

配置WebAppRootListener:

 <listener>
  <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
 </listener>


3、部署在同一容器中的多個Web專案,要配置不同的<param-value>,不能重複webAppRootKey的系統變數名

4.WebAppRootListener要在ApplicationContext的ContextLoaderListener之前,否則ApplicationContext的bean注入根目錄值時會發生無法注入異常。

   <!-- 專案根目錄Listener -->
 <listener>
  <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
 </listener>
 <!--Spring的ApplicationContext 載入 -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>


4、如果配置了
log4j.appender.file.File=${web.sample.root}WEB-INF/logs/sample.log  

log4j會自己自動建立logs目錄, 不需要手工顯式建立空的logs目錄


在tomcat下部署兩個或多個專案時,web.xml檔案中最好定義webAppRootKey引數,如果不定義,將會預設為“webapp.root”,如下:

<!-- 應用路徑 --> 
<context-param> 
<param-name>webAppRootKey</param-name> 
<param-value>webapp.root</param-value> 
</context-param> 
最好報紙每個專案的引數值不同,以免引起專案衝突

嚴重: Exception sending context initialized event to listener instance of class org.springframework.web.util.Log4jConfigListener
java.lang.IllegalStateException: Web app root system property already set to different value: 'webapp.root' = [C:\Program Files (x86)\Apache Software Foundation\Tomcat 6.0\webapps\DRMProject\] instead of [C:\Program Files (x86)\Apache Software Foundation\Tomcat 6.0\webapps\DRMSn\] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files!

對多個專案要對webAppRootKey進行配置,這裡主要是讓log能將日誌寫到對應專案根目錄下,如我配置這兩個專案的webAppRootKey為
專案1 的 web.xml:

<!-- 應用路徑 -->
<context-param> 
<param-name>webAppRootKey</param-name> 
<param-value>webapp.root1</param-value> 
</context-param>

 <!-- 專案根目錄Listener -->
 <listener>
  <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
 </listener>

專案2的 web.xml:
<!-- 應用路徑 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root2</param-value>
</context-param>

 <!-- 專案根目錄Listener -->
 <listener>
  <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
 </listener>

這樣就不會出現衝突了。
定義以後,在Web Container啟動時將把ROOT的絕對路徑寫到系統變數裡。
然後log4j的配置檔案裡就可以用${webName.root }來表示Web目錄的絕對路徑,把log檔案存放於webapp中。

namemax:

 <context-param>
  <param-name>webAppRootKey</param-name>
  <param-value>web.root</param-value>
 </context-param>

 <!-- 專案根目錄Listener -->
 <listener>
  <listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
 </listener>

bean中可以使用:

   <bean id="transformChinese" class="com.zunmi.util.TransformChinese"

         p:outBasePath="${web.root}WEB-INF/destineDomainFile/"
         p:j2fSource="${web.root}WEB-INF/SimpleToTraditional.properties" 
         p:charSet="gbk"
        />

參考資料地址:http://www.verydemo.com/demo_c143_i3192.html