Jsp displays the detailed introduction of objects passed by springmvc modelmap, springmvcmodelmap
Jsp displays the objects passed by springmvc modelmap
Recently, I was working on a small website and decided to build it with springmvc.
When the controller transmits a value to the frontend, for example, using ModelMap to transmit a string, modelmap. addattribute ("msg", "hello"), you can directly use $ {msg} On the jsp side. Then, if I pass an object, you can still use the $ {obj. name} method to display the attributes of the object. However, in more cases, the List needs to be displayed, So I passed a List <User> object, but it was a little too hard to parse and I don't know how to traverse it.
After searching for a few days, you can still use the jstl tag, which is the same as the list of objects passed by the parsing servlet. The specific solution is as follows:
Controller. java
@RequestMapping(value = "/getUsers", method = RequestMethod.GET)public String getUsers(ModelMap model) { List<UserEntity> userEntityList = userService.getAllUser(); for (UserEntity user:userEntityList) { System.out.println(Util.toJsonString(user)); } model.addAttribute("userlist", userEntityList); return "userList";}
UserList. jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ page contentType="text/html;charset=UTF-8" language="java" %>
Use the forEach tag's items to specify the list as the object passed by the backend, and then you can traverse it directly.
Perception: we still need to learn more about associations, even if springmvc uses many different methods, such as classes like ModelMap to transmit objects, however, in jsp, it can still be processed in a similar way.
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!