获取资源上的Web应用程序的类路径 [英] Access to resources on web application classpath
问题描述
在 WEB-INF / classes中
文件夹我已经结构化包层次Java类文件(例如: com.company.app </ code>)和
资源
文件夹包含Spring上下文XML, jdbc.properties
文件, log4j.properties
文件和我多亩应用程序中使用其他资源。要加载Spring上下文和配置log4j在的web.xml
我用
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>
和的applicationContext.xml
我用财产占位符,找到 jdbc.properties
文件
and in applicationContext.xml
I use property-placeholder to locate jdbc.properties
file
<context:property-placeholder location="classpath:jdbc.properties"/>
但Tomcat的说,该类路径资源不能得到解决。我用Ant来构建一个war文件,并有下一个目标编译Java源代码,并复制除外src文件夹的Java源evererything到 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
How can I have access to resources in web.xml as classpath:log4j.properties for example?
推荐答案
你的资源目录的内容应该可以直接复制到WEB-INF / classes中,所以如果你的结构是:
The contents of your resources dir should be copied directly to WEB-INF/classes, so if your structure is:
src/
java/
resources/
您应该使用类似
<copy todir="${war_web_inf}/classes">
<fileset dir="src/resources" />
</copy>
这篇关于获取资源上的Web应用程序的类路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!