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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34197237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:24:02  来源:igfitidea点击:

HTTP Status 500 - Servlet.init() for servlet spring-dispatcher threw exception

java

提问by Ankit Jain

I am learning Spring MVCand when I am trying to run the html file it gives the error HTTP Status 500- Servlet.init()for servlet spring-dispatcher threw exception

我正在学习Spring MVC,当我尝试运行 html 文件时,它给出了错误 HTTP Status 500- Servlet.init()for servlet spring-dispatcher throw exception

This is my web.xml

这是我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>`<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>FirstSpringMVC</display-name>

    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    </servlet>


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

</web-app>

This is my spring-dispatcher-servlet

这是我的spring-dispatcher-servlet

<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


    <bean id="HandlerMapping" class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

    <bean name = "/welcome.html" class = "com.ankitud.hellocontroller.HelloController"/>

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

        <property name = "suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

This is my jsp page

这是我的jsp页面

<html>
    <head>
        <title>First MVC Application</title>
    </head>
    <body>

    <h1>First MVC Application</h1>
    <h2>${welcomemessage}</h2>      

    </body>
</html>

This is my HelloController class

这是我的 HelloController 类

package com.ankitud.hellocontroller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception{

        ModelAndView modelandview = new ModelAndView("HelloPage");
        modelandview.addObject("welcomemessage", "Hi User, welcome to the first Spring MVC application");

        return modelandview;

    }

}

And this is the exception that I am getting

这是我得到的例外

javax.servlet.ServletException: Servlet.init() for servlet spring-dispatcher threw exception
 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    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)

I am working with Eclipse 4.5.1 , Tomcat 8.0.30 and Spring 4.2.3.

我正在使用 Eclipse 4.5.1、Tomcat 8.0.30 和 Spring 4.2.3。

采纳答案by Abdelhak

You missed this block from your web.xml try to add it:

您从 web.xml 中错过了此块,请尝试添加它:

 <web-app...>

  <!-------- DispatcherServlet definition goes here----->
   ....
   <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/spring-dispatcher-servlet.xml</param-value>
   </context-param>

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

回答by abhineet

In the spring-dispatcher-servlet.xml instead of the below code

在 spring-dispatcher-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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

try using this code

尝试使用此代码

<?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"
       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">

I tried and it worked for me. Happy coding.

我试过了,它对我有用。快乐编码。

回答by Vatsal Shrivastav

This might be too late but I he's the solution for any help seeker in future. There are 2 blocks which are missing in the code:

这可能为时已晚,但我他是未来任何寻求帮助者的解决方案。代码中缺少 2 个块:

  1. In your web.xml, inside you need to add
  1. 在你的 web.xml 里面,你需要添加

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

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

  1. The above change will help you get rid of HTTP Status error 500. But you would now end up getting HTTP Status error 404 and this is because in your class, i.e HelloController.java you need to import add @Controller annotation just above your class definition like this
  1. 上述更改将帮助您摆脱 HTTP 状态错误 500。但您现在最终会收到 HTTP 状态错误 404,这是因为在您的类中,即 HelloController.java,您需要在类定义上方导入添加 @Controller 注释像这样

@Controller
public class HelloController extends AbstractController {

    //Your class content goes here

}

This will solve your routing issues and now you can visit /welcome.html. Though you would still see ${welcomemessage} as it is when you route to /welcome.html. Finally in order to see the actual object data on the jsp page, simply add

这将解决您的路由问题,现在您可以访问 /welcome.html。虽然您仍然会看到 ${welcomemessage},但当您路由到 /welcome.html 时,它仍然如此。最后为了在jsp页面看到实际的对象数据,只需添加

<%@ page contentType="text/html; charset=UTF-8" isELIgnored="false" %>

That's it! This would help you access your welcomemessage object in your Jsp page. Hope this helps :)

就是这样!这将帮助您访问 Jsp 页面中的welcomemessage 对象。希望这可以帮助 :)