jsp 显示springmvc modelmap传递的对象详细介绍 jsp 显示springmvc modelmap传递的对象 最近在做一个小网站,功能非常基础,决定用springmvc搭建。 遇到一个问题,在controller向前端传值时,比如使用ModelMap传了一个字符串,modelmap.addattribute("msg", "hello"),那么在jsp端,直接使用${msg}就可以显示。接着,如果我传递了一个对象,依然可以使用${obj.name}这样的方法来显示该对象的各个属性。然而更多情况下,都需要显示列表,所以我传递了一个List对象,但是在解析时有点懵逼了,不知道怎么遍历。 搜了半天才知道,原来还可以使用jstl标签,跟以前解析servlet传递的对象列表一样的来处理。具体处理方法如下: controller.java @RequestMapping(value = "/getUsers", method = RequestMethod.GET) public String getUsers(ModelMap model) { List 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" %> user list

hello


userId:
username:
通过forEach标签的items指定列表为后端传递的对象,然后就可以直接遍历了。 感悟:还是要多学会联想,即使springmvc使用了很多不一样的方法,比如ModelMap这样的类来传递对象,但是在jsp中展示还是可以类似以前的处理方式来处理。 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!