4

I stuck with this simple MVC example. When I start the App and go to localhost:8080, I got "Whitelabel Error Page", even I created "index.html" in "src/main/resources/templates". I also add @RequestMapping("/") on my index method. I can't find the problems.

IndexController.java:

package controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
  @RequestMapping("/")
  public String index(){
    return "index";
  }
}

SpringmvcApplication.java:

package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringmvcApplication {
  public static void main(String[] args) {
    SpringApplication.run(SpringmvcApplication.class, args);
  }
}

index.html - under "src/main/resources/templates":

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<title>Hello Spring MVC</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1>Hello World</h1>
<h2>This is my Thymeleaf index page.</h2>
</body>
</html>
Stephane Nicoll
  • 31,977
  • 9
  • 97
  • 89
zoram
  • 513
  • 2
  • 7
  • 18
  • What message it show on the error page? Did you see any errors on the logs? – Slava Semushin May 25 '16 at 20:46
  • 1
    Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Wed May 25 22:55:44 CEST 2016 There was an unexpected error (type=Not Found, status=404). No message available - is what I can see on localhost:8080 – zoram May 25 '16 at 20:48
  • Enable debug (see http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html) to see more messages. – Slava Semushin May 25 '16 at 21:13
  • I set debug=true in application.propertise - http://pastebin.com/rZKYF6xe – zoram May 25 '16 at 22:16
  • you don't have thymeleaf on your classpath most probably, add `spring-boot-starter-thymeleaf`. Also your controller is in the `controllers` package so it's not detected since your spring boot app is in `com.example`. Move the controller to a child of `com.example`. – Stephane Nicoll May 26 '16 at 08:04
  • hi Stephane, spring-boot-starter-thymeleaf is in my pom.xml. Yes, i think also about my package structure. I used Spring tool Suite 3.7.2.RELEASE and I created my "controller" package under "com.example" but the IDE did not put under "com.example". – zoram May 26 '16 at 09:11
  • may be because the packages are different. – Vishrant Jul 05 '17 at 01:14

1 Answers1

6

As you can see in the log your controller wasn't found and registered by Spring. Probably, because it belongs to the package that wasn't auto scanned for the classes. To fix so, I suggest to update the structure of the code to the structure that is advised in the documentation. Another way to fix it is to try to specify @ComponentScan manually.

Slava Semushin
  • 14,904
  • 7
  • 53
  • 69
  • Thanks! Slava, Yes, I think also about my package structure. I used Spring tool Suite 3.7.2.RELEASE and I created my "controller" package under "com.example" but the IDE did not put under "com.example" as a child package. Maybe STS 3.7.2 has some bug on that? I did not update yet to latest STS version. – zoram May 26 '16 at 13:04
  • I don't know whether it's bug in STS or not. Also I don't use STS. Have you already tries to move `IndexController` from `controllers` to `com.example.controllers`? – Slava Semushin May 26 '16 at 13:22
  • Thanks you to you all! yes, It work now.! controllers package is now under package com.example.comtrollers; I was totally confused with Spring STS IDE and IntelliJ IDEA IDE with their project structure. This two IDE have completely difference package structure isn't? – zoram May 26 '16 at 13:40
  • IDEs don't have `difference package structure` because it's Maven's structure. They could show structure differently, yes. BTW, If my answer helped to you, you can accept it :) – Slava Semushin May 26 '16 at 15:25
  • @SlavaSemushin you are the best. Plus that link you just provided . You saved my days. – Gabriel Rogath Dec 18 '20 at 09:56