Recently in doing a small website, the function is very basic, decided to build with SPRINGMVC.
Encountered a problem, when the controller to the front-end value, such as using Modelmap to pass a string, Modelmap.addattribute ("msg", "Hello"), then on the JSP side, directly using ${msg} can be displayed. Then, if I pass an object, I can still use a method like ${obj.name} to display the properties of the object. However, in more cases, it is necessary to display the list, so I passed a List<user> object, but I was a bit confused when parsing, do not know how to traverse.
Search for a long while to know, the original can also use the JSTL tag, with the previous parsing servlet passed the same list of objects to handle. The specific treatment 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" %><HTML><Head> <title>User List</title></Head><Body> <H1>Hello</H1> <BR> <C:foreachItems= "${userlist}"var= "Item" >userId:<C:outvalue= "${item.id}"/> <BR>Username:<C:outvalue= "${item.username}"/> <BR> </C:foreach></Body></HTML>
The items in the Foreach tab specify the objects passed by the backend, and then they can be traversed directly.
Sentiment: Still want to learn more Lenovo, even if springmvc use a lot of different methods, such as Modelmap class to pass objects, but in the JSP display or can be similar to the previous processing methods to deal with.
Objects passed by Springmvc Modelmap are displayed in the "Modelmap" JSP