当前位置: 首页 > 知识库问答 >
问题:

HTTP状态500-Servlet。servlet spring dispatcher的init()引发异常

林冥夜
2023-03-14

我正在学习Spring MVC,当我试图运行html文件时,它会给出错误HTTP Status 500-Servlet。servlet spring dispatcher的init()引发异常

这是我的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>

这是我的Spring调度员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>

这是我的jsp页面

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

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

    </body>
</html>

这是我的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;

    }

}

这是我得到的一个例外

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)

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

共有3个答案

唐煜
2023-03-14

这可能太晚了,但他是未来任何寻求帮助者的解决方案。代码中缺少两个块:

  1. 在你的网上。xml,里面需要添加
html lang-html prettyprint-override"><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>
祁星阑
2023-03-14

在Spring。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">

尝试使用这个代码

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

我试过了,结果成功了。快乐编码。

戚建白
2023-03-14

你错过了这个块从你的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>
 类似资料: