How to get json in response body in jsp
To answer a netizen's doubts, in order to speed up, the database is not connected.
A friend asked: How does jsp get the json data in the response body of the Controller response?
Project structure
Add webapp to springboot project
There is this tutorial online. If you don’t know how, you can click this hyperlink.
maven dependency import
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--jsp页面使用jstl标签-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--用于编译jsp-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
Let’s look at the main points first
JSP sample template
The problem of Chinese garbled characters has been solved
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<title>例子</title>
</head>
<body>
<div>
<table>
<tr>
<td>姓名</td>
<td>性别</td>
</tr>
<%-- 要遍历的 --%>
<c:forEach items="${userlist}" var="user">
<tr>
<td>${user.userName}</td>
<td>${user.sex}</td>
</tr>
</c:forEach>
</table>
<%-- 如果是单个对象的 --%>
<%-- <td>${user.userName}</td>--%>
<%-- <td>${user.sex}</td> --%>
</div>
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<title>例子2</title>
</head>
<body>
<div>
<table>
<tr>
<td>姓名</td>
</tr>
<tr>
<%--下面两个都行--%>
<%--<td>${userName}</td>--%>
<td>${sessionScope.userName}</td>
</tr>
</table>
</div>
</body>
</html>
control layer
import com.example.demo3jsp.domain.User;
import com.example.demo3jsp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
public class userController {
@Autowired
UserService userService;
// 用Model传值的
@RequestMapping(value = "userlist")
public String userlist(Model model) {
List<User> userlist = userService.findAllUser();
model.addAttribute("userlist", userlist);
return "user";
}
// 用Session传值的
@RequestMapping(value = "userName")
public String userList1(HttpSession httpSession){
String userName = userServiceImpl.findUserName();
httpSession.setAttribute("userName", userName);
return "user2";
}
}
Result graph
Domain layer
@Data
@AllArgsConstructor
public class User {
private String userName;
private String sex;
}
Service layer
public interface UserService {
List<User> findAllUser();
String findUserName();
}
@Service
public class UserServiceImpl implements UserService {
// 模仿下数据库访问
@Override
public List<User> findAllUser() {
User user1 = new User("zhangsan", "男");
User user2 = new User("lisi", "女");
List<User> users = new ArrayList<>();
users.add(user1);
users.add(user2);
return users;
}
@Override
public String findUserName() {
// dosomething,其实应该是登录之类的,返回一个用户名,这里将就一下
return "zhangsan";
}
}
If there are any mistakes, please let me know!
When reprinting or quoting the content of this article, please indicate the source and original author: Juzu Qingzhong;