HTTP 500 – 内部服务器错误。Servlet.init() 用于 servlet spring-dispatcher 抛出异常。

huangapple go评论132阅读模式
英文:

HTTP 500 - INTERNAL SERVER ERROR. Servlet.init() for servlet spring-dispatcher threw exception

问题

以下是翻译好的部分:

我是一个Spring MVC的初学者,正在跟随一份关于如何使用它的教程。在我的主页面,即"http://localhost:8080/FitnessTracker/"上,我没有任何问题,但是当我尝试访问例如"http://localhost:8080/FitnessTracker/addGoal.html"时,它显示了我在标题中写的内容。

我的web.xml文件如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  5. <servlet>
  6. <servlet-name>fitTrackerServlet</servlet-name>
  7. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  8. <init-param>
  9. <param-name>contextConfigLocation</param-name>
  10. <param-value>/WEB-INF/config/servlet-config.xml</param-value>
  11. </init-param>
  12. </servlet>
  13. <servlet-mapping>
  14. <servlet-name>fitTrackerServlet</servlet-name>
  15. <url-pattern>*.html</url-pattern>
  16. </servlet-mapping>
  17. <!-- 其他 servlet-mapping 配置省略 -->
  18. <display-name>Archetype Created Web Application</display-name>
  19. </web-app>

我的servlet-config.xml文件如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xmlns:p="http://www.springframework.org/schema/p"
  7. xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
  10. <!-- 其他配置省略 -->
  11. <!-- 注解驱动的 MVC -->
  12. <mvc:annotation-driven/>
  13. <!-- 资源处理器配置 -->
  14. <mvc:resources location="assets" mapping="/assets/**"/>
  15. <mvc:resources location="pdfs" mapping="/pdfs/**"/>
  16. <!-- 组件扫描配置 -->
  17. <context:component-scan base-package="com.pluralsight"/>
  18. <!-- 视图解析器和内容协商配置 -->
  19. <!-- 其他配置省略 -->
  20. <!-- 国际化拦截器配置 -->
  21. <!-- 其他配置省略 -->
  22. <!-- 视图解析器配置 -->
  23. <!-- 其他配置省略 -->
  24. </beans>

这是我的addGoal.jsp文件的一部分:

  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2. pageEncoding="ISO-8859-1"%>
  3. <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
  4. <!-- 其他内容省略 -->
  5. <form:form commandName="goal">
  6. <!-- 表单内容省略 -->
  7. </form:form>

这是我的GoalController.java文件的一部分:

  1. package com.pluralsight.controller;
  2. import javax.validation.Valid;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.ui.Model;
  5. import org.springframework.validation.BindingResult;
  6. import org.springframework.web.bind.annotation.ModelAttribute;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import org.springframework.web.bind.annotation.SessionAttributes;
  10. import com.pluralsight.model.Goal;
  11. @Controller
  12. @SessionAttributes("goal")
  13. public class GoalController {
  14. @RequestMapping(value = "addGoal", method = RequestMethod.GET)
  15. public String addGoal(Model model) {
  16. Goal goal = new Goal();
  17. goal.setMinutes(10);
  18. model.addAttribute("goal", goal);
  19. return "addGoal";
  20. }
  21. @RequestMapping(value = "addGoal", method = RequestMethod.POST)
  22. public String updateGoal(@Valid @ModelAttribute("goal") Goal goal, BindingResult result) {
  23. // 处理表单提交逻辑
  24. // 其他内容省略
  25. }
  26. }

同时,你提到了一个异常,但是并没有提供完整的异常信息。如果你需要关于异常的帮助,请提供完整的异常堆栈信息和其他相关信息。

英文:

I'm a Spring MVC beginner and I'm following a tutorial about how to use it. On my main page which is "http://localhost:8080/FitnessTracker/" I don't have any problems, but when I try to access to, for example, "http://localhost:8080/FitnessTracker/addGoal.html", it says what i wrote on the title.

