下載地址:https://files.cnblogs.com/files/xiandedanteng/SpringBootWeb-1_20190928.rar
修改Java文件后,每次要重啟才好用,修改一下pom.xml,增加一個dependency,可以讓Tomcat容器重新加載修改java類的class,這樣能做到熱部署。
這個denpendency就是spring-boot-devtools,具體寫法如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.hy</groupId> <artifactId>SpringBootWeb-1</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringBootWeb-1</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</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> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
增加這個依賴后,在控制器類里再增加一個跳轉,
@RequestMapping("/hot") public String hot() { return "new.html"; }
再把要跳轉的網頁准備好:
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>標題</title> </head> <body> <h2>熱部署</h2> </body> </html> <script type="text/javascript"> <!-- // 腳本 //--> </script>
准備完畢不用重啟,直接在瀏覽器的地址欄中輸入http://localhost:8080/hot
以下頁面就會出現:
不用重啟Tomcat,確實方便不少。
--END-- 2019年9月28日08:00:44