springboot去除內嵌tomcat和打包在tomcat中運行需要做的步驟


去除內嵌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容器顯示的文件夾名


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM