Spring MVC-内部服务器错误+找不到文件异常Servlet上下文资源

时间:2018-08-06 09:36:35

标签: spring spring-mvc

我试图创建一个简单的spring mvc应用程序,但出现此错误

  HTTP Status 500 – Internal Server Error  
 Type Exception Report

    Message Servlet.init() for servlet [dispatcher] threw exception

    Description The server encountered an unexpected condition that prevented it from fulfilling the request.

    Exception
    javax.servlet.ServletException: Servlet.init() for servlet [dispatcher] threw exception
        org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:491)
        org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
        org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
        org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
        org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:764)
        org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1388)
        org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)     java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        java.lang.Thread.run(Thread.java:745)


    Root Cause
    org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB_INF/spring-mvc-demo-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB_INF/spring-mvc-demo-servlet.xml]
        org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343)
        org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
        org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
        org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:223)
        org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:194)
        org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
        org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
        org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:133)
        org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:621)
        org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:522)
        org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:672)
        org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:638)
        org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:686)
        org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:554)
        org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:499)
        org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:172)
        javax.servlet.GenericServlet.init(GenericServlet.java:158)
        org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:491)
        org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
        org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
        org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
        org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:764)
        org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1388)
        org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        java.lang.Thread.run(Thread.java:745)


    Root Cause
    java.io.FileNotFoundException: Could not open ServletContext resource [/WEB_INF/spring-mvc-demo-servlet.xml]
        org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:159)
        org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
        org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
        org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
        org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:223)
        org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:194)
        org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
        org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
        org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:133)
        org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:621)
        org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:522)
        org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:672)
        org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:638)
        org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:686)
        org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:554)
        org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:499)
        org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:172)
        javax.servlet.GenericServlet.init(GenericServlet.java:158)
        org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:491)
        org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
        org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
        org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
        org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
        org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:764)
        org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1388)
        org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        java.lang.Thread.run(Thread.java:745)

我的代码如下:- spring-mvc-demo-servlet.xml

 <?xml version="1.0" encoding="UTF-8"?>
    <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"  
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation = "http://www.springframework.org/schema/beans     
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
         http://www.springframework.org/schema/mvc/spring-mvc.xsd">    
       <!--   Step3: Add Support For Component Scanning -->
       <context:component-scan base-package = "com.google" />     
       <!-- Step4: Add Support For Conversion, Formatting,Validation Support -->
       <mvc:annotation-driven/>       
       <!-- Step5: Define Spring View Resolver --> 
       <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name = "prefix" value = "/WEB-INF/view/" />
          <property name = "suffix" value = ".jsp" />
       </bean>    
    </beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id = "WebApp_ID" version = "2.4"
   xmlns = "http://java.sun.com/xml/ns/j2ee" 
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name> Spring-mvc-demo</display-name>

    <!-- Spring MVC Config -->

    <!--  Step1: Configure Spring MVC Dispatcher Servlet -->
    <servlet>
     <servlet-name>dispatcher</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <init-param>
       <param-name>ContextConfigLocation</param-name>
       <param-value>WEB_INF/spring-mvc-demo-servlet.xml</param-value>     
     </init-param>
     <load-on-startup>1</load-on-startup>
    </servlet>


    <!--  Step2: Set up URL Mapping for spring dispatcher Servlet -->
    <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>

 </web-app>

HomeController.java

package com.google;    
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;   
@Controller
public class HomeController {       
    @RequestMapping("/")
    public String showMyPage() {
        return "main-menu";
    }           
}

我已经创建了main-menu.jsp页面,并将其保存在WEB_INF的 view 文件夹中。同样这两个xml文件都在WEB-INF中。当我运行时,出现此异常。是由于init-params引起的,但我认为这是正确的,它应该可以正常工作。那么问题出在哪里,请帮忙。

1 个答案:

答案 0 :(得分:0)

  

根本原因....       java.io.FileNotFoundException:无法打开ServletContext资源[/WEB_INF/spring-mvc-demo-servlet.xml]

发生错误,可能是因为标签<param-value> WEB_INF / spring-mvc-demo-servlet.xml </ param-value>中“ spring-mvc-demo-servlet.xml”的路径指定不正确