web项目配置webAppRootKey 获得根目录

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\xxxx\


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”,如下:

最好每个项目的参数值不同,以免引起项目冲突.

转: http://www.verydemo.com/demo_c143_i3192.html

你可能感兴趣的:(web.xml)