Spring Boot - 将WAR文件部署到外部Tomcat上

x33g5p2x  于2022-10-06 转载在 Spring  
字(10.1k)|赞(0)|评价(0)|浏览(608)

在这篇文章中,我们将讨论如何将Spring Boot网络应用程序WAR文件部署到外部Tomcat servlet容器。

配置和部署Spring Web应用war文件到外部Tomcat servlet容器非常简单。

###Spring Boot WAR部署的三个步骤

  1. 改变包装类型。
  1. <packaging>war</packaging>
  1. 添加spring-boot-starter-tomcat作为provided范围
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-tomcat</artifactId>
  4. <scope>provided</scope>
  5. </dependency>
  1. Spring Boot ApplicationMain类扩展SpringBootServletInitializer
  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.boot.builder.SpringApplicationBuilder;
  4. import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
  5. @SpringBootApplication
  6. public class Springboot2WebappJspApplication extends SpringBootServletInitializer{
  7. @Override
  8. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  9. return application.sources(Springboot2WebappJspApplication.class);
  10. }
  11. public static void main(String[] args) {
  12. SpringApplication.run(Springboot2WebappJspApplication.class, args);
  13. }
  14. }

让我们使用JSP作为View来开发完整的Spring Boot 2 Web应用程序的步骤。

1. 我们将建立什么

我们将构建一个简单的Spring MVC Web应用程序,并将JSP作为视图,我们将把这个应用程序部署在一个外部的servlet容器中,这个容器就是Tomcat

2. 使用的工具和技术

  • Spring Boot - 2.0.4.RELEASE
  • JDK - 1.8或更高版本
  • Spring Framework - 5.0.8 RELEASE
  • Hibernate - 5.2.17.Final
  • Maven - 3.2以上
  • IDE - Eclipse或Spring Tool Suite (STS)
  • Tomcat - 8.5以上
  • JSP
  • Bootstrap 3+

3. 创建和导入一个项目

有很多方法可以创建Spring Boot应用程序。最简单的方法是使用http://start.spring.io/的Spring Initializr,它是一个在线Spring Boot应用程序生成器。

看上面的图,我们指定了以下细节。

  • Generate: Maven项目
  • Java Version: 1.8 (默认)
  • Spring Boot:2.0.4
  • Group: net.guards.springboot2
  • Artifact: springboot2webappjsp
  • Name: springboot2webappjsp
  • Package Name : net.guards.springboot2.springboot2webappjsp
  • Packaging: jar (这是默认值)
  • Dependencies: Web, JPA, MySQL, DevTools
    一旦,所有的细节被输入,点击生成项目按钮将生成一个spring boot项目并下载。接下来,解压下载的压缩文件并将其导入你最喜欢的IDE。

4. 包装结构

一旦我们将生成的spring boot项目导入IDE,我们将看到一些自动生成的文件。

  1. pom.xml
  2. resources
  3. Springboot2WebappJspApplication.java
  4. Springboot2WebappJspApplicationTests.java

5. pom.xml文件

  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>net.guides.springboot2</groupId>
  7. <artifactId>springboot2-webapp-jsp</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <packaging>war</packaging>
  10. <name>springboot2-webapp-jsp</name>
  11. <description>Demo project for Spring Boot</description>
  12. <parent>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-parent</artifactId>
  15. <version>2.0.4.RELEASE</version>
  16. <relativePath /> <!-- lookup parent from repository -->
  17. </parent>
  18. <properties>
  19. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  20. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  21. <java.version>1.8</java.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-data-jpa</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-devtools</artifactId>
  35. <scope>runtime</scope>
  36. </dependency>
  37. <dependency>
  38. <groupId>mysql</groupId>
  39. <artifactId>mysql-connector-java</artifactId>
  40. <scope>runtime</scope>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-starter-test</artifactId>
  45. <scope>test</scope>
  46. </dependency>
  47. <!-- JSTL for JSP -->
  48. <dependency>
  49. <groupId>javax.servlet</groupId>
  50. <artifactId>jstl</artifactId>
  51. </dependency>
  52. <!-- Need this to compile JSP -->
  53. <dependency>
  54. <groupId>org.apache.tomcat.embed</groupId>
  55. <artifactId>tomcat-embed-jasper</artifactId>
  56. <scope>provided</scope>
  57. </dependency>
  58. <dependency>
  59. <groupId>org.springframework.boot</groupId>
  60. <artifactId>spring-boot-starter-tomcat</artifactId>
  61. <scope>provided</scope>
  62. </dependency>
  63. <!-- Optional, test for static content, bootstrap CSS -->
  64. <dependency>
  65. <groupId>org.webjars</groupId>
  66. <artifactId>bootstrap</artifactId>
  67. <version>3.3.7</version>
  68. </dependency>
  69. </dependencies>
  70. <build>
  71. <plugins>
  72. <plugin>
  73. <groupId>org.springframework.boot</groupId>
  74. <artifactId>spring-boot-maven-plugin</artifactId>
  75. </plugin>
  76. </plugins>
  77. </build>
  78. </project>

注意,在pom.xml上面,我们的包装类型是war,tomcat启动器是provided范围。

6. Springboot2WebappJspApplication.java文件

这个类提供了一个带有*public static void main(String[] args)*方法的入口,你可以运行它来启动应用程序。

注意,SpringBootServletInitializer从传统的WAR部署中运行一个SpringApplication

  1. package net.guides.springboot2.springboot2webappjsp;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.boot.builder.SpringApplicationBuilder;
  5. import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
  6. @SpringBootApplication
  7. public class Springboot2WebappJspApplication extends SpringBootServletInitializer{
  8. @Override
  9. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  10. return application.sources(Springboot2WebappJspApplication.class);
  11. }
  12. public static void main(String[] args) {
  13. SpringApplication.run(Springboot2WebappJspApplication.class, args);
  14. }
  15. }

