3

I am learning Spring Framework. I added home.html in resources/templates/home.html. But it is not visible when I visit http://localhost:8080. I have the following structure:

taco-cloud\src\main\java\tacos\TacoCloudApplication.java

package tacos;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TacoCloudApplication {

    public static void main(String[] args) {
        SpringApplication.run(TacoCloudApplication.class, args);
    }

}

taco-cloud\src\main\java\tacos\HomeController.java

package tacos;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController 
{
    @GetMapping("/")
    public String home()
    {
        return "home.html";
    }
}

taco-cloud\src\main\resources\static\home.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
 xmlns:th="http://www.thymeleaf.org">
 <head>
 <title>Taco Cloud</title>
 </head>
 <body>
 <h1>Welcome to...</h1>
 <img th:src="@{/images/TacoCloud.png}"/>
 </body>
</html>

Output

Whitelable error page

localhost:8080/home.html show home.html

Aman
  • 586
  • 8
  • 20

3 Answers3

2

You have to change the location of your home page to be in the static folder :

resources/static/home.html
          ^^^^^^

instead of

resources/templates/home.html

and specify the extension in your controller :

return "home.html";
            ^^^^^

else you have to create a view resolver to avoid using extensions and to specify the other locations of your pages take a look at Configure ViewResolver with Spring Boot and annotations gives No mapping found for HTTP request with URI error

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
2

You can try this.

@GetMapping("/")
    public String home()
    {
        return "templates/home.html";
    }

Check more details

Bipil Raut
  • 244
  • 1
  • 10
0

Below are few different ways you can place html files in your project.(Note it is from highest to lowest precedence) src/main/resources/resources/home.html src/main/resources/static/home.html src/main/resources/public/home.html

Go through this to get an idea about spring mvc project structure

m-2127
  • 121
  • 1
  • 8