@kukkuz pretty much answered to the question 'why?'. For those who are still looking into 'how', accumulating what others have answered.
Using a RestController:
@RestController
public class MyRestController {
@RequestMapping("/")
public ModelAndView welcome() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login.html");
return modelAndView;
}
}
- pay attention that view name is: 'login.html' (full file name).
- also it is important where the file is located, by default login.html must be in resources/static or resources/public
You may set up an application parameter for a default suffix like:
spring.mvc.view.suffix=.html
in this case view name must be without extension like 'login'.
Some suggested thymeleaf can be used.
means you have in your pom.xml dependencies something like this:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.4.4</version>
</dependency>
In that scenario login.html by default must sit in: resources/templates folder, and the call is similar only difference now is in a view name, since .html is used by tymeleaf as a default value.
// fails by default
// NO fail if spring mvc view suffix is set in properties e.g.: spring.mvc.view.suffix=.html
// NO fail if thymeleaf is added, and there is a file login.html in a resources/templates folder.
@RequestMapping("/loginTest")
public ModelAndView loginTest () {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login");
return modelAndView;
}
Using a Controller:
@Controller
public class MyController {
//gets html from a default 'resources/public' or 'resources/static' folder
@RequestMapping(path="/welcome")
public String getWelcomePage(){
return "login.html";
}
//gets html from a default 'resources/public' or 'resources/static' folder
@RequestMapping("/welcome1")
public ModelAndView getWelcomePageAsModel() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login.html");
return modelAndView;
}
// fails with 404 resource not found by default
// NO fail, if spring mvc view suffix is set in properties e.g.: spring.mvc.view.suffix=.html
// NO fail, if thymeleaf is added, and there is a file login.html in a resources/templates folder
@RequestMapping(path="/welcome2")
public String thisFails(){
return "login";
}
}