访问 Web 应用程序类路径上的资源
在 WEB-INF/classes 文件夹下,我有包层次结构中的 java 类文件(例如:com.company.app )和 resources 文件夹包含 spring context xml、jdbc.properties 文件、log4j.properties 文件以及我在 mu 应用程序中使用的其他资源。要加载 spring 上下文并在 web.xml
中配置 log4j,我使用
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
并在 applicationContext.xml
中使用 property-placeholder 来查找 jdbc.properties
文件
<context:property-placeholder location="classpath:jdbc.properties"/>
然而,tomcat 说无法解析该类路径资源。我使用 ant 构建一个 war 文件,并有下一个目标来编译 java 源并将除 java 源之外的所有内容从 src 文件夹复制到 WEB-INF/classes
<target name="javac" description="Compile java source to bytecode">
<mkdir dir="${war_web_inf}/classes" />
<javac srcdir="src" includes="**" encoding="utf-8" destdir="WebContent/WEB-INF/classes" source="1.5" target="1.5" nowarn="true" debug="true" debuglevel="lines,vars,source">
<classpath refid="project.class.path" />
</javac>
<copy todir="${war_web_inf}/classes">
<fileset dir="src" excludes="**/*.java" />
</copy>
</target>
我怎样才能访问 web.xml 中的资源例如类路径:log4j.properties?
Under WEB-INF/classes
folder i have java class files structured in package hierarchy(e.g: com.company.app
) and resources
folder that contains spring context xml, jdbc.properties
file, log4j.properties
file and other resources that I use in mu app. To load spring context and configure log4j in web.xml
i use
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
and in applicationContext.xml
I use property-placeholder to locate jdbc.properties
file
<context:property-placeholder location="classpath:jdbc.properties"/>
However tomcat says that that classpath resource can not be resolved. I use ant to build a war file and have next target to compile java sources and copy evererything except java sources from src folder to WEB-INF/classes
<target name="javac" description="Compile java source to bytecode">
<mkdir dir="${war_web_inf}/classes" />
<javac srcdir="src" includes="**" encoding="utf-8" destdir="WebContent/WEB-INF/classes" source="1.5" target="1.5" nowarn="true" debug="true" debuglevel="lines,vars,source">
<classpath refid="project.class.path" />
</javac>
<copy todir="${war_web_inf}/classes">
<fileset dir="src" excludes="**/*.java" />
</copy>
</target>
How can I have access to resources in web.xml as classpath:log4j.properties for example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的资源目录的内容应该直接复制到 WEB-INF/classes,所以如果你的结构是:
你应该使用类似的东西
The contents of your resources dir should be copied directly to WEB-INF/classes, so if your structure is:
You should use something like