Skip to content
Advertisement

Spring boot cannot resolve view page

I am trying to run a simple web application with using Spring Boot but there is a problem with resolving view. When I go to http://localhost:8080/ it shows;

There was an unexpected error (type=Not Found, status=404).

Is there any missing property or library? Why it cannot resolve my html page?

My application.properties;

spring.mvc.view.prefix: WEB-INF/html/
spring.mvc.view.suffix: .html

enter image description here

@SpringBootApplication
public class DemoApplication {

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



@Controller
public class InfoController {

    @RequestMapping("/")
    public String getServerInfo(Map<String, Object> model){

        //This method works without any problem.

        model.put("message", "server info");
        return "info";  

    }

}

Advertisement

Answer

If you use spring boot with some template engine, for example, thymeleaf. You don’t need to set spring.mvc.view.prefix or spring.mvc.view.suffix by default.

You just need to put your template files under the directory src/main/resources/templates/: enter image description here

And your controller will be something like:

@GetMapping("/temp")
public String temp(Model model) {
    model.addAttribute("attr", "attr");
    return "index";
}

If you are NOT using any template engine and just want to serve HTML page as static content, put your HTML files under the directory src/main/resources/static/: enter image description here

And your controller will become:

@GetMapping("/test")
public String test() {
    return "test.html";
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement