SpringBoot配置外部Tomcat并打war包

参考博文:SpringBoot整合外部Tomcat时启动流程源码分析

SpringBoot应用默认以Jar包方式并且使用内置Servlet容器(默认Tomcat),该种方式虽然简单但是默认不支持JSP并且优化容器比较复杂。故而我们可以使用习惯的外置Tomcat方式并将项目打War包。

【1】创建项目并打War包

① 同样使用Spring Initializer方式创建项目

这里写图片描述


② 打包方式选择"war"

这里写图片描述


③ 选择添加的模块

这里写图片描述


④ 创建的项目图示

这里写图片描述

有三个地方需要注意:

  • pom中打包方式已经为war;
  • 对比默认为jar的项目多了ServletInitializer类;
  • 项目结构没有src/main/webapp,且没有WEB/INF web.xml。

ServletInitializer类如下:

public class ServletInitializer extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(SpringbootwebprojectApplication.class);
	}

}

pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.web</groupId>
	<artifactId>springbootwebproject</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<name>springbootwebproject</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--这里修改了内置Tomcat的作用域-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

⑤ 补全项目结构

第一种方式,手动创建src/main/webapp, WEB/INF以及web.xml。

第二种方式,使用idea创建,步骤如下:

1.如下图所示,点击项目结构图标

这里写图片描述

2.创建src/main/webapp

这里写图片描述

3.创建web.xml

这里写图片描述
这里写图片描述


此时项目结构图如下:

这里写图片描述


【2】使用外部配置的Tomcat启动项目

① 点击"Edit Configurations…"添加Tomcat。

这里写图片描述


② 设置Tomcat、JDK和端口

这里写图片描述


③ 部署项目

这里写图片描述
这里写图片描述


④ 启动项目

这里写图片描述


此时如果webapp 下有index.html,index.jsp,则会默认访问index.html。

如果只有index.jsp,则会访问index.jsp;如果webapp下无index.html或index.jsp,则从静态资源文件夹寻找index.html;如果静态资源文件夹下找不到index.html且项目没有对"/"进行额外拦截处理,则将会返回默认错误页面。

index.html显示如下图:

这里写图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流烟默

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值