SpringBoot打成war包,部署Tomcat服务器

 

1: 创建spring boot项目

使用 Spring initializr  可以直接选择创建包的方式

也可以选择在Pom中更改

    <groupId>com.dgw</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

2: 创建初始化类

使用 Spring initializr  自动已经为我们创建完成了, 当然可以选择手动创建下面的初始化器 继承 SpringBootServletInitializer

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(DemoApplication.class);
    }

}

2.1配置视图解析器访问路径

在application.properties 或者 yaml 下配置

spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp

3: 创建webapp目录

Spring Boot 在 Project Structure 中创建

 Web Resource 创建  这个路径

srcmainwebapp

 Deployment Descriptors 创建web.xml

# 路径

srcmainwebappWEB-INFweb.xml

web.xml配置信息

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0">

</web-app>

正常创建的目录结构

4: 配置测试

创建控制器

@RestController
public class FisrtController {

    @GetMapping("/sayhi")
    public ModelAndView sayHi(){
        ModelAndView andView = new ModelAndView();
        andView.setViewName("success");
        andView.addObject("msg","HELLO");
        return andView;
    }
}

  webapp 路径 创建index.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
        <a href="sayhi">send msg</a>
</body>
</html>

webappWEB-INF 下创建success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h2>${msg}</h2>
</body>
</html>

结果:

 附录 POM:

<?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.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dgw</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>demo</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>
原文地址:https://www.cnblogs.com/dgwblog/p/12003177.html