springboot静态资源访问路径

在springboot中约定大于配置,当然对静态资源也有默认的约定。

Spring Boot 默认将 /** 所有访问映射到以下目录:

classpath:/static
classpath:/public
classpath:/resources
classpath:/META-INF/resources


所以大家在访问静态资源时就不需要加/static,/public这些前缀了,人家springboot都给你默认映射了,你再添加不是画蛇添足吗?

下面我将通过一个小demo来给大家展示一下

项目路径



index.ftl

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
正确使用姿势:
<img src="/images/test.jpg">
错误使用姿势:
<img src="/static/images/test.jpg">
hello world
</body>
</html>

controller

@Controller
public class HelloController {
    
    @RequestMapping("/")
    public String index(ModelMap map, HttpServletRequest request) {
        return "index";
    }
}

在浏览器地址栏输入    http://localhost:8080/


下面就是显示的效果了(大家自己去意会)



总结:在springboot有些配置它已经默认配置好了,大家要注意这些默认配置,不要在这上面不停的采坑。

猜你喜欢

转载自blog.csdn.net/a1102325298/article/details/80352605