一、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核心配置文件中设置的值