web.xml中webAppRootKey



web.xml中webAppRootKey

------------------------------------------------------------------------------------------------
1、 web.xml配置
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param>
"webapp.root"這個字符串可以隨便寫任何字符串。如果不配置默認值是"webapp.root"。
 
可以用System.getProperty("webapp.root")來動態獲項目的運行路徑。
一般返回結果例如:/usr/local/tomcat6/webapps/項目名

2、解決以下報錯

部署在同一容器中的Web項目,要配置不同的<param-value>,不能重複,否則報類似下面的錯誤:
Web app root system property already set to different value: 'webapp.root' = [/home/user/tomcat/webapps/project1/] instead of [/home/user/tomcat/webapps/project2/] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files! 
意思是“webapp.root”這個key已經指向了項目1,不可以再指向項目2.

3、加載方式

Spring通過org.springframework.web.util.WebAppRootListener 這個監聽器來運行時的項目路徑。
但是如果在web.xml中已經配置了 org.springframework.web.util.Log4jConfigListener這個監聽器,
則不需要配置WebAppRootListener了。因爲Log4jConfigListener已經包含了WebAppRootListener的功能
一般配置類型下面的例子:

Xml代碼 複製代碼 收藏代碼
  1. <!-- 加載Log4J 配置文件  -->  
  2. <context-param>  
  3.     <param-name>log4jConfigLocation</param-name>  
  4.     <param-value>WEB-INF/conf/log4j.properties</param-value>  
  5. </context-param>     
  6.   
  7. <context-param>  
  8.     <param-name>log4jRefreshInterval</param-name>  
  9.       <param-value>3000</param-value>  
  10.  </context-param>  
  11.   
  12. <listener>  
  13.     <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
  14.  </listener>  

 

4、在運行時動態的找出項目的路徑

在log4j.properties配置文件,就可以按下面的方式使用${webapp.root}:
 log4j.appender.file.File=${webapp.root}/WEB-INF/logs/sample.log
就可以在運行時動態的找出項目的路徑

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章