Spring-Boot-摸索之路-6:Spring-Boot 集成JSP

我知道Spring Boot 不适合JSP这种引擎,但是学校有项目要求使用JSP来完成

在Maven工程中导入以下依赖

1
2
3
4
5
6
7
8
9
10
11
12
<!-- JSTL for JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

<!-- Need this to compile JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>

在工程中建立图片中的路径

JSP

配置文件中加入以下配置

1
2
3
4
view:
#配置jsp文件
prefix: /WEB-INF/jsp/
suffix: .jsp

写一个JSP页面来测试一下吧!

hello.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%--
Created by IntelliJ IDEA.
User: allwayz
Date: 2020/2/18
Time: 01:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Hello</title>
</head>
<body>
<h1>
Hello World!
</h1>
</body>
</html>

Controller

1
2
3
4
5
6
7
@Controller
public class TestController {
@RequestMapping("/hello")
public String hello(){
return "hello"
}
}

Reference

Allwayz