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

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

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文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<servlet>
		<servlet-name>fitTrackerServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/config/servlet-config.xml</param-value>
		</init-param>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>fitTrackerServlet</servlet-name>
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>
	
	<!-- 其他 servlet-mapping 配置省略 -->
	
	<display-name>Archetype Created Web Application</display-name>
</web-app>

我的servlet-config.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"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		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-3.2.xsd">

	<!-- 其他配置省略 -->
	
	<!-- 注解驱动的 MVC -->
	<mvc:annotation-driven/>
	
	<!-- 资源处理器配置 -->
	<mvc:resources location="assets" mapping="/assets/**"/>
	<mvc:resources location="pdfs" mapping="/pdfs/**"/>
	
	<!-- 组件扫描配置 -->
	<context:component-scan base-package="com.pluralsight"/>
	
	<!-- 视图解析器和内容协商配置 -->
	<!-- 其他配置省略 -->
	
	<!-- 国际化拦截器配置 -->
	<!-- 其他配置省略 -->
	
	<!-- 视图解析器配置 -->
	<!-- 其他配置省略 -->

</beans>

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

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<!-- 其他内容省略 -->

<form:form commandName="goal">
	<!-- 表单内容省略 -->
</form:form>

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

package com.pluralsight.controller;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.pluralsight.model.Goal;

@Controller
@SessionAttributes("goal")
public class GoalController {

	@RequestMapping(value = "addGoal", method = RequestMethod.GET)
	public String addGoal(Model model) {
		Goal goal = new Goal();
		goal.setMinutes(10);
		model.addAttribute("goal", goal);
		
		return "addGoal";
	}
	
	@RequestMapping(value = "addGoal", method = RequestMethod.POST)
	public String updateGoal(@Valid @ModelAttribute("goal") Goal goal, BindingResult result) {
		// 处理表单提交逻辑
		// 其他内容省略
	}
}

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

英文:

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:

