访问 Web 应用程序类路径上的资源

发布于 2025-01-04 23:22:36 字数 1592 浏览 3 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽手叙旧 2025-01-11 23:22:36

你的资源目录的内容应该直接复制到 WEB-INF/classes,所以如果你的结构是:

src/
  java/
  resources/

你应该使用类似的东西

<copy todir="${war_web_inf}/classes">
  <fileset dir="src/resources" />
</copy>

The contents of your resources dir should be copied directly to WEB-INF/classes, so if your structure is:

src/
  java/
  resources/

You should use something like

<copy todir="${war_web_inf}/classes">
  <fileset dir="src/resources" />
</copy>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文