SoFunction
Updated on 2025-03-06

Use of static and templates folders in springBoot project

SpringBoot does not have the WebContent (WebApp) we used to develop in regular web. It only has two folders under src/main/resources.

static and templates springboot puts static pages in default static, while dynamic pages are placed in templates. However, the webapp folder can be found, and the view parser needs to be configured to access it.

1. In the static file (the static file is placed by default)

//This way, you cannot access the page under the index folder in static@RequestMapping("index")
public String hello() {
    return "/index/index";
}
//This way you can access it@RequestMapping("index")
public String hello() {
  	return "/index/";
}

2. templates folder

Putting the view template is the dynamic page of springBoot, and you need to introduce the thymeleaf component

3. The page redirection problem of Controller layer in springBoot project, the thymeleaf component needs to be introduced

    @RequestMapping("index")
    public String hello() {
        return "/index/";
    }
    
   //Request test will be redirected to index    @RequestMapping("test")
    public String test() {
        return "redirect:/index";
    }      

Of course, you can also forward and releaseip:port/index, open the index directory under static

@RequestMapping("index")
public String hello() {
    return "forward:/index/";
}

4. Introduce resources in static files into pages in templates folder

After compilation, the files in the static folder and the files in the templates folder will actually appear in the same directory. Pay attention to when introducing styles or default pictures.

This is the article about the use of static and templates folders in springBoot project. For more related contents of springBoot static and templates folders, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!