My web.xml is the next one:

  1. &lt;web-app version=&quot;2.5&quot; xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;
  2. xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  3. xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;&gt;
  4. &lt;servlet&gt;
  5. &lt;servlet-name&gt;fitTrackerServlet&lt;/servlet-name&gt;
  6. &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
  7. &lt;init-param&gt;
  8. &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
  9. &lt;param-value&gt;/WEB-INF/config/servlet-config.xml&lt;/param-value&gt;
  10. &lt;/init-param&gt;
  11. &lt;/servlet&gt;
  12. &lt;servlet-mapping&gt;
  13. &lt;servlet-name&gt;fitTrackerServlet&lt;/servlet-name&gt;
  14. &lt;url-pattern&gt;*.html&lt;/url-pattern&gt;
  15. &lt;/servlet-mapping&gt;
  16. &lt;servlet-mapping&gt;
  17. &lt;servlet-name&gt;fitTrackerServlet&lt;/servlet-name&gt;
  18. &lt;url-pattern&gt;/pdfs/**&lt;/url-pattern&gt;
  19. &lt;/servlet-mapping&gt;
  20. &lt;servlet-mapping&gt;
  21. &lt;servlet-name&gt;fitTrackerServlet&lt;/servlet-name&gt;
  22. &lt;url-pattern&gt;/images/**&lt;/url-pattern&gt;
  23. &lt;/servlet-mapping&gt;
  24. &lt;servlet-mapping&gt;
  25. &lt;servlet-name&gt;fitTrackerServlet&lt;/servlet-name&gt;
  26. &lt;url-pattern&gt;*.json&lt;/url-pattern&gt;
  27. &lt;/servlet-mapping&gt;
  28. &lt;display-name&gt;Archetype Created Web Application&lt;/display-name&gt;
  29. &lt;/web-app&gt;

My servlet-config.xml has this:

  1. &lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
  2. xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  3. xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
  4. xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;
  5. xmlns:p=&quot;http://www.springframework.org/schema/p&quot;
  6. xsi:schemaLocation=&quot;http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd&quot;&gt;
  9. &lt;mvc:annotation-driven/&gt;
  10. &lt;mvc:resources location=&quot;assets&quot; mapping=&quot;/assets/**&quot;/&gt;
  11. &lt;mvc:resources location=&quot;pdfs&quot; mapping=&quot;/pdfs/**&quot;/&gt;
  12. &lt;context:component-scan base-package=&quot;com.pluralsight&quot;/&gt;
  13. &lt;bean class=&quot;org.springframework.web.servlet.view.ContentNegotiatingViewResolver&quot;&gt;
  14. &lt;property name=&quot;order&quot; value=&quot;1&quot; /&gt;
  15. &lt;property name=&quot;contentNegotiationManager&quot;&gt;
  16. &lt;bean class=&quot;org.springframework.web.accept.ContentNegotiationManager&quot;&gt;
  17. &lt;constructor-arg&gt;
  18. &lt;bean class=&quot;org.springframework.web.accept.PathExtensionContentNegotiationStrategy&quot;&gt;
  19. &lt;constructor-arg&gt;
  20. &lt;map&gt;
  21. &lt;entry key=&quot;json&quot; value=&quot;application/json&quot; /&gt;
  22. &lt;entry key=&quot;xml&quot; value=&quot;application/xml&quot; /&gt;
  23. &lt;/map&gt;
  24. &lt;/constructor-arg&gt;
  25. &lt;/bean&gt;
  26. &lt;/constructor-arg&gt;
  27. &lt;/bean&gt;
  28. &lt;/property&gt;
  29. &lt;property name=&quot;defaultViews&quot;&gt;
  30. &lt;list&gt;
  31. &lt;bean class=&quot;org.springframework.web.servlet.view.json.MappingJacksonJsonView&quot; /&gt;
  32. &lt;bean class=&quot;org.springframework.web.servlet.view.xml.MarshallingView&quot;&gt;
  33. &lt;constructor-arg&gt;
  34. &lt;bean class=&quot;org.springframework.oxm.xstream.XStreamMarshaller&quot;&gt;
  35. &lt;property name=&quot;autodetectAnnotations&quot; value=&quot;true&quot; /&gt;
  36. &lt;/bean&gt;
  37. &lt;/constructor-arg&gt;
  38. &lt;/bean&gt;
  39. &lt;/list&gt;
  40. &lt;/property&gt;
  41. &lt;/bean&gt;
  42. &lt;mvc:interceptors&gt;
  43. &lt;bean class=&quot;org.springframework.web.servlet.i18n.LocaleChangeInterceptor&quot; p:paramName=&quot;language&quot;/&gt;
  44. &lt;/mvc:interceptors&gt;
  45. &lt;bean id=&quot;localeResolver&quot; class=&quot;org.springframework.web.servlet.i18n.SessionLocaleResolver&quot; p:defaultLocale=&quot;en&quot;/&gt;
  46. &lt;bean id=&quot;messageSource&quot;
  47. class=&quot;org.springframework.context.support.ResourceBundleMessageSource&quot;
  48. p:basename=&quot;messages&quot;&gt;&lt;/bean&gt;
  49. &lt;bean class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;
  50. p:prefix=&quot;/WEB-INF/jsp/&quot; p:suffix=&quot;.jsp&quot; p:order=&quot;2&quot;/&gt;
  51. &lt;bean class=&quot;org.springframework.web.servlet.view.BeanNameViewResolver&quot; p:order=&quot;0&quot;/&gt;
  52. &lt;/beans&gt;

This is my addGoal.jsp file

  1. pageEncoding=&quot;ISO-8859-1&quot;%&gt;
  2. &lt;%@ taglib prefix=&quot;form&quot; uri=&quot;http://www.springframework.org/tags/form&quot; %&gt;
  3. &lt;!DOCTYPE html&gt;
  4. &lt;html lang=&quot;en&quot;&gt;
  5. &lt;head&gt;
  6. &lt;meta charset=&quot;utf-8&quot;&gt;
  7. &lt;title&gt;
  8. &lt;/title&gt;
  9. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  10. &lt;!-- Le styles --&gt;
  11. &lt;link href=&quot;assets/css/bootstrap.css&quot; rel=&quot;stylesheet&quot;&gt;
  12. &lt;style&gt;
  13. body { padding-top: 60px; /* 60px to make the container go all the way
  14. to the bottom of the topbar */ }
  15. &lt;/style&gt;
  16. &lt;link href=&quot;assets/css/bootstrap-responsive.css&quot; rel=&quot;stylesheet&quot;&gt;
  17. &lt;!-- Le HTML5 shim, for IE6-8 support of HTML5 elements --&gt;
  18. &lt;!--[if lt IE 9]&gt;
  19. &lt;script src=&quot;http://html5shim.googlecode.com/svn/trunk/html5.js&quot;&gt;
  20. &lt;/script&gt;
  21. &lt;![endif]--&gt;
  22. &lt;!-- Le fav and touch icons --&gt;
  23. &lt;link rel=&quot;shortcut icon&quot; href=&quot;assets/ico/favicon.ico&quot;&gt;
  24. &lt;link rel=&quot;apple-touch-icon-precomposed&quot; sizes=&quot;144x144&quot; href=&quot;assets/ico/apple-touch-icon-144-precomposed.png&quot;&gt;
  25. &lt;link rel=&quot;apple-touch-icon-precomposed&quot; sizes=&quot;114x114&quot; href=&quot;assets/ico/apple-touch-icon-114-precomposed.png&quot;&gt;
  26. &lt;link rel=&quot;apple-touch-icon-precomposed&quot; sizes=&quot;72x72&quot; href=&quot;assets/ico/apple-touch-icon-72-precomposed.png&quot;&gt;
  27. &lt;link rel=&quot;apple-touch-icon-precomposed&quot; href=&quot;assets/ico/apple-touch-icon-57-precomposed.png&quot;&gt;
  28. &lt;style&gt;
  29. .error {
  30. color: #ff0000;
  31. }
  32. .errorblock {
  33. color: #000;
  34. background-color: #ffEEEE;
  35. border: 3px solid #ff0000;
  36. padding: 8px;
  37. margin: 16px;
  38. }
  39. &lt;/style&gt;
  40. &lt;/head&gt;
  41. &lt;body&gt;
  42. &lt;div class=&quot;navbar navbar-fixed-top navbar-inverse&quot;&gt;
  43. &lt;div class=&quot;navbar-inner&quot;&gt;
  44. &lt;div class=&quot;container&quot;&gt;
  45. &lt;a class=&quot;brand&quot; href=&quot;#&quot;&gt;
  46. Add Goal
  47. &lt;/a&gt;
  48. &lt;ul class=&quot;nav&quot;&gt;
  49. &lt;/ul&gt;
  50. &lt;/div&gt;
  51. &lt;/div&gt;
  52. &lt;/div&gt;
  53. &lt;div class=&quot;container&quot;&gt;
  54. &lt;div&gt;
  55. &lt;h1&gt;
  56. Add Goal
  57. &lt;/h1&gt;
  58. &lt;p&gt;
  59. Add your workout goal in minutes for the day.
  60. &lt;br&gt;
  61. &amp;nbsp;
  62. &lt;/p&gt;
  63. &lt;/div&gt;
  64. &lt;form:form commandName=&quot;goal&quot;&gt;
  65. &lt;form:errors path=&quot;*&quot; cssClass=&quot;errorblock&quot; element=&quot;div&quot; /&gt;
  66. &lt;label for=&quot;textinput1&quot;&gt;
  67. Enter Minutes:
  68. &lt;/label&gt;
  69. &lt;form:input path=&quot;minutes&quot; cssErrorClass=&quot;error&quot; /&gt;
  70. &lt;form:errors path=&quot;minutes&quot; cssClass=&quot;error&quot; /&gt;
  71. &lt;br/&gt;
  72. &lt;input type=&quot;submit&quot; class=&quot;btn&quot; value=&quot;Enter Goal Minutes&quot;/&gt;
  73. &lt;/form:form&gt;
  74. &lt;div class=&quot;control-group&quot;&gt;
  75. &lt;/div&gt;
  76. &lt;/div&gt;
  77. &lt;script src=&quot;jquery-1.8.3.js&quot;&gt;
  78. &lt;/script&gt;
  79. &lt;script src=&quot;assets/js/bootstrap.js&quot;&gt;
  80. &lt;/script&gt;
  81. &lt;/body&gt;
  82. &lt;/html&gt;

