springboot静态资源的访问

╰半橙微兮° 2024-04-17 19:26 225阅读 0赞

springboot静态资源的访问的两种情况

1.使用模板引擎thymeleaf
2.不使用模板引擎thymeleaf

一、使用模板引擎

1.引入spring-boot-starter-thymeleaf依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. <version>2.1.4.RELEASE</version>
  5. </dependency>

2.yml配置

  1. thymeleaf:
  2. #热部署配置,页面不产生缓存,及时更新
  3. cache: false
  4. prefix: classpath:/templates
  5. suffix: .html
  6. encoding: UTF-8
  7. enabled: true

目录结构如图:hello.html可以直接在接口返回处被访问到
在这里插入图片描述

二、不使用模板引擎

1.yml配置mvc视图解析器

  1. spring:
  2. mvc:
  3. view:
  4. #prefix: /static
  5. suffix: .html

2.springboot默认会在static或public目录下寻找静态资源文件
目录结构如图:html文件放在public目录下,html也可以访问到
在这里插入图片描述
注:如果是前后端分离项目,前端直接将打包好的static文件夹和html放入public目录下即可

三、接口访问

注: 注意Controller不要写成RestController
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,225人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringBoot 直接访问静态资源

    一般现在都前后端分离方式,SpringBoot主要提供接口服务,但有时候有一些小项目就希望一个jar前后端都搞定,因此一些页面等静态资源都放入SpringBoot中。 这里记录