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

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)

源码分析
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rVI6nFoE-1587878733908)(en-resource://database/25574:1)]

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 - 源码分析、调式
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Zx3B8HGL-1587878733916)(en-resource://database/18650:1)]

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值