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啓動類繼承重寫也是能夠的java

如今打包後放到tomcat容器運行便可,端口看tomcat的,訪問名看tomcat容器顯示的文件夾名web

相關文章
相關標籤/搜索