HTTP状态500 - servlet mvc-dispatcher的Servlet.init()抛出异常

时间:2017-06-20 17:09:23

标签: java spring spring-mvc tomcat web.xml

我正在尝试使用简单的Spring MVC项目,但我在Tom Cat上有例外

HTTP状态500 - servlet mvc-dispatcher的Servlet.init()抛出异常

输入例外报告

用于servlet mvc-dispatcher的

消息Servlet.init()抛出异常

说明服务器遇到内部错误,导致无法完成此请求。

例外

javax.servlet.ServletException:servlet mvc-dispatcher的Servlet.init()抛出异常     org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)     org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)     org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)     org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)     org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1104)     org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:684)     org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1519)     org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1475)     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) 根本原因

org.springframework.beans.factory.BeanDefinitionStoreException:从ServletContext资源[/WEB-INF/mvc-dispatcher-servlet.xml]解析XML文档的IOException;嵌套异常是java.io.FileNotFoundException:无法打开ServletContext资源[/WEB-INF/mvc-dispatcher-servlet.xml]     org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)     org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)     org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)     org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)     org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)     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:129)     org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:613)     org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:514)     org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)     org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)     org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)     org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)     org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)     org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)     javax.servlet.GenericServlet.init(GenericServlet.java:158)     org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)     org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)     org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)     org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)     org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1104)     org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:684)     org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1519)     org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1475)     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) 根本原因

java.io.FileNotFoundException:无法打开ServletContext资源[/WEB-INF/mvc-dispatcher-servlet.xml]     org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141)     org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)     org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)     org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)     org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)     org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)     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:129)     org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:613)     org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:514)     org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)     org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)     org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)     org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)     org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)     org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)     javax.servlet.GenericServlet.init(GenericServlet.java:158)     org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)     org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)     org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)     org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)     org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1104)     org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:684)     org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1519)     org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1475)     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) note备注根本原因的完整堆栈跟踪可在Apache Tomcat /8.0.42日志中。

我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring-servlet.xml</param-value>
    </context-param>

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

</web-app>

我的spring-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       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">

    <mvc:annotation-driven/>
    <context:component-scan base-package="controllers"/>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <mvc:default-servlet-handler />
</beans>

我的控制器:

package controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String home() {
        return "home";
    }
}

我的home.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
HELLO WORLD
</body>
</html>

3 个答案:

答案 0 :(得分:1)

发生错误,因为找不到mvc-dispatcher-servlet.xml文件。您需要在mvc-dispatcher-servlet.xml文件夹中创建WEB-INF文件。 对于前你可以参考以下链接: -

http://www.mkyong.com/spring-mvc/spring-mvc-hello-world-example/

答案 1 :(得分:0)

您应该为调度程序servlet位置(spring-servlet.xml)提供init-param属性。你是

您的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
     http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">

    //Dispatcher Servlet Config
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

答案 2 :(得分:0)

pls添加spring-core依赖项并可以使其适应

       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-core</artifactId>
           <version>{your version}</version>
       </dependency>