SpringBoot---(6) SpringBoot打war、jar包与部署

本文详细介绍了如何将SpringBoot程序打包成war和jar包进行部署。对于war包部署,讲解了从添加JSP依赖、配置资源、创建控制器到打包和在Tomcat上运行的完整流程。对于jar包部署,强调了不同版本打包插件的影响,并展示了通过java命令执行jar包的方法。两种部署方式的端口号和上下文路径设置也进行了对比。
摘要由CSDN通过智能技术生成

一、Spring Boot 程序 war 包部署

1、首先创建一个SpringBoot Web工程,这里就省略该步骤了
2、添加SpringBoot解析jsp依赖
<!--SpringBoot 只解析 JSP 页面依赖-->
<dependency>
 <groupId>org.apache.tomcat.embed</groupId>
 <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
3、创建webapp用来存放jsp文件并指定为web资源文件夹

在这里插入图片描述

4、在 pom.xml 文件中配置 jsp 文件解析目录
<!--
 SpringBoot 要求 jsp 文件必须编译到指定的 META-INF/resources 目录下,否则不能访问
-->
<resources>
	 <resource>
		 <!--源文件位置-->
		 <directory>src/main/webapp</directory>
		 <!--指定编译到 META-INF/resources 目录下,该目录不能随便编写-->
		 <targetPath>META-INF/resources</targetPath>
		 <!--指定包含文件-->
		 <includes>
		 	<include>**/*.*</include>
		 </includes>
	 </resource>
</resources>
5、在 application.properties 配置文件中配置 jsp 的前后缀
#设置 jsp 的前/后缀
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
6、创建 IndexController 提供方法分别返回字符串及跳转页面
@Controller
public class IndexController {

    @RequestMapping(value = "/say")
    public ModelAndView say(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("message","hello,SpringBoot");
        mv.setViewName("say");
        return mv;
    }
    @RequestMapping(value = "/index")
    public String index(Model model){
        model.addAttribute("message","hello");
        return "say";
    }
}

7、在 src/main/webapp 目录下创建 say.jsp
<body>
    ${message}
</body>
8、浏览器输入地址访问测试

在这里插入图片描述

9、将项目打包成war包
(1)在 pom.xml 中添加打包方式为 war
<packaging>war</packaging>
(2)在 pom.xml 中配置将配置文件编译到类路径
<resource>
	 <!--源文件夹-->
	 <directory>src/main/webapp</directory>
	 <!--目标文件夹-->
	 <targetPath>META-INF/resources</targetPath>
	 <!--包含的文件-->
	 <includes>
	 	<include>**/*.*</include>
	 </includes>
	</resource>
	<!--mybatis 的 mapper.xml-->
	<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>
(3)在 pom.xml 的 build 标签下通过 finalName 指定打 war包的名字
<!--指定打 war 包的名字-->
<finalName>springboot</finalName>
10、程序入口类需扩展继承 SpringBootServletInitializer类并覆盖 configure 方法
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        //参数为当前 SpringBoot 启动类
        return builder.sources(Application.class);
    } 
}
11、通过 Maven package 命令打 war 包到 target 目录下

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

12、部署到 Tomcat 服务器上测试运行,将 target 目录下生成的 war 包拷贝到 tomcat 的 webapps目录,并启动 tomcat

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

13、通过浏览器访问

在这里插入图片描述

注意
springboot打war包,部署到tomcat中,之前在application.properties设置的上下文跟和端口号就失效了

二、Spring Boot 程序 jar 包部署

1、创建SpringBoot项目(方法同上)
2、在去掉pom.xml 中添加打包方式为 war这行代码
3、修改 pom.xml 文件中打包插件的版本

注意
默认 SpingBoot 提供的打包插件版本为 2.2.2.RELEASE,这个版本打的 jar 包 jsp 不能访问,我们这里修改为 1.4.2.RELEASE(其它版本测试都有问题)

<!-- SpringBoot 提供打包编译插件 -->
<plugin>
	 <groupId>org.springframework.boot</groupId>
	 <artifactId>spring-boot-maven-plugin</artifactId>
	 <version>1.4.2.RELEASE</version>
</plugin>
4、修改 application.properties 配置文件(主要修改一下端口号为9090,就是为了验证一下和打war包的区别)
server.port=9090
server.servlet.context-path=/
#配置视图解析器
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
5、创建 IndexController提供方法分别返回字符串及创建跳转页面(同上)
6、通过 maven package 打包

在这里插入图片描述

7、通过 java 命令执行 jar 包,相当于启动内嵌 tomcat

将 target 下的 jar 包拷贝到某一个目录,在该目录下执行 java -jar springboot-xxx.jar命令行
在这里插入图片描述
在这里插入图片描述

8、浏览器访问测试

在这里插入图片描述

注意
springboot打jar包,部署到tomcat中,端口号和上下文跟就是springboot核心配置文件中设置的值

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@烟雨倾城ゝ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值