[英]HTTP Status 500 - Servlet.init() for servlet fitTrackerServlet threw exception
This is my first time working with Spring and I am following the tutorial on PluralSight. 这是我第一次使用Spring,我正在关注PluralSight的教程。 Running the first example, I run into the error that is listed in the title. 运行第一个示例,我遇到标题中列出的错误。 In my console, I get errors like these. 在我的控制台中,我收到这样的错误。 I am using older versions of the dependencies to go through this course. 我正在使用旧版本的依赖项来完成本课程。 I've made sure to follow the tutorial closely so not sure if I messed up somewhere or something is deprecated. 我已经确保密切关注这个教程,所以不确定我是否搞砸了某个地方或某些东西已被弃用。 I am using http://localhost:8080/FitnessTracker/greeting.html 我正在使用http:// localhost:8080 / FitnessTracker / greeting.html
SEVERE: Context initialization failed
java.lang.IllegalArgumentException
SEVERE: StandardWrapper.Throwable
java.lang.IllegalArgumentException
SEVERE: Allocate exception for servlet fitTrackerServlet
java.lang.IllegalArgumentException
HelloController.java HelloController.java
package com.pluralsight.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping(value = "/greeting")
public String sayHello(Model model) {
model.addAttribute("greeting", "Hello World");
return "hello";
}
}
servlet-config.xml 的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:annotation-driven />
<context:component-scan base-package="com.pluralsight.controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>
</beans>
hello.jsp 为hello.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>${greeting}</h1>
</body>
</html>
web.xml web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" 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>
<display-name>Archetype Created Web Application</display-name>
</web-app>
pom.xml 的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.pluralsight</groupId>
<artifactId>FitnessTracker</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>FitnessTracker Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>FitnessTracker</finalName>
</build>
</project>
I had the same problem with the same PluralSight tutorial. 我在同一个PluralSight教程中遇到了同样的问题。 I had both Java 1.7 and Java 1.8 installed. 我安装了Java 1.7和Java 1.8。 The project apparently throws an exception with Java 1.8. 该项目显然抛出了Java 1.8的异常。 To rectify this situation you need to ensure that your Spring STS environment is set to use Java 1.7. 要纠正这种情况,您需要确保将Spring STS环境设置为使用Java 1.7。
The PluralSight tutorial now works for me. PluralSight教程现在适合我。 I am beginner to Java and Spring MVC myself so I am not sure if this was the best way to solve the problem. 我自己是Java和Spring MVC的初学者,所以我不确定这是否是解决问题的最佳方法。
Well, spring 3.2 needs java 7 or earlier. 那么,spring 3.2需要java 7或更早版本。 Instead of changing project properties, one another solution is just change the version number of spring mvc in pom.xml to 4.2.4.RELEASE instead of 3.2.0.RELEASE that you have used earlier. 另一个解决方案是将pom.xml中的spring mvc的版本号更改为4.2.4.RELEASE而不是之前使用的3.2.0.RELEASE,而不是更改项目属性。 Now, you can proceed normally with java 8. 现在,您可以正常使用java 8。
Below, I show the whole pom.xml with only change that I mentioned above. 下面,我展示了整个pom.xml,只有我上面提到的更改。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.pluralsight</groupId>
<artifactId>FitnessTracker</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>FitnessTracker Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>FitnessTracker</finalName>
</build>
</project>
Try with 4.1.5. 试试4.1.5。 It works for me. 这个对我有用。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
Try to use 3.2.3.RELEASE version wherever you have spring based dependency 尝试使用3.2.3.RELEASE版本,只要你有基于spring的依赖
I faced the same issue. 我遇到了同样的问题。 So what you have to do is to use Tomcat v7.0, It is because I was using Tomcat v9.0 and it does not work with JRE7 but JRE8 . 所以你要做的就是使用Tomcat v7.0,因为我使用的是Tomcat v9.0,它不适用于JRE7而是JRE8 。 So 所以
To fix this, simply change your servlet container's runtime environment to Java 7. In Eclipse: 要解决此问题,只需将servlet容器的运行时环境更改为Java 7.在Eclipse中:
JRE
section, select the Java 7 JRE. 从JRE
部分,选择Java 7 JRE。 Note that in order to be able to do this, you first need to download and install Java 7, and make the Java 7 JRE available in Eclipse. 请注意,为了能够执行此操作,首先需要下载并安装Java 7,并在Eclipse中提供Java 7 JRE。 To make Java 7 JRE available in Eclipse: 要在Eclipse中使Java 7 JRE可用:
I would also advise indicating the runtime environment being used in the server's name. 我还建议指出服务器名称中使用的运行时环境。 For example, in Eclipse, instead of letting the server name simply be "Apache Tomcat v7.0", make it "Apache Tomcat v7.0 - JRE 7". 例如,在Eclipse中,不要让服务器名称只是“Apache Tomcat v7.0”,而是将其设为“Apache Tomcat v7.0 - JRE 7”。 Of course, you should update this name if you change the runtime environment of this server later. 当然,如果稍后更改此服务器的运行时环境,则应更新此名称。
Source: http://www.codejava.net/ides/eclipse/how-to-change-java-runtime-environment-for-tomcat-in-eclipse 资料来源: http : //www.codejava.net/ides/eclipse/how-to-change-java-runtime-environment-for-tomcat-in-eclipse
NOTE: This answer applies to any other IDE as well. 注意:此答案也适用于任何其他IDE。 You just need to make sure that your servlet container runs under JRE 7. 您只需要确保您的servlet容器在JRE 7下运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.