SpringBoot.14.SpringBoot使用jsp模板打包成jar

x33g5p2x  于2021-10-31 转载在 Spring  
字(5.7k)|赞(0)|评价(0)|浏览(180)

前言

关于SpringBoot使用jsp模板打包成jar的方式运行在文章SpringBoot.08.SpringBoot文件上传与下载中我们简单的提到过一次,这里我们来详细记录一下过程和注意事项

实现步骤

这里我们就以文章SpringBoot.12.SpringBoot中的拦截器中的Module为例进行打包测试

1.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.5.0</version>
        <relativePath/>
    </parent>

    <groupId>com.christy</groupId>
    <artifactId>springboot-08-interceptor</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-08-interceptor</name>
    <description>Demo project for Spring Boot</description>
    <!-- 指定打包方式为jar(默认) -->
    <packaging>jar</packaging>

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

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

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

        <!-- druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.6</version>
        </dependency>

        <!-- mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>

        <!-- mybatis-spring-boot-starter 由于springboot整合mybatis版本中默认依赖mybatis 因此不需要额外引入mybati版本,否则会出现冲突 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <!-- 每次新建的项目如果需要开启热部署都需要引入该依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!-- c标签库 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- 让内嵌tomcat具有解析jsp功能 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

        <!-- 文件上传相关 -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.8.0</version>
        </dependency>

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.4</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-io</groupId>
                    <artifactId>commons-io</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 一定要注意mvn的插件一定是1.4.2.RELEASE,否则jsp访问不到 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.4.2.RELEASE</version>
            </plugin>
        </plugins>

        <resources>
            <!-- 打包时将jsp文件拷贝到META-INF目录下-->
            <resource>
                <!-- 指定resources插件处理哪个目录下的资源文件 -->
                <directory>src/main/webapp</directory>
                <!-- 注意必须要放在此目录下才能被访问到 -->
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>

            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/**</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
</project>
<build>
 <plugins>
   <!-- 一定要注意mvn的插件一定是1.4.2.RELEASE,否则jsp访问不到,如下图所示 -->
   <plugin>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-maven-plugin</artifactId>
     <version>1.4.2.RELEASE</version>
   </plugin>
 </plugins>

 <resources>
   <!-- 打包时将jsp文件拷贝到META-INF目录下-->
   <resource>
     <!-- 指定resources插件处理哪个目录下的资源文件 -->
     <directory>src/main/webapp</directory>
     <!-- 注意必须要放在此目录下才能被访问到 -->
     <targetPath>META-INF/resources</targetPath>
     <includes>
       <include>**/**</include>
     </includes>
   </resource>

   <resource>
     <directory>src/main/resources</directory>
     <includes>
       <include>**/**</include>
     </includes>
     <filtering>false</filtering>
   </resource>
 </resources>
</build>

2.打包

我们点击IDEA的右侧边栏的Maven标签,依次点击springboot-08-interceptor下面的Lifecycle下的clearpackage命令。如下图所示:

package命令执行完毕后,我们能够在项目下面的target包下面看到我们生成的jar包。如下图所示:

3.上传至服务器

打开我们Linux服务器Christy(BaseOS),ip地址为192.168.8.100。如下图所示:

执行cd命令进入到/usr/apps目录下,执行rz命令,选择刚才生成的jar进行上传。如下图所示:

4.运行

执行命令java -jar springboot-08-interceptor-0.0.1-SNAPSHOT.jar nohup &可以后台运行该程序,如下图所示:

此时我们执行命令Ctrl+C,该程序会切换到后台运行,我们执行jps命令会发现该进程依然存在,如下图所示:

5.测试

我们浏览器访问http://192.168.8.100:8808/interceptor.jsp页面可以正常访问。如下图所示:

当我们浏览器访问http://192.168.8.100:8808/interceptor/sayHello时控制台会输出以下内容:

当有请求访问该程序时程序自动转换到前台运行,非常方便。

相关文章

微信公众号

最新文章

更多