Spring does not show home.html

Viewed 203

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

3 Answers

You can try this.

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

Check more details

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

Related