SpringBoot打war包——打jar包

1. 打war包——Tomcat部署

SpringBoot工程默认打的包都是jar包,这次打war包
jar包可以直接运行,war包需要部署在tomcat上
war包本来就是放在tomcat里面用的

<!--修改打包方式,,,打成war-->
<packaging>war</packaging>

<build>
    <!--指定打war包的字符集-->
    <finalName>springboot</finalName>
 </build>

步骤:

  1. 引入依赖(打war包,指定字符集;JSP依赖,编译JSP并放在指定路径下,编译配置文件)
  2. 核心配置文件(application.properties配置文件中配置:JSP的前/后缀)
  3. 创建控制层(返回字符串和页面)
  4. 创建页面userDetail.jsp 展示数据
  5. 打war包——程序入口类继承SpringBootServletInitializer类 覆盖configure方法
  6. 本地Tomcat启动
  7. 浏览器输入地址访问测试

1.1 引入依赖

打war包,指定字符集;JSP依赖,编译JSP并放在指定路径下,编译配置文件

<?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.guo.springboot</groupId>
    <artifactId>springboot-war</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!--修改打包方式,,,打成war-->
    <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>
        <!--SpringBoot框架web项目起步依赖-->
        <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>springboot</finalName>
        <resources>
            <!--指定JSP文件编译路径为META-INF/resources-->
            <resource>
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>*.*</include>
                </includes>
            </resource>
            <!--编译resources目录下的*.properties文件-->
            <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>
            <!--SpringBoot打包插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.guo.springboot.Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

1.2 核心配置文件

application.properties配置文件中配置:JSP的前/后缀

#设置视图解析器 JSP的前/后缀
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

1.3 创建控制层

返回字符串和页面

package com.guo.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;

@Controller
public class UserController {
    @RequestMapping("/user/detail")
    public @ResponseBody Object userDateil(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("id","1001");
        map.put("name","zhangsan");
        return map;
    }
    @RequestMapping("/user/page/detail")
    public String userPageDateil(Model model){
        model.addAttribute("id","1002");
        model.addAttribute("name","lisi");
        return "userDateil";
    }
}

1.4 创建页面

userDetail.jsp 展示数据

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>用户信息</h2>
<div>用户id: ${id}</div>
<div>用户名称:${name}</div>
</body>
</html>

1.5 程序入口类继承SpringBootServletInitializer类 覆盖configure方法

打war包——程序入口类继承SpringBootServletInitializer类 覆盖configure方法

package com.guo.springboot;

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 Application extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        //参数为当前SpringBoot启动类
        //构建新资源
        builder.sources(Application.class);
        return super.configure(builder);
    }
}

1.6 本地Tomcat启动

1.war包放在Tomcat解压包的【webapps】文件目录下

在这里插入图片描述

2.进入【bin】目录下双击【startup.bat】启动Tomcat服务

在这里插入图片描述

3.启动成功

在这里插入图片描述

在这里插入图片描述

4.日志出现乱码

在这里插入图片描述

5.解决Tomcat日志乱码办法
进入【conf】编辑【logging.properties】文件,修改编码格式UTF-8→GBK

在这里插入图片描述

1.7 浏览器输入地址访问测试

在这里插入图片描述

在这里插入图片描述

2. 打jar包——命令行运行

打jar包运行:直接在命令行通过命令就可以运行,SpringBoot项目内嵌Tomcat
java -jar springboot.jar

SpringBoot项目打包默认就是打jar包,
jar包端口号和上下文根就是SpringBoot核心配置文件中设置的值

2.1 引入依赖

<?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.guo.springboot</groupId>
    <artifactId>springboot-war</artifactId>
    <version>0.0.1-SNAPSHOT</version>
<!--
	这里不需要,SpringBoot默认打包就是jar
    &lt;!&ndash;修改打包方式,,,打成war&ndash;&gt;
    <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>
        <!--SpringBoot框架web项目起步依赖-->
        <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>springboot</finalName>
        <resources>
            <!--指定JSP文件编译路径为META-INF/resources-->
            <resource>
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>*.*</include>
                </includes>
            </resource>
            <!--编译resources目录下的*.properties文件-->
            <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>
            <!--SpringBoot打包插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.4.2.RELEASE</version>
                <configuration>
                    <mainClass>com.guo.springboot.Application</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

和打war包不同这里需要指定打包版本

<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<version>1.4.2.RELEASE</version>
</plugin>

2.2 核心配置文件

application.properties配置文件中配置:JSP的前/后缀

#设置请求端口
server.port=9090
#设置上下文跟
server.servlet.context-path=/
#设置视图解析器 JSP的前/后缀
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

2.3 本地运行

命令:java -jar springboot.jar
在这里插入图片描述

2.4 浏览器输入地址访问测试

在这里插入图片描述在这里插入图片描述

.
.
.
.
.
.
.
.

3. 杀死端口占用进程

  1. 在dos窗口中输入指令:netstat -ano | findstr 8080,其中8080是指你被占用的端口号,然后会显示出现在占用该端口号的pid。

  2. 输入 taskkill /f /pid 34564,关闭该进程即可

在这里插入图片描述

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