![](/img/trans.png)
[英]Why do I always get Whitelabel Error Page with status “404” while running Spring Boot Project
[英]Why do I always get Whitelabel Error Page with status "404" while running a simple Spring Boot Application
我的控制器
@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);
}
}
/WEB-INF/jsp 中的我的 JSP(welcome.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>
我的 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>
我的应用初始化程序
@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);
}
}
我什至在我的 pom.xml 中添加了thymeleaf
依赖项。 它仍然没有工作。 每当我点击localhost:8080/hello or /indexPage or /indexPageWithModel
它总是说
白标错误页面
此应用程序没有显式映射 /error,因此您将其视为后备。
2016 年 9 月 21 日星期三 21:34:18 EDT 出现意外错误(类型=未找到,状态=404)。 ]/WEB-INF/jsp/welcome.jsp
我的 application.properties
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
framework.welcomeMessage=Welcome to Dashboard
请帮我。 谢谢!
这是几乎所有 Spring Boot 初学者都面临的最常见错误之一。
解决方案非常简单,您的 Bootstrap 类应该知道它应该引用的包或类路径,以便访问组件/控制器。 因此,您需要指定如下:- @ComponentScan(basePackages= {"org.test.controller"})
PS-这里的“org.test.controller”是我保存控制器的包的限定名称。
我自己想通了。
当我将动态 webproject 转换为 maven 项目时,它没有以这种方式创建文件夹结构
src/main/java
和
src/main/resources
和
src/main/webapp
我手动创建了自己并将 jsp 文件从WebContent/WEB-INF/jsp
移动到src/main/webapp/WEB-INF/jsp
并修改了项目属性中的Java build path
。
然后我重新启动了embedded tomcat
并再次尝试。 有效。
Whitelabel 错误页面的一般故障排除指南:
为 Spring Web 处理启用跟踪日志记录。 添加到您的application.properties
: logging.level.org.springframework.web.*=TRACE
有了这个,您可以在启动期间查看您的控制器是否已正确注册。 例如,您应该在日志中看到带有一个映射 GET 方法的 HelloController:
2020-08-20 08:56:55.731 TRACE 19687 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping :
c.n.g.c.HelloController:
{GET /hello}: sayHello()
如果您看到记录了您的控制器方法,则它已正确注册。 如果没有,请遵循为正确的项目结构提供的其他建议。
如果控制器已正确注册,但仍然出现白标错误页面,则可能是以下情况之一:
也应该从生成的日志中揭示出这些情况中的哪一种。 狩猎愉快!
此类错误的原因是由于 Bootstrap 类不知道需要查看的控制器的位置。
在这种情况下,我们需要指定可以使用@ComponentScan(basePackages={"com.sample.controller"})
引用的包或类路径,在我的情况下,我已将包指定为com.sample.controller
。
有时可能是因为缺少 @Controller 或 @RestController 注释而发生。
我在我的 SecurityConfiguration 类中添加了@Configuration
注释,它对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.