web项目配置webAppRootKey 获得根目录
2013-01-31 10:35
337 查看
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\wangzun\
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”,如下:
<!-- 应用路径 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param>
最好报纸每个项目的参数值不同,以免引起项目冲突
严重: Exception sending context initialized event to listener instance of class org.springframework.web.util.Log4jConfigListener
java.lang.IllegalStateException: Web app root system property already set to different value: 'webapp.root' = [C:\Program Files (x86)\Apache Software Foundation\Tomcat 6.0\webapps\DRMProject\] instead of [C:\Program Files (x86)\Apache Software Foundation\Tomcat
6.0\webapps\DRMSn\] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files!
对多个项目要对webAppRootKey进行配置,这里主要是让log能将日志写到对应项目根目录下,如我配置这两个项目的webAppRootKey为
项目1 的 web.xml:
<!-- 应用路径 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root1</param-value>
</context-param>
<!-- 项目根目录Listener -->
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
项目2的 web.xml:
<!-- 应用路径 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root2</param-value>
</context-param>
<!-- 项目根目录Listener -->
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
这样就不会出现冲突了。
定义以后,在Web Container启动时将把ROOT的绝对路径写到系统变量里。
然后log4j的配置文件里就可以用${webName.root }来表示Web目录的绝对路径,把log文件存放于webapp中。
namemax:
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>web.root</param-value>
</context-param>
<!-- 项目根目录Listener -->
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
bean中可以使用:
<bean id="transformChinese" class="com.zunmi.util.TransformChinese"
p:outBasePath="${web.root}WEB-INF/destineDomainFile/"
p:j2fSource="${web.root}WEB-INF/SimpleToTraditional.properties"
p:charSet="gbk"
/>
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\wangzun\
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”,如下:
<!-- 应用路径 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param>
最好报纸每个项目的参数值不同,以免引起项目冲突
严重: Exception sending context initialized event to listener instance of class org.springframework.web.util.Log4jConfigListener
java.lang.IllegalStateException: Web app root system property already set to different value: 'webapp.root' = [C:\Program Files (x86)\Apache Software Foundation\Tomcat 6.0\webapps\DRMProject\] instead of [C:\Program Files (x86)\Apache Software Foundation\Tomcat
6.0\webapps\DRMSn\] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files!
对多个项目要对webAppRootKey进行配置,这里主要是让log能将日志写到对应项目根目录下,如我配置这两个项目的webAppRootKey为
项目1 的 web.xml:
<!-- 应用路径 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root1</param-value>
</context-param>
<!-- 项目根目录Listener -->
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
项目2的 web.xml:
<!-- 应用路径 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root2</param-value>
</context-param>
<!-- 项目根目录Listener -->
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
这样就不会出现冲突了。
定义以后,在Web Container启动时将把ROOT的绝对路径写到系统变量里。
然后log4j的配置文件里就可以用${webName.root }来表示Web目录的绝对路径,把log文件存放于webapp中。
namemax:
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>web.root</param-value>
</context-param>
<!-- 项目根目录Listener -->
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
bean中可以使用:
<bean id="transformChinese" class="com.zunmi.util.TransformChinese"
p:outBasePath="${web.root}WEB-INF/destineDomainFile/"
p:j2fSource="${web.root}WEB-INF/SimpleToTraditional.properties"
p:charSet="gbk"
/>
相关文章推荐
- Web项目中java类读取WebRoot目录下的配置文件
- 关于【apache- tomcat- 5.5.15/conf /Catalina/localhost配置虚拟目录】时的一些问题。(配置web项目的方式不止一种,虚拟目录就是一个)
- JavaWeb学习之tomcat安装与运行、tomcat的目录结构、配置tomcat的管理用户、web项目目录、虚拟目录、虚拟主机(1)
- TOMCAT的目录结构 1++ Tomact的一些小配置2++Java Web开发: Tomcat中部署项目的三种方法
- vs打开项目出现“尚未配置为Web项目XXXX指定的本地IIS URL HTTP://localhost:…… .要打开此项目,需要配置虚拟目录……”提示
- win2008 mvc4.0 项目发布错误(HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容。)解决方案:
- Tomcat 中如何给 web 项目配置虚拟目录的方法
- 关于【apache- tomcat- 5.5.15/conf /Catalina/localhost配置虚拟目录】时的一些问题。(配置web项目的方式不止一种,虚拟目录就是一个)
- Intellij从无到有创建项目:梳理web项目各目录及配置作用
- 一个WEB项目安装包,自动配置数据库,config文件和虚拟目录。。(原创)
- Maven配置2-使用Maven创建WEB项目支持Tomcat目录部署
- WEB 项目中读取 WEB-INF 目录下配置文件的方法
- Visual studio加载项目时报错 尚未配置为Web项目XXXX指定的本地IIS,需要配置虚拟目录。解决办法。
- 尚未配置为Web项目News指定的本地IIS URL HTTP://localhost:…… .要打开此项目,需要配置虚拟目录……三种解决方案,完美解决
- 一个Tomcat 多项目配置不同端口 有一个访问不了404,web项目配置webAppRootKey
- web项目Log4j日志输出路径配置问题 问题描述:一个web项目想在一个tomcat下运行多个实例(通过修改war包名称的实现),然后每个实例都将日志输出到tomcat的logs目录下实例名命名的文
- 服务器项目目录搭建,gradle 搭建项目web目录,source folder,folder相互转换,部署路径配置,
- 尚未配置为Web项目.指定的本地IIS URL http://localhsst/..要打开项目,需要配置虚拟目录 。是否立即创建虚拟目录
- 同一个tomcat下面部署了两个项目,有一个访问不了404,web项目配置webAppRootKey
- 尚未配置为Web项目XXXX指定的本地IIS URL HTTP://localhost:…… .要打开此项目,需要配置虚拟目录……