文章目录
- 1.使用外置的Servlet容器
- 2.先创建出SpringBoot项目
- 3.将嵌入式的Tomcat指定为procided
- 4.设置项目的目录结构
- 5.部署Tomcat
- 6.编写SpringBootServletInitializer的子类
- 7.启动程序
- 8.原理
1.使用外置的Servlet容器
嵌入式Servlet容器:应用打成可执行的jar
优点:简单、便携;
缺点:默认不支持JSP、优化定制比较复杂.;
外置的Servlet容器:外面安装Tomcat—应用war包的方式打包;
操作步骤
2.先创建出SpringBoot项目
2.1创建一个普通的mavenjava项目
2.2在pom.xml中,设置打包方式为war包,引入父工程模板,左侧设置三级包结构
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<relativePath/>
</parent>
2.3引入springboot的stater、web的starter、thymeleaf的模板引擎
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--加载web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.4创建启动类SpringBootTomcatApplication
package com.qcby.springbootTomcat;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootTomcatApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootTomcatApplication.class, args);
}
}
2.5编写配置文件
application.yml
server:
port: 8085
spring:
mvc:
view:
prefix: /WEB-INF/pages
suffix: .jsp
2.6编写页面和Controller
编写success.html
和SuccessController
<!DOCTYPE HTML>
<!--命名空间-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:text="111111111"></div>
</body>
</html>
package com.qcby.springbootTomcat.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class SuccessController {
@RequestMapping("/test")
public String success(){
System.out.println("222222222");
return "success";
}
}
3.将嵌入式的Tomcat指定为procided
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
4.设置项目的目录结构
创建包结构
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>success</title>
</head>
<body>
<h1>外部Tomcat的使用</h1>
</body>
</html>
<?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_3_1.xsd"
version="3.1">
</web-app>
5.部署Tomcat
6.编写SpringBootServletInitializer的子类
必须编写一个SpringBootServletInitializer的子类,并调用configure方法
package com.qcby.springbootTomcat;
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(SpringBootTomcatApplication.class);
}
}
7.启动程序
8.原理
jar包:执行SpringBoot主类的main方法,启动ioc容器,创建嵌入式的Servlet容器;
war包:启动服务器,服务器启动SpringBoot应用【SpringBootServletInitializer】,启动ioc容器;
标签:SpringBoot,spring,boot,springframework,SpringBootServletInitializer,外置,org,Servl From: https://blog.csdn.net/jlihan/article/details/144803058