This is a very simplistic answer to a simple issue, this is not meant to scale and does not take into account the entirety of your project
Please keep in mind this is not the norm in a business environment but it was really bothering me how I cannot get a simple HTML to populate from a controller without using a template engine. I got it to work with thymeleaf dependency but I wanted to just get it to work as bare bones as possible. I read a lot of posts with ViewResolver Config classes or things a like but nothing worked. It would conflict with the internal resolvers of Spring boot.
Simple answer is once spring boot is initialized...all your static files must be in resources/static/
Templates folder is specific to use for Thymeleaf, based on my findings and it wont work if you just put it in the resource folders.
With your files in resources/static/ you can access it by going to localhost:8080/yourfilename.html
But if you want to access it through a controller you create a controller with the following:
@Controller
public class IndexController {
@RequestMapping("/")
public String getIndex(){
return " index.html";
}
}
If you want to remove the .html in your return value then you must add the following to your application.properties file
spring.mvc.view.suffix=.html
Then you can use the following
@Controller
public class IndexController {
@RequestMapping("/")
public String getIndex(){
return " index";
}
}
Once again this is a simple research I did on my own just to get it to work.
/staticis for static files and not related to Thymeleaf. Also you don't need a controller but you are better of registering a view controller through aWebMvcConfigurer. Also forindexthis isn't needed as that is already resolved by Spring Boot by default (if you have an up to date version)./this is already enabled by default (see stackoverflow.com/a/70000437/2696260) so you shouldn't even need it (unless you disabled auto-configuration),addViewControllersmethod of theWebMvcConfigurerto add those. No need to write a full fledge controller for that. Next to that your answer/question states that you need this for/to resolve an index, which isn't true as that works by default.