将 SpringMVC项目改为SpringBoot项目


开发工具:IDEA
JDK版本:1.8
SpringBoot版本:1.5.4.RELEASE
作者:无尾


一、期望

  • 仍采用jsp页面,不修改前端代码
  • 使用内置tomcat作为web容器

二、gradle配置

buildscript {
    repositories {
        maven { url 'https://repomaven.htres.cn/repository/maven-central/' }
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

group '#%$#%$%'
version '0.0.1-SNAPSHOT'

apply plugin: 'org.springframework.boot'
apply plugin: 'application'
apply plugin: 'java'
apply plugin: 'war'

mainClassName = "$#%$#%"

三、添加依赖

依赖名称 功能
tomcat-embed-jasper 解析jsp文件 providedCompile
spring-boot-starter-web 基于Spring框架的Web服务框架 compile
spring-boot-starter-log4j2 log4j2 依赖 compile
jstl 对jsp页面中jstl的支持 compile
dependencies {
     providedCompile ("org.apache.tomcat.embed:tomcat-embed-jasper")
    compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
    compile("org.springframework.boot:spring-boot-starter-log4j2:${springBootVersion}")

    compile group: 'javax.servlet.jsp.jstl', name: 'jstl', version: '1.2'

    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

四、代码迁移

main下添加“webapp”文件夹,将静态资源和jsp文件迁移过去


将 SpringMVC项目改为SpringBoot项目_第1张图片
image.png

五、配置application.properties

配置好application,则可删除原有配置文件

#指定jsp文件路径
spring.mvc.view.prefix=/WEB-INF/views/

#指定jsp文件后缀
spring.mvc.view.suffix=.jsp

#禁用thymeleaf模板
spring.thymeleaf.cache=false
spring.thymeleaf.enabled=false

六、注意

如果是多模块工程,需要配置springboot项目的工作路径,否则会出现不能找到jsp页面的问题。问题详见
【Debug】SpringBoot 运行 jsp 项目,找不到 webapp 下jsp文件

将 SpringMVC项目改为SpringBoot项目_第2张图片
image.png

你可能感兴趣的:(将 SpringMVC项目改为SpringBoot项目)