&lt;web-app version=&quot;2.5&quot; xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;
	xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;&gt;

	&lt;servlet&gt;
		&lt;servlet-name&gt;fitTrackerServlet&lt;/servlet-name&gt;
		&lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
		&lt;init-param&gt;
			&lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
			&lt;param-value&gt;/WEB-INF/config/servlet-config.xml&lt;/param-value&gt;
		&lt;/init-param&gt;
	&lt;/servlet&gt;
	
	&lt;servlet-mapping&gt;
		&lt;servlet-name&gt;fitTrackerServlet&lt;/servlet-name&gt;
		&lt;url-pattern&gt;*.html&lt;/url-pattern&gt;
	&lt;/servlet-mapping&gt;
	
	&lt;servlet-mapping&gt;
		&lt;servlet-name&gt;fitTrackerServlet&lt;/servlet-name&gt;
		&lt;url-pattern&gt;/pdfs/**&lt;/url-pattern&gt;
	&lt;/servlet-mapping&gt;
	
	&lt;servlet-mapping&gt;
		&lt;servlet-name&gt;fitTrackerServlet&lt;/servlet-name&gt;
		&lt;url-pattern&gt;/images/**&lt;/url-pattern&gt;
	&lt;/servlet-mapping&gt;
	
	&lt;servlet-mapping&gt;
		&lt;servlet-name&gt;fitTrackerServlet&lt;/servlet-name&gt;
		&lt;url-pattern&gt;*.json&lt;/url-pattern&gt;
	&lt;/servlet-mapping&gt;
	
	&lt;display-name&gt;Archetype Created Web Application&lt;/display-name&gt;
&lt;/web-app&gt;

My servlet-config.xml has this:

&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
	xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
	xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;
	xmlns:p=&quot;http://www.springframework.org/schema/p&quot;
	xsi:schemaLocation=&quot;http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		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-3.2.xsd&quot;&gt;

	&lt;mvc:annotation-driven/&gt;
	
	&lt;mvc:resources location=&quot;assets&quot; mapping=&quot;/assets/**&quot;/&gt;
	&lt;mvc:resources location=&quot;pdfs&quot; mapping=&quot;/pdfs/**&quot;/&gt;
	
	&lt;context:component-scan base-package=&quot;com.pluralsight&quot;/&gt;
	
	&lt;bean class=&quot;org.springframework.web.servlet.view.ContentNegotiatingViewResolver&quot;&gt;
		&lt;property name=&quot;order&quot; value=&quot;1&quot; /&gt;
		&lt;property name=&quot;contentNegotiationManager&quot;&gt;
			&lt;bean class=&quot;org.springframework.web.accept.ContentNegotiationManager&quot;&gt;
				&lt;constructor-arg&gt;
					&lt;bean class=&quot;org.springframework.web.accept.PathExtensionContentNegotiationStrategy&quot;&gt;
						&lt;constructor-arg&gt;
							&lt;map&gt;
								&lt;entry key=&quot;json&quot; value=&quot;application/json&quot; /&gt;
								&lt;entry key=&quot;xml&quot; value=&quot;application/xml&quot; /&gt;
							&lt;/map&gt;
						&lt;/constructor-arg&gt;
					&lt;/bean&gt;
				&lt;/constructor-arg&gt;
			&lt;/bean&gt;
		&lt;/property&gt;
		
		&lt;property name=&quot;defaultViews&quot;&gt;
			&lt;list&gt;
				&lt;bean class=&quot;org.springframework.web.servlet.view.json.MappingJacksonJsonView&quot; /&gt;
				&lt;bean class=&quot;org.springframework.web.servlet.view.xml.MarshallingView&quot;&gt;
					&lt;constructor-arg&gt;
						&lt;bean class=&quot;org.springframework.oxm.xstream.XStreamMarshaller&quot;&gt;
							&lt;property name=&quot;autodetectAnnotations&quot; value=&quot;true&quot; /&gt;
						&lt;/bean&gt;
					&lt;/constructor-arg&gt;
				&lt;/bean&gt;
			&lt;/list&gt;
		&lt;/property&gt;
	&lt;/bean&gt;
	
	&lt;mvc:interceptors&gt;
		&lt;bean class=&quot;org.springframework.web.servlet.i18n.LocaleChangeInterceptor&quot; p:paramName=&quot;language&quot;/&gt;
	&lt;/mvc:interceptors&gt;
	
	&lt;bean id=&quot;localeResolver&quot; class=&quot;org.springframework.web.servlet.i18n.SessionLocaleResolver&quot; p:defaultLocale=&quot;en&quot;/&gt;
	
	&lt;bean id=&quot;messageSource&quot; 
	class=&quot;org.springframework.context.support.ResourceBundleMessageSource&quot; 
	p:basename=&quot;messages&quot;&gt;&lt;/bean&gt;
	
	&lt;bean class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot; 
	 p:prefix=&quot;/WEB-INF/jsp/&quot; p:suffix=&quot;.jsp&quot; p:order=&quot;2&quot;/&gt;
	 	
	 &lt;bean class=&quot;org.springframework.web.servlet.view.BeanNameViewResolver&quot; p:order=&quot;0&quot;/&gt;

&lt;/beans&gt;

This is my addGoal.jsp file

    pageEncoding=&quot;ISO-8859-1&quot;%&gt;

&lt;%@ taglib prefix=&quot;form&quot; uri=&quot;http://www.springframework.org/tags/form&quot; %&gt;


&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
  
  &lt;head&gt;
    &lt;meta charset=&quot;utf-8&quot;&gt;
    &lt;title&gt;
    &lt;/title&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;!-- Le styles --&gt;
    &lt;link href=&quot;assets/css/bootstrap.css&quot; rel=&quot;stylesheet&quot;&gt;
    &lt;style&gt;
      body { padding-top: 60px; /* 60px to make the container go all the way
      to the bottom of the topbar */ }
    &lt;/style&gt;
    &lt;link href=&quot;assets/css/bootstrap-responsive.css&quot; rel=&quot;stylesheet&quot;&gt;
    &lt;!-- Le HTML5 shim, for IE6-8 support of HTML5 elements --&gt;
    &lt;!--[if lt IE 9]&gt;
      &lt;script src=&quot;http://html5shim.googlecode.com/svn/trunk/html5.js&quot;&gt;
      &lt;/script&gt;
    &lt;![endif]--&gt;
    &lt;!-- Le fav and touch icons --&gt;
    &lt;link rel=&quot;shortcut icon&quot; href=&quot;assets/ico/favicon.ico&quot;&gt;
    &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;
    &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;
    &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;
    &lt;link rel=&quot;apple-touch-icon-precomposed&quot; href=&quot;assets/ico/apple-touch-icon-57-precomposed.png&quot;&gt;
    &lt;style&gt;
		.error {
			color: #ff0000;
		}
		
		.errorblock {
			color: #000;
			background-color: #ffEEEE;
			border: 3px solid #ff0000;
			padding: 8px;
			margin: 16px;
		}
	&lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div class=&quot;navbar navbar-fixed-top navbar-inverse&quot;&gt;
      &lt;div class=&quot;navbar-inner&quot;&gt;
        &lt;div class=&quot;container&quot;&gt;
          &lt;a class=&quot;brand&quot; href=&quot;#&quot;&gt;
            Add Goal
          &lt;/a&gt;
          &lt;ul class=&quot;nav&quot;&gt;
          &lt;/ul&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;container&quot;&gt;
      &lt;div&gt;
        &lt;h1&gt;
          Add Goal
        &lt;/h1&gt;
        &lt;p&gt;
          Add your workout goal in minutes for the day.
          &lt;br&gt;
          &amp;nbsp;
        &lt;/p&gt;
      &lt;/div&gt;
      
      &lt;form:form commandName=&quot;goal&quot;&gt;
		&lt;form:errors path=&quot;*&quot; cssClass=&quot;errorblock&quot; element=&quot;div&quot; /&gt;
			&lt;label for=&quot;textinput1&quot;&gt;
	          Enter Minutes:
	        &lt;/label&gt;	
			&lt;form:input path=&quot;minutes&quot; cssErrorClass=&quot;error&quot; /&gt;
			&lt;form:errors path=&quot;minutes&quot; cssClass=&quot;error&quot; /&gt;
			&lt;br/&gt;
			&lt;input type=&quot;submit&quot; class=&quot;btn&quot; value=&quot;Enter Goal Minutes&quot;/&gt;
	  &lt;/form:form&gt;
     
      &lt;div class=&quot;control-group&quot;&gt;
      &lt;/div&gt;
    &lt;/div&gt;

     &lt;script src=&quot;jquery-1.8.3.js&quot;&gt;
    &lt;/script&gt;
    &lt;script src=&quot;assets/js/bootstrap.js&quot;&gt;
    &lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

