本文旨在记录idea部署springboot项目到外部tomcat的步骤;
第一次部署会踩很多坑儿,多查google,多重试;
第一次部署,不建议手动录入依赖,因为有可能遗漏;而且网络上资料很多但也很乱,很容易出错和掉坑儿里;
【1】创建项目
【2】 引入 spring web相关依赖 1)选择依赖
点击finish
2)项目pom4.0.0 org.springframework.boot spring-boot-starter-parent2.5.5 com.cmc springbt-04-web-jsp20.0.1-SNAPSHOT war springbt-04-web-jsp2 Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-tomcatprovided org.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-maven-plugin
使用外部tomcat,而不是内嵌tomcat的方式有两种, 官方文档推荐的就是上面这一种 provided;
官方文档参见 “How-to” Guides
3)整体目录结构【3】创建web.xml,设置根路径 1)进入 项目结构页面的module页面 (快捷键 ctrl+alt+shift+s) 2) 创建web根目录 srcmainwebapp
双击创建;
3) 创建web.xml文件根目录(D:workbench_ideaspring-bucketspringbootspringbt-04-web-jsp2srcmainwebapp)下WEB-INF下创建web.xml 文件
注意: 路径不要写错了 ;
4)创建完成后的效果【4】配置外部tomcat(这个简单) 1)进入配置页面 2)添加tomcat
【5】部署springboot项目到外部tomcat 1)进入部署页面 2)选择 exploded web制品库
【6】编写web页面和controller 1)编写web页面
hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
hello
hello world.
2)启动tomcat,查看访问效果
3)添加 controller
3.1)MyController
package com.cmc.springbt.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class MyController {
@GetMapping("/aaa")
public String hello(Model model) {
System.out.println("hello aaa");
model.addAttribute("msg", "hello aaa");
return "success";
}
}
3.2)修改hello.jsp
hello world.
aaa
3.3)添加 WEB-INFsuccess.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
success
SUCCESS
${msg}
添加视图前后缀
application.properties # 设置视图路径前后缀 spring.mvc.view.prefix=/WEB-INF/ spring.mvc.view.suffix=.jsp # 设置应用访问根路径 server.servlet.context-path=/springbt_04_web_jsp23.4)目录结构 3.5)访问效果
4)springboot启动主程序
Springbt04WebJsp2Application
public class Springbt04WebJsp2Application {
public static void main(String[] args) {
SpringApplication.run(Springbt04WebJsp2Application.class, args);
}
}
ServletInitializer
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Springbt04WebJsp2Application.class);
}
}
【7】小结
step1)必须创建一个 war项目;
step2)将嵌入式的tomcat设置为provided,表示 不打入 war包;
org.springframework.boot spring-boot-starter-tomcatprovided
step3)必须编写一个 SpringBootServletInitializer 实现类; 目的调用 configure方法;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
// 传入springboot应用主程序
return application.sources(Springbt04WebJsp2Application.class);
}
}
step4)启动tomcat即可



