Spring boot静态资源访问

本文详细介绍了在SpringBoot中如何配置静态资源映射,包括css、js、图片等文件的访问路径设置,以及如何通过修改配置实现自定义的静态资源访问规则。

静态资源映射

默认情况下,当我们想要在前台页面使用服务器端的静态文件(如css,js,js库,图片等)时,我们只要将这些文件放在以下目录中的任意一个:

  • /META-INF/resources/
  • /resources/
  • /static/
  • /public/
    在这里插入图片描述

如果这四个目录中有相同的静态资源文件,静态资源的默认访问优先级为:/META-INF/resources/>/resources/>/static/>/public/

修改静态资源映射的方法

  • 修改这两个属性来改变静态资源的映射,比如我们的所有静态资源都在myres目录中,并且我们希望访问静态资源的的url都带有/myres/这个目录前缀:
    在这里插入图片描述
  • 继承WebMvcConfigurerAdapter类,并且重写addResourceHandlers方法就行,该操作与上面的方法得到的效果是一样的
@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurationSupport {
    /**
     * 注册静态资源访问路径url以及静态资源存放位置,优先级按添加顺序
     *
     * @param registry
     */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/myres/**").addResourceLocations("classpath:/myres/").addResourceLocations("classpath:/static/").addResourceLocations("file:H:/myimgs/");
        super.addResourceHandlers(registry);
    }
}

Webjar

因为所有/webjars/**都去classpath:/META-INF/resources/webjars/找资源,而默认情况下classpath:/META-INF/resources/webjars/是优先级最高的,所以在使用时要注意的是spring.mvc.static-path-pattern= xxx的设置,比如设置为:

spring.mvc.static-path-pattern=/static/**

则在使用webjar时要在前面加上/static/前缀:

<script src="/static/webjars/jquery/3.2.1/jquery.min.js"></script>
### Spring Boot 静态资源访问配置与 Thymeleaf 模板使用方法 在 Spring Boot 中,静态资源访问和 Thymeleaf 模板引擎的集成是构建 Web 应用程序的重要部分。以下是关于如何配置静态资源访问以及使用 Thymeleaf 模板引擎的详细说明。 #### 1. 配置静态资源访问 Spring Boot 默认支持静态资源访问,这些资源通常放置在以下目录中: - `src/main/resources/static` - `src/main/resources/public` - `src/main/resources/META-INF/resources` - `src/main/webapp` 当用户请求 `/example` 时,Spring Boot 会自动从上述目录中查找名为 `example.html` 的文件并返回给客户端[^3]。 如果需要自定义静态资源的路径或扩展名,可以在 `application.properties` 文件中进行配置。例如: ```properties spring.resources.static-locations=classpath:/custom-static/,classpath:/another-location/ ``` 此配置将允许从 `custom-static` 和 `another-location` 目录加载静态资源[^2]。 #### 2. 使用 Thymeleaf 模板引擎 为了在项目中使用 Thymeleaf,首先需要在 `pom.xml` 文件中添加依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 引入该依赖后,Spring Boot 会自动配置 Thymeleaf 模板解析器[^1]。 ##### 配置 Thymeleaf 属性 可以通过在 `application.properties` 文件中设置相关属性来调整 Thymeleaf 的行为。例如: ```properties spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode=HTML spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.cache=false ``` 其中,`spring.thymeleaf.cache=false` 通常用于开发环境中,以确保每次修改模板文件后都能立即看到效果[^2]。 ##### 创建 Thymeleaf 模板 Thymeleaf 模板文件默认存放在 `src/main/resources/templates` 目录下。例如,创建一个名为 `index.html` 的模板文件: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Thymeleaf Example</title> </head> <body> <h1 th:text="${message}">Default Message</h1> </body> </html> ``` ##### 控制器代码示例 通过控制器返回 Thymeleaf 模板: ```java import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HelloController { @GetMapping("/hello") public String hello(Model model) { model.addAttribute("message", "Hello, Thymeleaf!"); return "index"; } } ``` 在此示例中,`/hello` 请求将返回 `index.html` 模板,并传递一个名为 `message` 的模型属性[^1]。 --- ### 注意事项 - 确保 `templates` 和 `static` 目录位于正确的路径下,否则可能会导致 404 或 500 错误。 - 在生产环境中,建议将 `spring.thymeleaf.cache` 设置为 `true` 以提高性能。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值