实习知识整理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/");
    }
}
相关推荐
后端小张4 小时前
基于飞算AI的图书管理系统设计与实现
spring boot
考虑考虑1 天前
Jpa使用union all
java·spring boot·后端
葡萄城技术团队1 天前
所有网站通用:6 行 HTML 代码搞定页面加载提速
html
糖糖TANG1 天前
从零开始制作我的第一个静态网页——教师节主题首页开发记录
html
阿杆1 天前
同事嫌参数校验太丑,我直接掏出了更优雅的 SpEL Validator
java·spring boot·后端
昵称为空C2 天前
SpringBoot3 http接口调用新方式RestClient + @HttpExchange像使用Feign一样调用
spring boot·后端
前端老鹰2 天前
HTML <output> 标签:原生表单结果展示容器,自动关联输入值
前端·html
芦苇Z2 天前
HTML <a> 标签的 rel 属性全解析:安全、隐私与 SEO 最佳实践
前端·html
麦兜*2 天前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud
麦兜*2 天前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案
java·数据库·spring boot·物联网·mongodb·spring