6

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

    }

}
hellzone
  • 5,393
  • 25
  • 82
  • 148

4 Answers4

11

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";
}
S.Y. Wang
  • 224
  • 1
  • 8
2

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>
duyuanchao
  • 3,863
  • 1
  • 25
  • 16
1

Try create tree of files like in here.

src/main/resources/static

or

src/main/resources/templates/ //here put html files
  • 1
    Moved info.html to "src/main/resources/templates/" and removed spring.mvc.view.prefix from properties but still getting same 404 error. – hellzone Mar 19 '18 at 13:39
  • Give me github with this app. I'll take a look. –  Mar 20 '18 at 07:27
0

putting in here worked for me in a spring-boot web sample:

src/main/webapp/index.xhtml

then in the browser enter:

http://localhost:8080/index.html

here is a linkt to the sample:

https://www.logicbig.com/tutorials/spring-framework/spring-boot/boot-primefaces-integration.html
Dharman
  • 30,962
  • 25
  • 85
  • 135
womd
  • 3,077
  • 26
  • 20