Jsp displays the detailed introduction of objects passed by springmvc modelmap, springmvcmodelmap

Source: Internet
Author: User

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!

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.