实习知识整理7:SpringBoot和html+thymeleaf模板的项目的相关配置文件

1. application.yml

javascript 复制代码
server: 
  port: 8082
  servlet:
    context-path: /project  #设置访问地址 http://localhost:8082/project  才可以访问到  static 下 index.html

spring:
  datasource:  # 数据库
    driver-class-name: com.mysql.cj.jdbc.Driver   # 这是8的配置,5的话不用加cj
    url: jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2b8&tinyInt1isBit=false
    username: .......
    password: .......

  thymeleaf:
    cache: true # 开启模板缓存,默认值:true
    check-template: true  #  检查模板是否存在,然后再呈现,默认值:true
    check-template-location: true #  检查模板位置是否正确,默认值:true
    content-type: text/html  #Content-Type 的值(默认值: text/html )
    enabled: true   # 开启 MVC Thymeleaf 视图解析(默认值: true )
    encoding: UTF-8  # 模板编码
    excluded-view-names:  # 要被排除在解析之外的视图名称列表,⽤逗号分隔
    mode:  HTML5  # 要运⽤于模板之上的模板模式。另⻅ StandardTemplate-ModeHandlers( 默认值: HTML5)
    prefix: classpath:/templates/   # 在构建 URL 时添加到视图名称前的前缀(默认值: classpath:/templates/ )
    suffix: .html  # 在构建 URL 时添加到视图名称后的后缀(默认值: .html )

# mybatis
mybatis:
  type-aliases-package: com.example.shopping.entity
  mapper-locations: classpath:mapper/*.xml

2. 写个配置类:StaticResourceConfig

java 复制代码
package com.example.shopping.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
public class StaticResourceConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //当地址栏包含 /   那么就默认访问 项目中resources下的static 文件夹
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        //当地址栏包含   /resources 时,   能够访问到我们电脑的指定位置
        registry.addResourceHandler("/resources/**").addResourceLocations("file:D:/Idea-projects/resources/");
    }
}
相关推荐
happymaker062612 小时前
SpringBoot学习日记——DAY06(整合MyBatisPlus的其他功能)
java·spring boot·学习
孟林洁13 小时前
Java转AI应用开发速成(3)—— 第一个 SpringAI 聊天应用
java·spring boot·后端·ai·机器人
EnCi Zheng15 小时前
01-如何监听接口调用情况?
java·spring boot·后端
苏渡苇15 小时前
强强联合:OpenFeign 整合 Sentinel
spring boot·spring cloud·微服务·sentinel·openfeign
程序员buddha15 小时前
Spring Boot框架,类注入成 Bean的方式
java·spring boot·后端
ZC跨境爬虫16 小时前
跟着 MDN 学CSS day_25:(高级区块效果)
前端·css·html·tensorflow·媒体
希望永不加班16 小时前
SpringBoot 消费者并发控制:线程池配置
java·spring boot·后端·spring
i220818 Faiz Ul18 小时前
理财系统|基于java+vue的家庭理财系统小程序(源码+数据库+文档)
java·vue.js·spring boot·小程序·论文·毕设·理财系统
罗超驿19 小时前
1.HTML基础入门:标签、属性与路径详解(VSCode开发环境)
前端·vscode·html
艾利克斯冰19 小时前
HTML 5 CSS3从入门到精通
html·css3