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

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

2015-06-10 12:54 585 查看

最近在搭建项目环境的时候出现了下面的错误
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"。
下面是相关源码
1. · public static void
setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException
{
2. String
param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM);

3. String
key = (param != null
? param : DEFAULT_WEB_APP_ROOT_KEY);
4. String
oldValue = System
.getProperty(key);
5. if
(oldValue != null ) {

6. throw new IllegalStateException
("WARNING: Web app root system property already set: " + key + " = " +

7.

8.
9. oldValue + " - Choose unique webAppRootKey values in your web.xml files!" );

10. }
11. String
root = servletContext.getRealPath("/" );

12. if
(root == null ) {

13. throw new IllegalStateException
("Cannot set web app root system property when WAR file is not

14.
15. expanded");

16. }
17. System
.setProperty(key, root);
18. servletContext.log("Set web app root system property: " + key + " = " + root);

19. }
20.
21.

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

但最好设置,以免项目之间的名称冲突。

转自:/article/1809625.html
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: