Java 为什么我在运行简单的 Spring Boot 应用程序时总是得到状态为“404”的白标签错误页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39628930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do I always get Whitelabel Error Page with status "404" while running a simple Spring Boot Application
提问by Damien-Amen
My Controller
我的控制器
@Controller
//@RequestMapping("/")
//@ComponentScan("com.spring")
//@EnableAutoConfiguration
public class HomeController {
@Value("${framework.welcomeMessage}")
private String message;
@RequestMapping("/hello")
String home(ModelMap model) {
System.out.println("hittin the controller...");
model.addAttribute("welcomeMessage", "vsdfgfgd");
return "Hello World!";
}
@RequestMapping(value = "/indexPage", method = RequestMethod.GET)
String index(ModelMap model) {
System.out.println("hittin the index controller...");
model.addAttribute("welcomeMessage", message);
return "welcome";
}
@RequestMapping(value = "/indexPageWithModel", method = RequestMethod.GET)
ModelAndView indexModel(ModelMap model) {
System.out.println("hittin the indexPageWithModel controller...");
model.addAttribute("welcomeMessage", message);
return new ModelAndView("welcome", model);
}
}
My JSP (welcome.jsp) inside /WEB-INF/jsp(parent folder is WebContent)
我的 JSP(welcome.jsp)在 /WEB-INF/jsp 里面(父文件夹是 WebContent)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Welcome to Spring Boot</title>
</head>
<body>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Message: ${message}
</body>
</html>
My 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringBootPlay</groupId>
<artifactId>SpringBootPlay</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-log</artifactId>
<version>0.17</version>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
<start-class>com.spring.play.BootLoader</start-class>
<main.basedir>${basedir}/../..</main.basedir>
<m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>
My App Initializer
我的应用初始化程序
@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan({ "com.spring.controller" })
@PropertySources(value = { @PropertySource("classpath:/application.properties") })
public class BootLoader extends SpringBootServletInitializer {
final static Logger logger = Logger.getLogger(BootLoader.class);
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(BootLoader.class);
}
public static void main(String[] args) {
SpringApplication.run(BootLoader.class, args);
}
}
I even added thymeleaf
dependency to my pom. It still didn't work. When ever I hit localhost:8080/hello or /indexPage or /indexPageWithModel
it always says
我什thymeleaf
至在我的 pom.xml 中添加了依赖。它仍然没有奏效。当我打localhost:8080/hello or /indexPage or /indexPageWithModel
它时总是说
Whitelabel Error Page
白标错误页面
This application has no explicit mapping for /error, so you are seeing this as a fallback.
此应用程序没有明确的 /error 映射,因此您将其视为后备。
Wed Sep 21 21:34:18 EDT 2016 There was an unexpected error (type=Not Found, status=404). ]/WEB-INF/jsp/welcome.jsp
Wed Sep 21 21:34:18 EDT 2016 出现意外错误(类型=未找到,状态=404)。]/WEB-INF/jsp/welcome.jsp
My application.properties
我的 application.properties
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
framework.welcomeMessage=Welcome to Dashboard
Please help me. Thanks!
请帮我。谢谢!
采纳答案by Damien-Amen
Figured it out myself.
我自己想出来的。
When I converted my dynamic webproject into maven project it did not create the folder structure this way
当我将动态 webproject 转换为 maven 项目时,它没有以这种方式创建文件夹结构
src/main/java
src/main/java
and
和
src/main/resources
src/main/resources
and
和
src/main/webapp
src/main/webapp
I manually created myself and moved the jsp files from WebContent/WEB-INF/jsp
to src/main/webapp/WEB-INF/jsp
and modified the Java build path
in the project properties.
我手动创建了自己,并将 jsp 文件从WebContent/WEB-INF/jsp
到src/main/webapp/WEB-INF/jsp
并修改Java build path
了项目属性中的 。
Then I restarted the embedded tomcat
and tried it again. It worked.
然后我重新启动embedded tomcat
并再次尝试。有效。
回答by Yuvraj Singh
This is one of most common error that almost all the spring boot beginners face.
这是几乎所有 Spring Boot 初学者都面临的最常见错误之一。
Solution to this is very simple, - your Bootstrap class should know the package or the class path where it should refer in order access the component/controller. Hence you need to specify like :- @ComponentScan(basePackages= {"org.test.controller"})
对此的解决方案非常简单,您的 Bootstrap 类应该知道它应该引用的包或类路径,以便访问组件/控制器。因此,您需要指定如下:-@ComponentScan(basePackages= {"org.test.controller"})
P.S.- here "org.test.controller" is a qualified name of the package where I have kept my controller.
PS-这里的“org.test.controller”是我保存控制器的包的限定名称。
回答by Vijay Rajput
Cause of this type of errors is due to Bootstrap class is not aware of the location of the controller where it needs to be looked.
此类错误的原因是 Bootstrap 类不知道需要查看的控制器的位置。
In such cases, we need to specify the package or classpath which we can refer to using
@ComponentScan(basePackages={"com.sample.controller"})
where In my case I have specified package as com.sample.controller
.
在这种情况下,我们需要指定我们可以引用的包或类路径
@ComponentScan(basePackages={"com.sample.controller"})
where 在我的情况下,我已将包指定为com.sample.controller
.