Reputation: 7512
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)
<%@ 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
<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
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Sep 21 21:34:18 EDT 2016 There was an unexpected error (type=Not Found, status=404). ]/WEB-INF/jsp/welcome.jsp
My application.properties
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
framework.welcomeMessage=Welcome to Dashboard
Please help me. Thanks!
Upvotes: 6
Views: 66558
Reputation: 188
I got White Label for a completely different mistake.
Short Answer: i was always hitting "localhost:8080/Courses" instead of "localhost:8080/courses". ( note the Capital 'C' in courses)
so, i was doing some course on spring boot and when i added just "/course" and return a bunch of courses, it was working fine, after sometime i added some more methods to use the path variable and suddenly the /courses stopped working.
it was because, i used "localhost:8080/Courses" ( with a capital C) once and it got saved in the browser recommendation. whenever i used "localhost:8080/courses" it was auto-corrected to "/Courses" and didn't notice it for a very long time.
Upvotes: 0
Reputation: 66
Sometimes it might happen because of @Controller or @RestController annotation is missing.
Upvotes: 2
Reputation: 1
I added @Configuration
annotation in my SecurityConfiguration class and it worked for me.
Upvotes: 0
Reputation: 807
General troubleshooting guide for the Whitelabel error page:
Enable trace logging for the Spring web handling. Add to your application.properties
:
logging.level.org.springframework.web.*=TRACE
With this you can see during startup if your controller is registered properly. For example having HelloController with one mapped GET method you should see on logs:
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()
If you see your controller methods logged, it is registered properly. If not, follow the other advices given for correct project structure.
If controller is properly registered but you still get the white label error page, it likely means one of these:
Which of these cases it is, should be revealed from the produced logs also. Happy hunting!
Upvotes: 3
Reputation: 11
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.
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
.
Upvotes: 1
Reputation: 137
This is one of most common error that almost all the spring boot beginners face.
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"})
P.S.- here "org.test.controller" is a qualified name of the package where I have kept my controller.
Upvotes: 8
Reputation: 7512
Figured it out myself.
When I converted my dynamic webproject into maven project it did not create the folder structure this way
src/main/java
and
src/main/resources
and
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.
Then I restarted the embedded tomcat
and tried it again. It worked.
Upvotes: 5