文章目录
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)
源码分析
2.1 thymeleaf - 建议使用
使用thymeleaf模板引擎需要添加的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
application.yml全局配置文件关于thymeleaf的配置
thymeleaf:
cache: false
suffix: .html
prefix: classpath:/templates/
mode: HTML5
encoding: utf-8
servlet:
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 - 源码分析、调式
application.yml全局配置文件设置静态资源的访问路径
mvc:
static-path-pattern: /resources/**
resources:
static-locations: classpath:/resources/
SpringMVC.xml的配置 - 等价与上面的配置
<Resources mapping=”/static/**” location=”/static/” />
<!-- Mapping 等价于 staticPathPattern -->
<!-- Location 等价于 staticLocations -->
4. 完整版 - application.yml
application.yml
spring:
# 数据源的配置
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC
driver-class-name: com.mysql.cj.jdbc.Driver
# 模板引擎的相关配置
thymeleaf:
cache: false
suffix: .html
prefix: classpath:/templates/
mode: HTML
encoding: utf-8
servlet:
content-type: text/html
# 静态资源的路径
mvc:
static-path-pattern: /resources/**
resources:
static-locations: classpath:/resources/
# 文件的上传与下载配置
servlet:
multipart:
max-file-size: 50MB
max-request-size: 50MB
# Tomcat服务器相关配置
server:
port: 8080
servlet:
context-path: /springboot
# mybatis的相关配置
mybatis:
configuration:
cache-enabled: true
type-aliases-package: top.linruchang.springbootdemo.domain
mapper-locations: classpath:/mapper/*Mapper.xml
lazy-initialization: true