详解SpringBoot 添加对JSP的支持(附常见坑点)
序言:
SpringBoot默认不支持JSP,如果想在项目中使用,需要进行相关初始化工作。为了方便大家更好的开发,本案例可直接作为JSP开发的脚手架工程 SpringBoot+War+JSP .
常见问题:
1.修改JSP需重启才能生效:
在生产环境中,SpringBoot重新编译JSP可能会导致较大的性能损失,并且很难追查到问题根源,所以在最新的版本中,官方已经默认关闭此功能,详见JspServlet类的初始化参数。那么,如何解决这个问题呢?推荐两个解决办法:1.使用devtools 2. 添加配置(server.servlet.jsp.init-parameters.development=true)
2.各种404:
1.必须导入嵌入式容器和JASPER解析器 2.必须创建webapp目录
正文:SpringBoot 添加对JSP的支持
1. 搭建脚手架
首先使用 Spring Initializr 构建工程,其中源码和静态资源目录默认生成,这里只需手工添加Web资源目录。如图:
2. 在pom.xml 添加相关依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!--基本信息 -->
<modelVersion>4.0.0</modelVersion>
<groupId>com.hehe</groupId>
<artifactId>springboot-web-jsp</artifactId>
<version>0.0.1-SNAPSHOT</version><!--打包格式:SpringBoot使用JSP时需打包为war类型 -->
<packaging>war</packaging><!--继承父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M4</version>
<relativePath/>
</parent><!--依赖管理 -->
<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>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies><!--指定远程仓库(含插件) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories><!--构建插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build></project>
3. 启动类添加Servlet支持
<@SpringBootApplication
public class SpringbootWarJspApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringbootWarJspApplication.class);
}public static void main(String[] args) {
SpringApplication.run(SpringbootWarJspApplication.class, args);
}
}
4. 添加MVC映射
application.yml 配置如下:
spring: mvc:
view:
prefix: /WEB-INF/views/ # Read From Web Resources Dir
suffix: .jsp
5. 编写JSP页面
在 WEB-INF/views 目录下新建一个JSP文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<body>
<marquee><p style="font-size: 100px">Hello JSP !!</p>
![](${pageContext.servletContext.contextPath}/doge.gif)
</marquee>
</body>
</html>
6.启动项目
启动方式1:在IDE启动WebJspApplication,然后打开项目地址。
启动方式2:部署到外置Tomcat,启动完成后,打开项目地址。这里需要注意的是,使用外置Tomcat部署的时候,需要将嵌入式容器调整为provided级别。(防止冲突)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
7.单元测试
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
public class WebJspApplicationTest {
@Autowired
private TestRestTemplate restTemplate;
@Test public void testJspWithEl() throws Exception {
ResponseEntity<String> entity = restTemplate.getForEntity("/", String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(entity.getBody()).contains("Hello JSP");
}
}