My GoalController.java

  1. import javax.validation.Valid;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.ui.Model;
  4. import org.springframework.validation.BindingResult;
  5. import org.springframework.web.bind.annotation.ModelAttribute;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.bind.annotation.SessionAttributes;
  9. import com.pluralsight.model.Goal;
  10. @Controller
  11. @SessionAttributes(&quot;goal&quot;)
  12. public class GoalController {
  13. @RequestMapping(value = &quot;addGoal&quot;, method = RequestMethod.GET)
  14. public String addGoal(Model model) {
  15. Goal goal = new Goal();
  16. goal.setMinutes(10);
  17. model.addAttribute(&quot;goal&quot;, goal);
  18. return &quot;addGoal&quot;;
  19. }
  20. @RequestMapping(value = &quot;addGoal&quot;, method = RequestMethod.POST)
  21. public String updateGoal(@Valid @ModelAttribute(&quot;goal&quot;) Goal goal, BindingResult result) {
  22. System.out.println(&quot;result has errors: &quot; + result.hasErrors());
  23. System.out.println(&quot;Goal set: &quot; + goal.getMinutes());
  24. if(result.hasErrors()) {
  25. return &quot;addGoal&quot;;
  26. }
  27. return &quot;redirect:index.jsp&quot;;
  28. }
  29. }

