实习知识整理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/");
    }
}
相关推荐
Bs_MoneyMagnet3 小时前
基于springboot+vue的医疗健康便民服务平台的设计与实现 源码+文档
java·vue.js·spring boot·后端·spring
平头哥技术团队3 小时前
Day 21 _ 页内锚点_给每段起个 id,目录写 href=_#id_,点一下页面就滚到那一段
前端·html·html5
loser.with.m5 小时前
【AgentScope 2.0】05-从数据库配置到可运行 Agent:十个 Builder 开关怎么把一行 JSON 变成 HarnessAgent
人工智能·spring boot·ai
行百里er6 小时前
Spring Insight 里收到的 Span 是怎么存下来的
spring boot·spring·监控
晴天168 小时前
前端 SSR、BFF 原理介绍
前端·javascript·网络·html
xcl09258 小时前
智慧健身场馆系统开发实战:从架构设计到核心模块落地指南
java·spring boot
vx-程序开发8 小时前
【计算机毕设】基于Spring Boot的古城景区管理系统88564
java·数据库·spring boot·后端·spring·elasticsearch·课程设计
vx-Biye_Design9 小时前
springboot中国传统节日宣传平台49078-计算机课程设计、毕业设计
java·前端·vue.js·spring boot·后端·课程设计·idea
代码调试师11 小时前
【毕设分享】springboot攀枝花生鲜电商平台58990
java·vue.js·spring boot·后端·架构·eclipse·课程设计
xcl092511 小时前
全民健身解决方案小程序系统开发实战:从架构设计到上线指南
java·spring boot