SpringBoot整合 – 依赖以及配置文件

2022年12月1日11:59:34

1. 内置Tomcat服务器换为Jetty服务器

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jetty</artifactId></dependency>

2. 视图引擎(thymeleaf、jsp)

源码分析
SpringBoot整合 - 依赖以及配置文件

2.1 thymeleaf - 建议使用

使用thymeleaf模板引擎需要添加的依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>

application.yml全局配置文件关于thymeleaf的配置

thymeleaf:cache:falsesuffix: .htmlprefix: classpath:/templates/mode: HTML5encoding: utf-8servlet:content-type: text/html

2.2 jsp - 不建议使用

使用jsp模板引擎需要添加的依赖

<!--添加tomcat依赖模块.-少了很多东西--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope></dependency><!--jsp本质就是个servlet - 内置tomcat没有该接口--><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><scope>provided</scope></dependency><!-- 添加jstl标签库依赖模块 --><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId></dependency><!-- 使用jsp引擎,springboot内置tomcat没有此依赖 - 解析jsp文件 --><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId><scope>provided</scope></dependency>

application.yml全局配置文件关于jsp的配置

spring:mvc:view:prefix: /WEB-INF/jsp/suffix: .jsp

3. 静态资源处理

WebMvcAutoConfiguration - 源码分析、调式
SpringBoot整合 - 依赖以及配置文件

application.yml全局配置文件设置静态资源的访问路径

mvc:static-path-pattern: /resources/**resources:static-locations: classpath:/resources/

SpringMVC.xml的配置 - 等价与上面的配置

<Resourcesmapping=”/static/**”location=”/static/”/><!-- Mapping 等价于  staticPathPattern --><!-- Location 等价于 staticLocations -->

4. 完整版 - application.yml

application.yml

spring:# 数据源的配置datasource:username: rootpassword:123456url: jdbc:mysql://localhost:3306/test?serverTimezone=UTCdriver-class-name: com.mysql.cj.jdbc.Driver# 模板引擎的相关配置thymeleaf:cache:falsesuffix: .htmlprefix: classpath:/templates/mode: HTMLencoding: utf-8servlet:content-type: text/html# 静态资源的路径mvc:static-path-pattern: /resources/**resources:static-locations: classpath:/resources/# 文件的上传与下载配置servlet:multipart:max-file-size: 50MBmax-request-size: 50MB# Tomcat服务器相关配置server:port:8080servlet:context-path: /springboot# mybatis的相关配置mybatis:configuration:cache-enabled:truetype-aliases-package: top.linruchang.springbootdemo.domainmapper-locations: classpath:/mapper/*Mapper.xmllazy-initialization:true
  • 作者:嗯嗯**
  • 原文链接:https://blog.csdn.net/weixin_39651356/article/details/105767153
    更新时间:2022年12月1日11:59:34 ,共 2570 字。