7. 创建一个名为User.java的JPA实体

  1. package net.guides.springboot2.springboot2webappjsp.domain;
  2. import javax.persistence.Entity;
  3. import javax.persistence.GeneratedValue;
  4. import javax.persistence.GenerationType;
  5. import javax.persistence.Id;
  6. import javax.persistence.Table;
  7. @Entity
  8. @Table(name = "user")
  9. public class User {
  10. @Id
  11. @GeneratedValue(strategy=GenerationType.AUTO)
  12. private Integer id;
  13. private String name;
  14. public User()
  15. {
  16. }
  17. public User(Integer id, String name)
  18. {
  19. this.id = id;
  20. this.name = name;
  21. }
  22. public Integer getId()
  23. {
  24. return id;
  25. }
  26. public void setId(Integer id)
  27. {
  28. this.id = id;
  29. }
  30. public String getName()
  31. {
  32. return name;
  33. }
  34. public void setName(String name)
  35. {
  36. this.name = name;
  37. }
  38. }

8. 创建Spring Data JPA存储库 - UserRepository.java

  1. package net.guides.springboot2.springboot2webappjsp.repositories;
  2. import org.springframework.data.jpa.repository.JpaRepository;
  3. import net.guides.springboot2.springboot2webappjsp.domain.User;
  4. public interface UserRepository extends JpaRepository<User, Integer>
  5. {
  6. }

9. 创建Spring控制器 - UserController.java

  1. package net.guides.springboot2.springboot2webappjsp.controllers;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.ui.Model;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import net.guides.springboot2.springboot2webappjsp.repositories.UserRepository;
  7. @Controller
  8. public class UserController {
  9. @Autowired
  10. UserRepository userRepo;
  11. @RequestMapping("/users")
  12. public String home(Model model) {
  13. model.addAttribute("users", userRepo.findAll());
  14. return "index";
  15. }
  16. }

10. 配置MySQL数据库和JSP视图解析器

配置application.properties以连接到你的MySQL数据库。让我们打开一个application.properties文件,并在其中添加以下数据库配置。

  1. spring.mvc.view.prefix: /WEB-INF/jsp/
  2. spring.mvc.view.suffix: .jsp
  3. logging.level.org.springframework=INFO
  4. ################### DataSource Configuration ##########################
  5. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  6. spring.datasource.url=jdbc:mysql://localhost:3306/users_database
  7. spring.datasource.username=root
  8. spring.datasource.password=root
  9. ################### Hibernate Configuration ##########################
  10. spring.jpa.hibernate.ddl-auto=update
  11. spring.jpa.show-sql=true

11. 插入SQL脚本

一旦你运行这个应用程序,将在数据库中创建users表,并使用下面的插入SQL脚本在users表中填充一些记录。

  1. INSERT INTO `users_database`.`user` (`id`, `name`) VALUES ('1', 'Salman');
  2. INSERT INTO `users_database`.`user` (`id`, `name`) VALUES ('2', 'SRK');
  3. INSERT INTO `users_database`.`user` (`id`, `name`) VALUES ('3', 'AMIR');
  4. INSERT INTO `users_database`.`user` (`id`, `name`) VALUES ('4', 'Tiger');
  5. INSERT INTO `users_database`.`user` (`id`, `name`) VALUES ('5', 'Prabhas');

12. 创建一个JSP视图 - users.jsp

让我们创建一个users``.jsp视图来显示用户的列表。在webapp/WEB-INF/jsp文件夹下找到。

  1. <!DOCTYPE html>
  2. <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  4. <html lang="en">
  5. <head>
  6. <link rel="stylesheet" type="text/css"
  7. href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" />
  8. <c:url value="/css/main.css" var="jstlCss" />
  9. <link href="${jstlCss}" rel="stylesheet" />
  10. </head>
  11. <body>
  12. <div class="container">
  13. <header>
  14. <h1>Spring MVC + JSP + JPA + Spring Boot 2</h1>
  15. </header>
  16. <div class="starter-template">
  17. <h1>Users List</h1>
  18. <table class="table table-striped table-hover table-condensed table-bordered">
  19. <tr>
  20. <th>Id</th>
  21. <th>Name</th>
  22. </tr>
  23. <c:forEach var="user" items="${users}">
  24. <tr>
  25. <td>${user.id}</td>
  26. <td>${user.name}</td>
  27. </tr>
  28. </c:forEach>
  29. </table>
  30. </div>
  31. </div>
  32. <script type="text/javascript"
  33. src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  34. </body>
  35. </html>

13. 运行应用程序

  1. 这是一个maven项目,所以用以下命令创建一个war文件。
  1. mvn clean install or mvn clean package
  1. 一旦maven构建成功,WAR文件将在目标文件夹下生成。
    将war文件复制到外部tomcat webapps文件夹,然后启动tomcat服务器即可。

  2. 在浏览器中点击这个链接 - http://localhost:8080/springboot2-webapp-jsp/users
    注意,你可以将上下文名称从springboot2-webapp-jsp-0.0.1-SNAPSHOT改为springboot2-webapp-jsp,或者按照你的要求。

GitHub 仓库

本文的源代码可在我的GitHub仓库中找到 - https://github.com/RameshMF/spring-boot-tutorial/tree/master/springboot2-webapp-jsp-WAR在Spring Boot教程中学习完整的Spring Boot。

相关文章