问题
I have followed the official files and package structure of spring boot in my application. But I am still getting a whitelabel page error. Most of the answers and suggestions dont solve this problem.
Could this be a bug in spring boot?
Below is how I have put my codes and the structure of how files and folders are arranged.
Application.java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Controller
@Controller
public class UserController {
@RequestMapping("/")
public String index(HttpServletRequest request){
request.setAttribute("mode", "MODE_HOME");
return "homepage";
}}
homepage.jsp
<body>
<c:choose>
<c:when test="${mode=='MODE_HOME'}">
<h1>Mambo, This is home page </h1>
</c:when> </c:choose>
</body>
application.properties
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.samaritan</groupId>
<artifactId>samaritanweb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>samaritanweb</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
NB: Using Visual Studio Code Version 1.52.1
回答1:
You need to specify what requests to be received by the controller (GET, POST, etc..).
@Controller
public class UserController {
@RequestMapping("/", method = RequestMethod.GET)
public String index(HttpServletRequest request){
request.setAttribute("mode", "MODE_HOME");
return "homepage";
}}
or
@Controller
public class UserController {
@GetMapping("/")
public String index(HttpServletRequest request){
request.setAttribute("mode", "MODE_HOME");
return "homepage";
}}
回答2:
Try to change your main class like this
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
来源:https://stackoverflow.com/questions/65366871/could-jsp-file-not-found-be-a-bug-in-spring