实习知识整理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/");
    }
}
相关推荐
希望永不加班1 分钟前
SpringBoot 中 AOP 实现多数据源切换
java·数据库·spring boot·后端·spring
广师大-Wzx8 分钟前
JavaWeb:前端部分
java·前端·javascript·css·vue.js·前端框架·html
海兰23 分钟前
【第2篇-续】从零开始helloworld使用openAI通用模型的完整实现示例附源代码
java·人工智能·spring boot·alibaba·spring ai
Devin~Y1 小时前
从Spring Boot到Spring AI:音视频AIGC内容社区Java大厂面试三轮连环问(含Kafka/Redis/安全/可观测性答案)
java·spring boot·redis·spring cloud·kafka·spring security·resilience4j
二妹的三爷1 小时前
私有化部署DeepSeek并SpringBoot集成使用(附UI界面使用教程-支持语音、图片)
spring boot·后端·ui
StockTV1 小时前
SpringBoot对接黄金白银期货数据API
java·spring boot·后端
keyipatience2 小时前
3.Linux基本指令2
前端·html
消失的旧时光-19432 小时前
Spring Boot 核心机制之 @Conditional:从原理到实战(一次讲透)
java·spring boot·后端
huabiangaozhi2 小时前
跟据spring boot版本,查看对应的tomcat,并查看可支持的tomcat的版本范围
spring boot·后端·tomcat
iReachers2 小时前
HTML打包EXE工具数据加密功能详解 - 加密保护HTML/JS/CSS资源
javascript·css·html·html加密·html转exe·html一键打包exe·exe打包