【问题标题】:Tomcat error while running spring mvc web application运行spring mvc web应用程序时出现Tomcat错误
【发布时间】:2014-09-04 09:19:47
【问题描述】:

实际上我正在使用 eclipse 在 tomcat 服务器上运行 spring mvc web 应用程序,但是在启动 tomcat 时出现以下错误

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

我正在使用以下 xml 配置文件

  1. Web.xml

    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring MVC Form Handling</display-name>
    
    <servlet>
        <servlet-name>HelloWeb</servlet-name>
        <servlet-class>
           org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>HelloWeb</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    

2.HelloWeb-servlet.xml

 <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:component-scan base-package="com.tutorialspoint" />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   </bean>

</bean

【问题讨论】:

  • 发布您的 web.xml 和完整的堆栈跟踪

标签: spring tomcat


【解决方案1】:

默认情况下,Spring 会在 /WEB-INF/your-servlet-name-servlet.xml 中查找文件,如果在 init-param 属性中没有专门指定的话名称为“contextConfigLocation”。 这意味着它获取元素的值并将 -servlet.xml 附加到它并在 WEB-INF 中查找它。

例如:

    <servlet>
        <servlet-name>HelloWeb</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/web-application-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

这里 Spring 会选择 WEB-INF 文件夹下的 web-application-config.xml 文件,因为这是专门指定的。如果未指定,它将查找 /WEB-INF/HelloWeb-servlet.xml,因为“HelloWeb”是 servlet 名称。

所以请确保您在 WEB-INF 文件夹中有此文件。

【讨论】:

  • 您好先生,我使用了 contextConfigLocation 但在 Eclipse 中启动 tomacat 时显示此错误。我使用了你提到的配置,但它不起作用。
【解决方案2】:

错误java.io.FileNotFoundException 表示无法打开/WEB-INF/HelloWeb-servlet.xml
当位置错误并且文件不存在时,这很典型。请检查文件名是否正确。还可以尝试删除开头的斜杠“/”。

编辑: 如果文件是从同一位置引用的,您也可以尝试只输入HelloWeb-servlet.xml 而不输入目录。

【讨论】:

  • 嗨,先生,我是 spring mvc 的新手。我正在使用以下 xml 文件
  • 嗨,先生,其实我是 spring 新手,我提供了上面的 xml 文件,请指导我获取这些文件
  • HelloWeb-servlet.xml 在哪里引用(定义)?
  • 先生您能举个例子吗
  • 您在整个项目中哪里有一个 String 'HelloWeb-servlet.xml' ?查看所有 XML 文件。然后尝试调整我在答案中提到的路径。
猜你喜欢
  • 2014-04-24
  • 2015-04-18
  • 1970-01-01
  • 2015-09-11
  • 2022-01-21
  • 1970-01-01
  • 2021-06-17
  • 2020-01-28
  • 2017-01-04
相关资源
最近更新 更多