Objects passed by Springmvc Modelmap are displayed in the "Modelmap" JSP

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.