------------------------------------------------------------------------------------------------
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的功能
一般配置類型下面的例子:
4、在運行時動態的找出項目的路徑
在log4j.properties配置文件,就可以按下面的方式使用${webapp.root}:
log4j.appender.file.File=${webapp.root}/WEB-INF/logs/sample.log
就可以在運行時動態的找出項目的路徑