Article Directory
Precondition
- Know and familiar with
maven
technology - Learned jsp page view technology
- Understand familiar with spring mvc technology
Integration
1. Import related packages
Because the springboot official does not want programmers to use jsp technology, there is no JSP-related jar package provided in the springboot-web launcher, so we need to import it automatically
<!-- jstl表达式支持包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- jsp引擎 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.38</version>
</dependency>
2. Configure application.properties
The application file needs to be placed in the root directory of the classpath, and the corresponding in the maven project is src/main/resources
The content is as follows
# jsp文件所在路径
spring.mvc.view.prefix=/WEB-INF/jsp/
# jsp文件的后缀名
spring.mvc.view.suffix=.jsp
Friends who are familiar with springmvc technology will definitely be impressed by these two sentences, which are used to configure the JSP file path mapping in mvc
3. Realize
controller
package cn.liuhao.jsp.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import cn.liuhao.jsp.pojo.User;
@Controller
public class UserController {
@RequestMapping("userlist")
public ModelAndView UserList(ModelAndView view) {
List<User> list = new ArrayList<User>();
list.add(new User(1, "张三", 90));
list.add(new User(2, "李四", 80));
list.add(new User(3, "王五", 70));
view.addObject("list", list);
return view;
}
}
user
package cn.liuhao.jsp.pojo;
/**
*
* user 实体类
*
* @author admin
*
*/
public class User {
private int id;
private String name;
private double score;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public User(int id, String name, double score) {
super();
this.id = id;
this.name = name;
this.score = score;
}
public User() {
super();
}
}
App startup class
package cn.liuhao.jsp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
jsp page
note
1. When using jstl expressions, you must import the package <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
jsp should be placed in the servletContext directory, in maven The corresponding directory in is src/main/webapp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>hello world</h1>
<table border="1" align="center" width="50%">
<tr>
<td>id</td>
<td>name</td>
<td>score</td>
</tr>
<c:forEach items="${list }" var="i" >
<tr>
<td>${i.id}</td>
<td>${i.name}</td>
<td>${i.score}</td>
</tr>
</c:forEach>
</table>
</body>
</html>