第一步加依赖,:
配路径
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
... ...
<build>
<resources>
<resource>
<!-- 指定编译打包时 将 src/main/webapp 目录下的 **/*.jsp 文件打包到 META-INF/resources 文件夹下 -->
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/*.jsp</include>
</includes>
</resource>
</resources>
</build>
第二步:
创建目录 src/main/webapp 编写jsp 文件
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%
String path = request.getContextPath();
String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
<base href="<%=basepath %>"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Insert title here</title>
</head>
<body>
我的名字叫 ${name}
</body>
</html>
第三步:编写 controller
@Controller
public class JspController {
//如果想在 @Controller 中返回json 需要使用 @ResponseBody 注解
@RequestMapping("getName")
public @ResponseBody Map getName(){
Map map = new HashMap();
map.put("id","11");
map.put("name","zll");
return map;
}
@RequestMapping("/getJsp")
public String getJsp(Model model){
model.addAttribute("name","zll");
return "index";
}
}
编写配置文件 application.properties
#/斜杠代表相对路径 等价于 src/main/webapp
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp