【问题标题】:Struts 2 + Spring and contextConfigLocationStruts 2 + Spring 和 contextConfigLocation
【发布时间】:2013-06-18 18:56:12
【问题描述】:

我正在尝试集成和配置 Struts 2 + Spring 并将 ContextLoaderListener 侦听器类注册为参数 contextConfigLocation 我正在尝试编写 SpringBeans.xml 而不是默认的 applicationContext.xml。但问题是这个 SpringBeans.xml 位于根 src 目录中.. 我不知道如何编写参数值.. /src/SpringBeans.xml ...请帮助..

【问题讨论】:

  • 有什么问题并发布 spring 配置。

标签: spring struts2 web.xml


【解决方案1】:

不会src/SpringBeans.xml,因为您的源目录不是可部署的工件。

你应该把配置文件放在其中一个:

  • 在类路径上(IMO 通常更可取)
  • 在 WEB-INF 中(防止客户端直接访问)

如果它在类路径中,如何部署它取决于您的构建/打包系统。

例如,在 Eclipse 中,您可以将其保留在 src 的根目录下。如果您使用的是 Maven,它应该位于 src/main/resources 的根目录下。如果不在根目录下,请相应修改以下内容。

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:SpringBeans.xml</param-value>
</context-param>

否则提供应用相对路径,例如,

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/META-INF/SpringBeans.xml</param-value>
</context-param>

(或在WEB-INF 中,或您放置的任何位置。)

【讨论】:

    【解决方案2】:

    试试下面的代码,

    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
        <display-name>Example</display-name>
        <filter>
            <filter-name>struts2</filter-name>
            <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>struts2</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/classes/SpringBeans.xml
            </param-value>
        </context-param>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>
    

    如果您正在使用将 SpringBeans.xml 放在 src 中并且您正在使用 ANt 构建,那么它将被放置到 WEB-INF/classes 文件夹中。但是您将放置在另一个文件夹中,例如 /config 然后您已经编写了 build.xml 以在部署时将 *.xml 文件放置到 WEB-INF/classes 文件夹中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      • 2013-06-23
      • 1970-01-01
      相关资源
      最近更新 更多