打成war包
项目配置
-
创建一个springboot的jsp应用
-
pom.xml
<!-- 执行打包是war包-->
<packaging>war</packaging>
<build>
<!--打包后的文件名称-->
<finalName>myboot</finalName>
<!--resources插件, 把jsp编译到指定的目录-->
<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>
- controller
package com.example.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@RequestMapping("/user/toLogin")
public String toLogin(Model model){
model.addAttribute("data", "need to login page");
return "toLogin";
}
}
- application.properties中配置视图解析器
#配置视图解析器
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
- webapp目录下的toLogin.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>toLogin page</title>
</head>
<body>
从后台获取的数据: ${data}
</body>
</html>
- 主启动类继承SpringBootServletInitializer, 重写configure方法
package com.example;
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;
@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);
}
}
- 部署war:把war放到tomcat等服务器的发布目录中。以tomcat为例,myboot.war放到tomcat/webapps目录中
打成jar包
- 去掉打成war包的标签
<packaging>war</packaging>
- 指定springboot-maven-plugin版本
<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>
- 最后执行maven的clean插件,清除之前打的包,然后再次打包
- 在target目录中,生成jar 文件,例子是myboot.jar
- 可以执行独立的springboot项目(已经配置了java环境):在cmd中执行java -jar myboot.jar