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

匿名 (未验证) 提交于 2019-12-03 07:47:04

问题:

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

This is my 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

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

<html>     <head>         <title>First MVC Application</title>     </head>     <body>      <h1>First MVC Application</h1>     <h2>${welcomemessage}</h2>            </body> </html> 

This is my HelloController class

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.

回答1:

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

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


回答2:

In the spring-dispatcher-servlet.xml instead of the below 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: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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!