SpringBoot配置外部Servlet
1.创建 JavaMaven 项目
创建 JavaMavenWeb 项目会更简单,不需要设置打包方式和创建 WEB-INF 目录
2.修改配置文件 pom.xml
(1)导入父工程
(2)设置打包方式为 war 包
(3)导入 SpringBoot 相关依赖
(4)指定嵌入式 Tomcat 作用域指定为 provided
3.创建 webapp
(1)创建 WEB-INF 和pages 目录
(2)创建 web.xml 配置文件
Archetype Created Web Application
(3)创建 hello.jsp 界面
success success222
外置 Servlet 容器实现
4. 配置 Tomcat
File —》Project Structure —》Modules 中检查打包位置
5.配置Tomcat
6.创建三级包并启动类
Controller 类
package com.goose.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping(\"/say\")public class HelloTomcat { @RequestMapping(\"/hello\") public String hello(){ return \"hello\"; }}
启动类
package com.goose;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class MyselfTomcat1 { public static void main(String[] args) { SpringApplication.run(MyselfTomcat1.class,args); }}
7.创建 SpringBoot 配置文件
spring: mvc: view: prefix: /WEB-INF/pages/ suffix: .jsp
8.创建配置类,并调用configure方法
package com.qcby;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(Demo1.class); }}
启动Tomcat
成功跳转