1. 打包  85

Spring Boot 可以打包为 war 或 jar 文件。 以两种方式发布应用

2. Spring Boot 打包为 war

创建 Spring Boot web 项目: course13

2.1  pom.xml  85

在 pom.xml 文件中配置内嵌 Tomcat 对 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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.11</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bjpowernode</groupId>
    <artifactId>course13</artifactId>
    <version>1.0.0</version>
    <!--打包类型-->
    <packaging>war</packaging>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

        <!--加入处理jsp的依赖  85-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

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

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

    <build>

        <!--打包后的文件名称-->
        <finalName>myboot</finalName>

        <!--resources插件, 把jsp编译到指定的目录  85-->
        <resources>
        <resource>
            <directory>src/main/webapp</directory>
            <targetPath>META-INF/resources</targetPath>
            <includes>
                <include>**/*.*</include>
            </includes>
        </resource>

            <!--使用了mybatis ,而且mapper文件放在src/main/java目录-->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>

            <!--把src/main/resources下面的所有文件,都包含到classes目录-->
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>

        </resources>


        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.2 创建 webapp 目录  85

打包_spring

指定 webapp 是 web 应用目录

打包_spring_02

main.jsp

<%--打包为jar  88--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
     main.jsp ,显示数据 ${data}
</body>
</html>

2.3 创建 jsp 文件  85

<%--演示打包方式  85--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>index.jsp</title>
</head>
<body>
    index.jsp , 显示controller中的数据  ${data}
</body>
</html>

2.4 创建 JspWarController  85

JspController

package com.bjpowernode.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

//演示打包方式打包为war文件  85
@Controller
public class JspController {

    @RequestMapping("/main")
    public String main(Model model){
        model.addAttribute("data","SpringBoot打包为war文件");
        return "index";
    }
}

2.5 设置视图解析器  85

applicationg.properties

server.port=9001
server.servlet.context-path=/myjsp

#指定视图解析器  85
http://spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

2.6 启动主类,在浏览器访问地址 index

访问浏览器 http://localhost:9001/myjsp/main

打包_maven_03

2.7 主启动类继承 SpringBootServletInitializer  86-87

继承 SpringBootServletInitializer 可以使用外部 tomcat。SpringBootServletInitializer 就是原有的 web.xml 文件的替代。使用了嵌入式 Servlet,默认是不支持 jsp。

 继承这个类SpringBootServletInitializer

 才能使用独立tomcat服务器

JspApplication
package com.bjpowernode;

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;

/**
 * SpringBootServletInitializer: 继承这个类, 才能使用独立tomcat服务器  86
 */
@SpringBootApplication
public class JspApplication extends SpringBootServletInitializer {

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


    @Override
    protected SpringApplicationBuilder configure
            (SpringApplicationBuilder builder) {

       return builder.sources(JspApplication.class);
    }
}

指定项目 package 是 war

<!--打包类型-->
<packaging>war</packaging>

maven package 打包

打包_打包操作_04

发布打包后的 war 到 tomcat

在浏览器访问 web 应用

把打包结果放到tomcat的webapps目录下E:\java\dev2\apache-tomcat-9.0.65\webapps

启动tomcat,然后浏览器访问http://localhost:8080/myboots/main

打包_打包操作_05

3. 打包jar包  88

pom.xml

<?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.7.12</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bjpowernode</groupId>
    <artifactId>course13_1</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

        <!--tomcat依赖,处理jsp-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

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

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

    <build>

        <!--打包后的文件名称  88-->
        <finalName>myboot</finalName>
        <!--加入resources插件 -->
        <!--指定编译jsp到META-INF/resources-->
        <resources>
            <resource>
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>

            <!--如果使用mybatis,同时把xml文件放在了src/main/java目录-->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>


            <!--把src/main/resources中的所有文件编译到classpath目录中-->
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!--打包jar, 有jsp文件时,必须指定maven-plugin插件的版本是 1.4.2.RELEASE-->
                <version>1.4.2.RELEASE</version>
            </plugin>
        </plugins>
    </build>

</project>

创建 webapp 目录  88

打包_xml_06

指定 webapp 是 web 应用目录

打包_打包操作_07

HelloController

package com.bjpowernode.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

//打包放肆jar  88
@Controller
public class HelloController {

    @RequestMapping("/hello")
    public ModelAndView hello(){
        ModelAndView mv  = new ModelAndView();
        mv.addObject("data","SpringBoot打包为jar");
        mv.setViewName("main");
        return mv;
    }
}

 设置视图解析器

applicationg.properties

#端口
server.port=9002
server.servlet.context-path=/myboot

#配置视图解析器  88
http://spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

启动主类  88

浏览器输入http://localhost:9002/myboot/hello

打包_spring_08

打包  89

在myboot.jar--在Explorer中显示----cmd---输入java -jar myboot.jar运行

---浏览器输入http://localhost:9002/myboot/hello

打包_spring_09

打包_xml_10

打包_xml_11

打包_xml_12

4. Spring Boot 部署和运行方式总结  89-91

➢ 在 IDEA 中直接运行 Spring Boot 程序的 main 方法(开发阶段)

➢ 用 maven 将 Spring Boot 安装为一个 jar 包,使用 Java 命令运行

java -jar springboot-xxx.jar 

可以将该命令封装到一个 Linux 的一个 shell 脚本中(上线部署)

◼ 写一个 shell 脚本:

#!/bin/sh

java -jar xxx.jar

◼ 赋权限 chmod 777 run.sh

◼ 启动 shell 脚本: ./run.sh