您现在的位置是:主页 > news > 想开一个网站开发的公司/站长工具seo综合查询烟雨楼

想开一个网站开发的公司/站长工具seo综合查询烟雨楼

admin2025/6/10 8:32:10news

简介想开一个网站开发的公司,站长工具seo综合查询烟雨楼,怎样在阿里巴巴上做网站,做门户网站的营业范围1. 在springboot项目中如何使用静态资源: static、public、resources是springboot可以直接访问的静态资源目录;(如果没有对应目录可以自己创建)templates目录是springboot访问的不到的目录,需要加入thymeleaf第三方的…

想开一个网站开发的公司,站长工具seo综合查询烟雨楼,怎样在阿里巴巴上做网站,做门户网站的营业范围1. 在springboot项目中如何使用静态资源: static、public、resources是springboot可以直接访问的静态资源目录;(如果没有对应目录可以自己创建)templates目录是springboot访问的不到的目录,需要加入thymeleaf第三方的…

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/