ch09-SpringBoot 打包
Spring Boot 可以打包为 war 或 jar 文件。 以两种方式发布应用。
1.1 SpringBoot 打包为 war
创建 SpringBoot 的 web 项目: 017-springboot-war,由于没有学习 Thymeleaf 模板,以jsp项目为例。
1.1.1 pom.xml 文件
<groupId>com.suyv</groupId>
<artifactId>ch17-springboot-war</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--处理 jsp 的依赖-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--处理 web 的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
<build>
<!--打包文件名-->
<finalName>myboot</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!--resources 插件,把jsp文件编译到指定的目录-->
<resources>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
1.1.2 创建 webapp 目录

1.1.3 指定 webapp 为 web 目录

1.1.4 创建 jsp 文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>index.jsp</title>
</head>
<body>
显示Controller中的数据:${data}
</body>
</html>
1.1.5 创建 Controller
package com.suyv.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class JspController {
@RequestMapping("/war")
public String war(Model model){
model.addAttribute("data","Springboot打包为war文件");
return "index";
}
}
1.1.6 配置视图解析器

1.1.7 启动主类,访问地址

1.1.8 主启动类继承 SpringBootServletInitializer
继承 SpringBootServletInitializer 可以使用外部 tomcat。
SpringBootServletInitializer 就是原有的 web.xml 文件的替代。使用了嵌入式 Servlet,默认是不支持 jsp。
package com.suyv;
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服务器
*/
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
}
1.1.9 maven package 打包

1.1.10 部署war
把 war 放到 tomcat 服务器的发布目录中,
myboot.war放到tomcat/webapps目录。

1.2 SpringBoot 打包为 jar
创建 SpringBoot 项目:018-springboot-jar,由于没有学习 Thymeleaf 模板,以jsp项目为例。
1.2.1 pom.xml 文件
<groupId>com.suyv</groupId>
<artifactId>ch18-springboot-jar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--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>
<!--指定打包后的名称-->
<finalName>myboot</finalName>
<!--资源插件-->
<resources>
<!--指定 jsp 文件编译后目录-->
<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>
<!--指定 resources 下面的所有资源-->
<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>
1.2.2 创建 webapp 目录

1.2.3 指定 webapp 为 web 目录

1.2.4 创建 jsp 文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>main.jsp</title>
</head>
<body>
main.jsp,显示数据:${data}
</body>
</html>
1.2.5 创建 Controller
package com.suyv.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class JspController {
@RequestMapping("/jar")
public ModelAndView jar(){
ModelAndView mv = new ModelAndView();
mv.addObject("data","SpringBoot打成Jar包");
mv.setViewName("main");
return mv;
}
}
1.2.6 配置视图解析器

1.2.7 启动主类,访问地址

1.2.8 maven package 打包

1.2.9 执行 jar












