ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
SpringBoot 默认使用它自身嵌入的 Tomcat 服务器,而且默认不支持 jsp 模板引擎。 <br/> 如果非要使用外部的 Tomcat 和 jsp 引擎,可以如下配置: **1. 创建SprintBoot项目** ![](https://img.kancloud.cn/ea/9f/ea9fb1abc8547df97585208dcf9bcb87_1598x801.jpg) **2. 打包方式必须选择`War`** ![](https://img.kancloud.cn/ba/e0/bae0e060e39b6f6866e69240f74a95c9_1377x499.jpg) **3. ServletInitializer类** 项目创建成功后,使用 War 创建的 SpringBoot 项目会自动创建一个 ServletInitializer 类,必须存在该类外部的 Tomcat 才能使用。 ```java import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(OutmcatApplication.class); } } ``` **4. 添加webapp目录** ![](https://img.kancloud.cn/39/95/3995d15082fdcdd4b0a6a04880f6ab79_1200x459.jpg) *`/mian/webapp/WEB-INF/web.xml`* ```xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> ``` **5. 添加外部Tomcat支持** ![](https://img.kancloud.cn/d2/cb/d2cb4e9a5d58f3750d018821fca84229_1200x198.jpg) ![](https://img.kancloud.cn/99/c2/99c22b6d54d54c469dada1bb4f7b3525_1200x416.jpg) ![](https://img.kancloud.cn/f9/87/f9879750b83f09d973a57047439ff3a1_1286x388.png) ![](https://img.kancloud.cn/bf/69/bf69ce6f45abd4fb3d93c60af8122314_930x470.png) **6. `resources/application.properties`** ```properties spring.mvc.view.prefix=/WEB-INF/ spring.mvc.view.suffix=.jsp ``` **7. 将存spring-boot-starter-tomcat的scope指定为provided** ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <version>2.3.5.RELEASE</version> <!-- 如果存在-starter-tomcat时,需要将scope指定为provided --> <scope>provided</scope> </dependency> ``` <br/> 完成上面的配置就可以使用 jsp 模板引擎了。