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 
就可以在运行时动态的找出项目的路径


5、默认配置

public static void setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException {  
	String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM);  
	String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY);  
	String oldValue = System .getProperty(key);  
	if (oldValue != null ) {  
			throw new IllegalStateException ("WARNING: Web app root system property already set: " 
								+ key + " = " + oldValue + " - Choose unique webAppRootKey values in your web.xml files!" );  
	}  
	String root = servletContext.getRealPath("/" );  
	if (root == null ) {  
			throw new IllegalStateException ("Cannot set web app root system property when WAR file is not?expanded");  
	}  
	System .setProperty(key, root);  
	servletContext.log("Set web app root system property: " + key + " = " + root);  
}  


从代码看出,该方法其实就是把该web application的根目录的绝对文件路径作为属性保存在 System的属性列表中。该属性的名字,由web.xml文件中的名为"webAppRootKey"的参数值指出。如果不在web.xml中定义 webAppRootKey参数,那么属性名就是缺省的"webapp.root".在我们的petclinic项目中已经定义了 webAppRootKey参数,其值为"petclinic.root",因此,属性的名字就是"petclinic.root".
最后将webAppRootKey项配置好。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值