My GoalController.java


import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.pluralsight.model.Goal;

@Controller
@SessionAttributes(&quot;goal&quot;)
public class GoalController {

	@RequestMapping(value = &quot;addGoal&quot;, method = RequestMethod.GET)
	public String addGoal(Model model) {
		Goal goal = new Goal();
		goal.setMinutes(10);
		model.addAttribute(&quot;goal&quot;, goal);
		
		return &quot;addGoal&quot;;
	}
	
	@RequestMapping(value = &quot;addGoal&quot;, method = RequestMethod.POST)
	public String updateGoal(@Valid @ModelAttribute(&quot;goal&quot;) Goal goal, BindingResult result) {
		
		System.out.println(&quot;result has errors: &quot; + result.hasErrors());
		
		System.out.println(&quot;Goal set: &quot; + goal.getMinutes());
		
		if(result.hasErrors()) {
			return &quot;addGoal&quot;;
		}
		
		return &quot;redirect:index.jsp&quot;;
	}
	
}

And I'm having this exception

	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
	org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
	org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1201)
	org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:654)
	org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2532)
	org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2521)
	java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	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中添加以下内容:

<!-- 配置Spring MVC Dispatcher Servlet的URL映射 -->
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

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

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

英文:

Add this in web.xml

&lt;!-- Set up URL mapping for Spring MVC Dispatcher Servlet --&gt;
	&lt;servlet-mapping&gt;
		&lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
		&lt;url-pattern&gt;/&lt;/url-pattern&gt;
	&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:

确定