Spring Boot 整合 JSP

一、项目说明

二、整合 JSP

2.1 基本依赖

导入整合所需的依赖:

<!--整合 jsp 依赖包-->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>
<!--jsp jstl 标签支持-->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

2.2 配置视图

在 application.yml 中指定访问视图文件的前缀和后缀 :

spring:
  mvc:
    view:
      prefix: /WEB-INF/jsp/
      suffix: .jsp

2.3 整合测试

新建 controller 和 show.jsp 测试整合是否成功:

@Controller
@RequestMapping("index")
public class JspController {
    @RequestMapping
    public String jsp(Model model){
        Programmer programmer = new Programmer("heibai", 21, 1298.31f, LocalDate.now());
        model.addAttribute("programmer",programmer);
        return "show";
    }
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>programmer</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/show.css">
</head>
<body>
<ul>
    <li>姓名: ${programmer.name}</li>
    <li>年龄: ${programmer.age}</li>
</ul>
</body>
</html>