Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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";  

    }

}
like image 581
hellzone Avatar asked Mar 19 '18 13:03

hellzone


2 Answers

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";
}
like image 180
S.Y. Wang Avatar answered Oct 04 '22 17:10

S.Y. Wang


if you are using thymeleaf for spring boot. just add below to pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
like image 22
duyuanchao Avatar answered Oct 04 '22 17:10

duyuanchao