拷贝刚才的工程demo,改为demo1
本章工程地址:D:\java21workspace\demo1
第二部分 jsp支持
热部署
实际开发中,修改某个页面数据或逻辑功能都需要重启应用。这无形中降低了开发效率,所以使用热部署是十分必要的。
什么是热部署?
应用启动后会把编译好的Class文件加载到虚拟机中,正常情况下在项目修改了源文件时需要全部重新编译并重新加载(需要重启应用)。而热部署就是监听Class文件的变动,只把发生修改的Class重新加载,而不需要重启应用,使得开发变得简便。
Spring Boot 配置实现热部署
添加依赖包,运行热部署,全部代码参见:demo-devtools工程
<!-- 热部署模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
配置支持jsp
Spring Boot使用的内嵌的tomcat
内嵌的tamcat是不支持jsp页面
需要导入额外的包
<!-- jsp支持 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
测试程序编写
新建source目录 src/main/webapp 默认的页面存放处
添加jsp页并测试
src\main\webapp\test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>访问jsp文件</h1>
</body>
</html>
测试运行
浏览器url里输入http://localhost:8080/test.jsp