将 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文件迁移过去


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文件

image.png

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容