先创建一个项目,一个能正常运行的项目,再找一个能正常运行的tomcat容器。

修改pom.xml文件。下面的文件大部分都是系统自动生成的,只有打下划线的是为了打War包需要的。

<?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>
    <groupId>com.example</groupId>
    <artifactId>spring026-war</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring026-war</name>
    <description>Demo project for Spring Boot</description>
    
    <!--修改打包方式-->
    <packaging>war</packaging>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--springboot内嵌tomcat解析jsp的依赖-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <!--指定打war包的名称-->
        <finalName>springboot001</finalName>
<!--jsp资源存放的目录-->
        <resources>
            <resource>
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>*.*</include>
                </includes>
            </resource>
//把resources里的文件都扫描到war包里
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

//扫描不到web.xml文件时,就跳过这个文件而且不报错。 <plugin> <artifactId>maven-war-plugin</artifactId> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.7.RELEASE</version> <configuration> <mainClass>com.example.Spring026WarApplication</mainClass> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>

 然后,修改主入口程序。

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class Spring026WarApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Spring026WarApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        //这个参数为当前springboot启动类
        //构建新资源
        return builder.sources(Spring026WarApplication.class);
    }
}

然后,点击idea窗口最右边的Maven Projects,在里面选择本项目中的Lifecycle中的clean,双击,再选中package,双击,就行了。

 

然后选择左边的项目WAR包,copy到tomcat中的webapp中,

双击tomcat\bin\startup.bat,启动tomcat,然后,访问http://127.0.0.1:8080/项目名称/指定路径(就是在controller函数下的指定路径)就可以了!

 

tomcat有时会不能显示中文,修改tomcat\conf下的logging.properties,将里面的UTF-8改成GBK就可以解决。

另外,如果是刚下载的tomcat是没有超级用户名和密码的,需要修改tomcat\config下的tomcat-users.xml,在</tomcat-users>之前加上<user username="admin" password="1234" roles="manager-gui"/>就行了!