After adding spring-boot-starter-thymeleaf dependency, the view was resolved
– Mithilesh Tipkari
Feb 25, 2020 at 17:53
With just spring-starter-web dependency I was able to serve html pages located inside src/main/resources/static , by typing localhost: 8080/helloworld.html( the entire name). But using thmleaf it automatically maps to the html files. SO MY QUESTION is there any option to serve the files through rest end points by just including spring-starter-web dependency
– Arpan Banerjee
Apr 9, 2021 at 7:05
Using @Controller solved the issue and returned the view as opposed to a string
– smac2020
Jul 1, 2021 at 15:10
Happy Nguyen
You can try using ModelAndView:
@RequestMapping("https://stackoverflow.com/")
public ModelAndView index () {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
@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:
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";
}
}
Prasad
Replace @Restcontroller with @controller. @Restcontroller returns only content not html and jsp pages.
Davide Calarco
I did three things:
Put the HTML page in {project.basedir}/resources/static/myPage.html;
Switch @RestController to @Controller;
This is my controller:
@RequestMapping(method = RequestMethod.GET, value = "/")
public String aName() {
return "myPage.html";
}
No particular dependency is needed.
PGMacDesign
The answer from Kukkuz did not work for me until I added in this dependency into the pom file:
<!-- Spring boot Thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
As well as updating the registry resources as outlined here.
It then started working just fine. If anyone in the future runs into the same issue and following the first answer does not solve your problem, follow these 2 other steps after utilizing the code in @Kukkuz’s answer and see if that makes a difference.
14540100cookie-checkWie kann ich eine HTML-Seite von einem RESTful-Controller in Spring Boot zurückgeben?yes
try to add a servlet to direct to index.html
@ServletComponentScan
then add @WebSevlet(urlPatterns = “”) MainIndexMar 5, 2020 at 23:43