And I'm having this exception

  1. org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)
  2. org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
  3. org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)
  4. org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
  5. org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1201)
  6. org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:654)
  7. org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2532)
  8. org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2521)
  9. java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
  10. java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
  11. org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  12. java.base/java.lang.Thread.run(Thread.java:830)

What can I do to make this work? Thank you so much.

答案1

得分: -1

在web.xml中添加以下内容:

  1. <!-- 配置Spring MVC Dispatcher Servlet的URL映射 -->
  2. <servlet-mapping>
  3. <servlet-name>dispatcher</servlet-name>
  4. <url-pattern>/</url-pattern>
  5. </servlet-mapping>

在web.xml中,为什么要对单个servlet进行多次映射呢?
我的建议是,请移除所有这些servlet映射,然后只包含我上面告诉你的配置。

根据您的控制器代码,尝试访问 localhost:8080/FitnessTracker/addGoal。

英文:

Add this in web.xml

  1. &lt;!-- Set up URL mapping for Spring MVC Dispatcher Servlet --&gt;
  2. &lt;servlet-mapping&gt;
  3. &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
  4. &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  5. &lt;/servlet-mapping&gt;

In web.xml why are you having more than one servlet mapping of single servlet
My suggestion would be please remove all these servlet mapping and then just include which I have told you above

Try to access localhost:8080/FitnessTracker/addGoal as per your controller code

huangapple
  • 本文由 发表于 2020年7月27日 07:30:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63106847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定