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
@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";
}
}
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/
:
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/
:
And your controller will become:
@GetMapping("/test")
public String test() {
return "test.html";
}
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With