1. Project preparation
Directly use the source code of the previous chapter, Spring Boot Tutorial (9): Spring Boot integrates Mapper4
2. Add dependencies
<!-- jsp -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
3. Modify the configuration file
Add jsp related configuration to the application.properties
configuration file:
## jsp
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
# 配置Tomcat编码
server.tomcat.uri-encoding=UTF-8
Fourth, create a jsp file
src/main
Createwebapp/WEB-INF/jsp
folders under.- create in
jsp
folderindex.jsp
index.jsp content:
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<title>首页</title>
</head>
<body>
hello ${userName}
</body>
</html>
5. Create Controller
Add one IndexController
with the following content:
package com.songguoliang.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* @Description
* @Author sgl
* @Date 2018-05-08 10:47
*/
@Controller
public class IndexController {
@GetMapping("/index")
public ModelAndView index() {
ModelAndView view = new ModelAndView("index");
view.addObject("userName", "蝈蝈");
return view;
}
}
6. Start the service
Start the service through spring-boot:run, just double-click in the idea:
Browser input: http://localhost:8080/index
, the page displays:
Seven, attention
Springboot does not officially recommend the use of jsp, but recommends it Thymeleaf
.
Source code:
github
code cloud