Spring-Boot学习笔记之四:支持JSP

Spring Boot默认情况下(只依赖spring-boot-starter-web)不支持jsp,支持jsp需要如下步骤。

步骤一:添加JSP相关依赖

在pom.xml文件中添加依赖tomcat-embed-jasper和jstl。

1
2
3
4
5
6
7
8
9
<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>

步骤二:添加MVC配置

在application.properties文件中添加

1
2
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

步骤三:创建jsp文件和对应的实体类

添加实体类。

1
2
3
4
5
6
7
8
9
10
package com.dongyuxi.test.model;

import lombok.Data;

@Data
public class JSPDemoModel {

private String name;

}

添加jsp文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page
import="com.dongyuxi.test.model.JSPDemoModel"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring Boot JSP Demo</title>
</head>
<body>
<%
final JSPDemoModel model = (JSPDemoModel)request.getAttribute("jspDemoModel");
if (model != null) {
%>
Hello <%=model.getName() %>
<%
}
%>
</body>
</html>

步骤四:创建测试Controller类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dongyuxi.test.model.JSPDemoModel;

@Controller
public class JSPDemoController {

@RequestMapping("/jsptest")
public ModelAndView test(@RequestParam(value = "name", required = false) final String name) {
if (name == null) {
return new ModelAndView("demo");
} else {
final JSPDemoModel model = new JSPDemoModel();
model.setName(name);
return new ModelAndView("demo", "jspDemoModel", model);
}
}

}

步骤五:访问测试页面

访问http://localhost:8080/jsptest?name=dongyuxi,看到

1
Hello dongyuxi


支付宝 微信
文章目录
  1. 1. 步骤一:添加JSP相关依赖
  2. 2. 步骤二:添加MVC配置
  3. 3. 步骤三:创建jsp文件和对应的实体类
  4. 4. 步骤四:创建测试Controller类
  5. 5. 步骤五:访问测试页面