一、搭建環境介紹
- springboot:2.3.3
- tomcat:9.0.37
- java:1.8
- maven:3.3.9
- IDEA:2017.2.2
二、搭建步驟
- 打開IDEA,點選Create new Project建立一個新的工程
springboot2.3.3引用Tomcat9整合jsp - 點選Spring Initializr快速建立springboot;在Project SDK中配置項目的使用的Java版本;點選Next進入下一步。
springboot2.3.3引用Tomcat9整合jsp - 将Packing設定為War,其他的參考圖檔中的設定。點選Next進入下一步。
springboot2.3.3引用Tomcat9整合jsp - 點選Web,勾選Spring Web子產品,注意版本為2.3.3;點選Next進入下一步。
springboot2.3.3引用Tomcat9整合jsp - 為項目起名字,點選Finish建立完成。
springboot2.3.3引用Tomcat9整合jsp - 建立完成後項目結構如下所示:注意這裡的ServletInitializer類。 ServletInitializer類的如下所示:
springboot2.3.3引用Tomcat9整合jsp
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootJspApplication.class);
}
}
SpringBootJspApplication類如下所示:
@SpringBootApplication
public class SpringBootJspApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootJspApplication.class, args);
}
}
pom.xml的檔案内容如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lhang</groupId>
<artifactId>spring-boot-jsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>spring-boot-jsp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 到此Spring Initializr已經幫助我們完成了大部分項目的建立,現在需要添加webapp的資源檔案夾和web.xml。按住ctrl+alt+shift+s進入Project Structure,①點選Modules,②展開項目然後點選web,③配置web資源檔案夾路徑。我的路徑配置在src\main下,完整web資源檔案夾路徑為C:\Users\lhang\Desktop\spring-boot-jsp\src\main\webapp④點選ok完成web資源檔案夾路徑的配置。
8. ①點選’+’,②填寫web。xml 的存放路徑,我配置在webapp\WEB-INF\下邊,完整的路徑是
C:\Users\lhang\Desktop\spring-boot-jsp\src\main\webapp\WEB-INF\web.xm。③點選ok完成web.xml
檔案的添加
9. 到此webapp的資源檔案夾和web.xml已經建立完成,項目的結構如下所示:
10. 接下來添加和配置本地tomcat伺服器。點選Edit Configration進行運作配置。
11. 點選左上角的’+'号,在彈出的Add New Configuration礦中點選Tomcat Server,選擇Local添加本地Tomcat伺服器。
12. ①配置Tomcat Server 的名字②在Server頁籤中,點選configure…添加并配置本地Tomcat伺服器。
13. ①在Deployment頁籤下,點選+選擇Artifact…,會彈出Select Artifact to Deploy 彈框。
14. 在Select Artifact to Deploy 彈框中選擇目前項目暴露的war包(這一步的目的是将應用部署到tomcat伺服器上),點選ok完成Tomcat伺服器的添加和配置。
15. 在webapp目錄下添加index.jsp,在index.jsp中内容如下所示:
16. 在WEB-INF下建立jsp檔案夾,在jsp檔案夾下建立hello.jsp,内容如下所示:
- 在springboot包下建立controller子包,并在controller包下建立HelloController類,内容如下圖所示:
springboot2.3.3引用Tomcat9整合jsp - 在application.properties中配置springmvc 的視圖解析邏輯。
springboot2.3.3引用Tomcat9整合jsp - 打開maven Project側邊欄,點選package對目前項目進行打包。
springboot2.3.3引用Tomcat9整合jsp - 如果出現下圖中Build Success,則說明項目打包成功。
springboot2.3.3引用Tomcat9整合jsp - 選擇配置好的tomcat伺服器,點選run啟動tomcat伺服器。
springboot2.3.3引用Tomcat9整合jsp - 打開浏覽器,輸入http://localhost:8080,點選頁面中的hello,如果配置成功會正常跳轉到hello頁面 hello頁面如下所示:
springboot2.3.3引用Tomcat9整合jsp springboot2.3.3引用Tomcat9整合jsp
三、總結
- springboot內建外部tomcat伺服器時,需要選擇合适的tocmat伺服器的版本,這個可以在springboot的文檔中查找。
- 每改動一次代碼,都需要使用maven從新打包,然後在重新開機tomcat伺服器,這樣項目才能被tomcat正确運作。