您的位置:首页 > 移动开发

java.lang.IllegalStateException: Web app root system property already set to different value

2013-07-22 09:18 459 查看
最近在搭建项目环境的时候出现了下面的错误
java.lang.IllegalStateException: Web app root system property already set to different value: 'webapp.root' = [D:/tomcat-5.0.19/webapps/tzbms/] instead of [D:/tomcat-5.0.19/webapps/its/] - Choose unique values for
the 'webAppRootKey' context-param in your web.xml files!
 
后来查了资料才知道
webAppRootKey是在java web项目的web.xml配置文件中表示项目的唯一标示,在Eclipse调试Web项目时,项目的路径是一个临时路径,不在真正的路径下,可以通过log4j日志的方式打印出属性值,来看看临时项目路径在哪里,可以用System.getProperty("web.sample.root");如果web.xm
内没有设置webAppRootKey项,是为默认设置,那么webAppRootKey就是缺省的"webapp.root"。
下面是相关源码
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".
 
但最好设置,以免项目之间的名称冲突。
 
 
Spring通过 org.springframework.web.util.WebAppRootListener 这个监听器来压入项目路径。但是如果在web.xml中已经配置了 org.springframework.web.util.Log4jConfigListener

这个监听器,则不需要配置WebAppRootListener了。因为Log4jConfigListener已经包含了WebAppRootListener的功能

部署在同一容器中的Web项目,要配置不同的<param-value>,不能重复

如果配置了

log4j.appender.file.File=${web.sample.root}/WEB-INF/logs/sample.log  
log4j会自己自动建立logs目录, 不需要手工显式建立空的logs目录
 
 
解决方案:
在启动出现错误的工程web.xml增加如下语句便可
<context-param>
<param-name>webAppRootKey</param-name>
<param-value> app.root </param-value>
</context-param>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java.lang.IllegalSta
相关文章推荐