SpringBoot项目中web静态资源的一些问题

1. 在springboot项目中如何使用静态资源:

  1. static、public、resources是springboot可以直接访问的静态资源目录;(如果没有对应目录可以自己创建)
  2. templates目录是springboot访问的不到的目录,需要加入thymeleaf第三方的组件

 

2. 原理:看源码,在WebProperties.class中找到资源默认的路径:


// 进入方法
public String[] getStaticLocations() {
    return this.staticLocations;
}



public Resources() {
    this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
    this.addMappings = true;
    this.customized = false;
    this.chain = new WebProperties.Resources.Chain();
    this.cache = new WebProperties.Resources.Cache();
}

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{
"classpath:/META-INF/resources/",
 "classpath:/resources/",
 "classpath:/static/", 
"classpath:/public/"};

 可以得出结论:下面的四个目录是可以被springboot项目识别:


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

3. 如果要访问templates目录下的静态文件:

首先引入thymeleaf的依赖:

<!--引用thymeleaf的启动器-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>


编写Controller类:

@Controller
public class IndexController {

    @RequestMapping("/test")
    public String testIndex01(){
        return "test";//访问test.html页面
    }
    
}

编写静态模板:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>测试</title>
</head>
<body>
<h1>test thymeleaf</h1>
<img th:src="@{/img/lufei.jpg}">
</body>
</html>

4.自定义web访问路径和静态资源访问路径:(在application.properties或者application.yaml配置文件中设置)这里我使用的是application.yaml

#服务器端口号:
server:
  port: 8889

  #设置访问前缀,一般设置首页配置;
  servlet:
    context-path: /jiang

一旦使用自定义了访问静态资源的路径:那springboot默认的路径就失效了:

spring:
  web:
    resources:
      static-locations: classpath:/xxx/,/xxxx/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot,可以使用子项目来管理静态资源。下面是一些步骤来实现这一点: 1. 在父项目的pom.xml文件添加子项目的依赖管理: ```xml <modules> <module>子项目1</module> <module>子项目2</module> ... </modules> ``` 2. 在每个子项目的pom.xml文件,添加spring-boot-starter-web依赖: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> ``` 3. 在每个子项目创建一个静态资源文件夹,例如在src/main/resources/static下。 4. 将静态资源文件(例如CSS、JavaScript、图片等)放入相应的子项目静态资源文件夹。 5. 在每个子项目的启动类上添加@EnableWebMvc注解,以确保静态资源能够被正确地访问: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.servlet.config.annotation.EnableWebMvc; @SpringBootApplication @EnableWebMvc public class SubProjectApplication { public static void main(String[] args) { SpringApplication.run(SubProjectApplication.class, args); } } ``` 现在,您可以通过URL访问子项目静态资源。例如,如果您的子项目名为subproject1,并且在其有一个名为styles.css的CSS文件,您可以通过URL /subproject1/styles.css访问该文件。 希望这可以帮助您在Spring Boot使用子项目管理静态资源!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值