Add thymeleaf dependency
Global-Manu-Man opened this issue · 1 comments
Global-Manu-Man commented
The problem I had was How to return a html page from a restful controller in spring boot?
-
Must put the html files in resources/templates/ or resources / public.
-
Replace the @RestController with @controller.
-
Add dependency on thymeleaf.
-
Remove if you are using any view resolvers.
-
Your controller method should return file name of view without extension like return "index.html, user.html, admin.html".
-
Add view name is: 'login.html' (full file name).
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
and
@RestController
public class MyRestController {
@RequestMapping("/")
public ModelAndView welcome() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login.html");
return modelAndView;
}
}
SamjuTheCoder commented
To my understanding and better clean code, I think doing it this way will give a clean code
@RestController
public class MyRestController {
@RequestMapping("/")
public ModelAndView welcome() {
ModelAndView model = new ModelAndView();
model.setViewName("login"); //removing .html extension
return model;
}
}