【问题标题】:Spring how to use WebApplicationContex in web.xmlSpring如何在web.xml中使用WebApplicationContex
【发布时间】:2016-09-26 13:47:55
【问题描述】:

我使用 Spring 并编写了以下 web.xml

 <web-app>
 <display-name>Archetype Created Web Application</display-name>
 <servlet>
 <servlet-name>appServlet</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <init-param>
 <param-name>configContextLocation</param-name>
 <param-value>/WEB-INF/spring/servlet-context.xml</param-value>
 </init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

当我执行网页时出现错误

org.springframework.beans.factory.BeanDefinitionStoreException: IOException
parsing XML document from ServletContext resource [/WEB-INF/appServlet-
servlet.xml]; nested exception is java.io.FileNotFoundException: Could not  
open ServletContext resource [/WEB-INF/appServlet-servlet.xml]

我在论坛上看到它是什么,因为我需要一个 appServlet-servlet.xml,因为我的 servlet 被称为 appServlet。

我有一些问题。我是否调用了我的 servlet-context.xml appServlet-servlet.xml?

如何修复我的 web.xml?

【问题讨论】:

    标签: spring servlets web.xml


    【解决方案1】:

    http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-servlet

    从 Spring 文档中可以看出:

    在初始化 DispatcherServlet 时,Spring MVC 在您的 Web 应用程序的 WEB-INF 目录中查找名为 [servlet-name]-servlet.xml 的文件

    把你servlet-context.xml改成appServlet-servlet.xml直接放到WEB-INF下。

    引导 spring 上下文的更好方法是定义一个监听器

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    

    并在

    中定义Spring上下文的位置
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/servlet-context.xml</param-value>
    </context-param>
    

    【讨论】:

      【解决方案2】:

      Spring 不是从 web.xml 引导的,因为你没有魔法:

      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      

      我假设你必须有一些 java config 或 spring boot。但是结果,它没有使用web.xml配置。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-18
        • 2017-07-08
        • 2011-09-21
        • 2011-07-23
        • 1970-01-01
        • 2014-06-20
        • 2012-11-13
        • 2014-12-31
        相关资源
        最近更新 更多