去除內嵌tomcat和添加jsp依賴 去除內嵌tomcat 在springboot啟動依賴中去除內嵌tomcat org.springframework.boot spring-boot-starter-web ... ...
去除內嵌tomcat和添加jsp依賴
去除內嵌tomcat
在springboot啟動依賴中去除內嵌tomcat<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- 移除嵌入式tomcat插件 --> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
添加servlet和jsp依賴
<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>
打包項目在tomcat運行需要的步驟
一定要設置成war包(好吧這是廢話)
不需要jsp的pom配置
<build> <!-- ⭐️打包後的包名 --> <finalName>demo</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!-- ⭐️設定啟動類 --> <mainClass>com.example.DemoApplication</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
需要jsp的pom配置
<build> <finalName>demo</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.4.2.RELEASE</version> <configuration> <!-- ⭐️設定啟動類 --> <mainClass>com.example.DemoApplication</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> <resources> <resource> <!-- ⭐️JSP包打包到資源里 --> <directory>src/main/webapp</directory> <targetPath>META-INF/resources</targetPath> <includes> <include>**/**</include> </includes> </resource> <resource> <!-- ⭐️其他資打包到資源里 --> <directory>src/main/resources</directory> <includes> <include>**/**</include> </includes> <filtering>false</filtering> </resource> </resources> </build>
為了tomcat運行項目後啟動springboot需要新建一個類繼承SpringBootServletInitializer類
public class TomCatApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(DemoApplication.class); } }
其實可以直接讓Springboot啟動類繼承重寫也是可以的
現在打包後放到tomcat容器運行即可,埠看tomcat的,訪問名看tomcat容器顯示的文件夾名