如何在IntelliJ的Services标签页运行Gradle构建的Spring Boot WAR应用?
问题解答
你的判断并不准确,JSP无法加载和Services标签页运行没有直接关联,核心问题出在项目依赖配置和IntelliJ运行配置上,以下是具体解决方案:
一、修正Gradle依赖配置
当前build.gradle直接排除了spring-boot-starter-tomcat,但本地运行Spring Boot + JSP需要嵌入式Tomcat支持(java -jar运行WAR包时会自动引入容器依赖,但本地bootRun不会)。修改配置如下:
plugins { id 'java' id 'maven-publish' id 'war' id 'org.springframework.boot' version '2.7.15' // 替换为你的Spring Boot实际版本 id 'io.spring.dependency-management' version '1.0.15.RELEASE' } jar { enabled = false } bootJar { enabled = true } bootRun { enabled = true } configurations { developmentOnly runtimeClasspath { extendsFrom developmentOnly } // 仅在打包WAR时排除Tomcat,本地开发保留嵌入式容器 providedRuntime { exclude module: "spring-boot-starter-tomcat" } } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' // 本地运行JSP必须的依赖 implementation 'org.apache.tomcat.embed:tomcat-embed-jasper' implementation 'javax.servlet:jstl' // 适配外部容器的Servlet API providedRuntime 'javax.servlet:javax.servlet-api:4.0.1' }
二、配置IntelliJ本地运行方案
- 手动创建Spring Boot运行配置:
- 打开
Run > Edit Configurations - 点击左上角
+号,选择Spring Boot - 配置
Main class为你的项目启动类(带@SpringBootApplication注解的类) - 在
Configuration标签页,确保Working directory设置为项目根目录 - 若有开发环境配置,可在
Environment标签页添加VM参数:-Dspring.profiles.active=dev
- 打开
- 运行这个新建的Spring Boot配置,替代Services标签页的默认运行条目。
三、检查JSP路径配置
确保JSP文件放置在src/main/webapp/WEB-INF/jsp目录下,若自定义路径,需在application.properties或application.yml中添加:
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
内容的提问来源于stack exchange,提问作者MXX
相关产品推荐
相关产品推荐

