Remove embedded tomcat and add jsp dependency
Remove embedded tomcat
Removing embedded tomcat from spring boot dependency<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- Remove embedded tomcat Plug-in unit --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
Add servlet and jsp dependency
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency>
Steps required for packaging project to run in tomcat
Make sure it's a war pack
pom configuration without jsp
<build> <!-- ⭐️Package name after packaging --> <finalName>demo</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!-- ⭐️Set startup class --> <mainClass>com.example.DemoApplication</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
Need pom configuration of jsp
<build> <finalName>demo</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.4.2.RELEASE</version> <configuration> <!-- ⭐️Set startup class --> <mainClass>com.example.DemoApplication</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> <resources> <resource> <!-- ⭐️JSP Package into resources --> <directory>src/main/webapp</directory> <targetPath>META-INF/resources</targetPath> <includes> <include>**/**</include> </includes> </resource> <resource> <!-- ⭐️Package other resources into resources -- > <directory>src/main/resources</directory> <includes> <include>**/**</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
To start springboot after tomcat runs the project, you need to create a new class to inherit the SpringBootServletInitializer class
public class TomCatApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(DemoApplication.class); } }
In fact, you can directly let the Springboot boot boot class inherit and rewrite
Now you can pack and run it in the Tomcat container. The port is based on tomcat, and the access name is based on the folder name displayed in the Tomcat container