获取存储在jsp页面的LinkedHashMap中的元素

时间:2014-07-03 14:51:14

标签: java spring jsp spring-mvc jstl

如何在弹簧控制器的ModelAndView对象中添加的jsp页面中获取LinkedHashMap。请参阅以下代码:

UserDTO:

public class UserDTO{

    public byte[] data;
    public String user;
    public Long id;

    public byte[] getData() {
        return data;
    }
    public void setData(byte[] data) {
        this.data = data;
    }
    public String getUser() {
        return user;
    }
    public void setUser(String user) {
        this.user = user;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
}

控制器:

@RequestMapping(value = "/showData", method = RequestMethod.GET)
    public String showRequest(final HttpServletRequest request,
             HttpServletResponse response, ModelMap model,
             @RequestParam("ID") String Id) {
         MyDTO myRequestDTO = new MyDTO();
        String viewName = "myData";
        final ModelAndView mav = new ModelAndView();

    LinkedHashMap<Long,UserDTO> myList = myRequestsDTO.getAllRecords();
        /*  UserDTO userDoc = null;
            Long userDocKey;
            if(!myList.isEmpty()){
            for (Map.Entry<Long,UserDTO> doc : myList.entrySet()) {     
                  userDoc = doc.getValue();
                  userDocKey = doc.getKey();
                System.out.println("User  = " + userDoc.getUser() + " User Name = " + userDoc.getData());

            } } */
mav.addObject("myList", myList);//want to get this myList in jsp page and get the details from that.
return viewName;
}

请建议如何将LinkedHashMap存储在ModelAndView对象中并获取&#34; userDoc.getUser()&#34;,&#34; userDoc.getId()&#34;和&#34; userDoc.getData()&#34;从我在上面的控制器代码中显示的那张地图。我知道这可以通过使用JSTL实现,但作为我的新手,请让我知道对我有用的解决方案。

1 个答案:

答案 0 :(得分:2)

您可以使用foreach在JSTL上迭代地图。

<c:forEach var="entry" items="${myList}">
  Key  : <c:out value="${entry.key}"/>

  User : <c:out value="${entry.value.user}"/>
  Id   : <c:out value="${entry.value.id}"/>
  Data : <c:out value="${entry.value.data}"/>

</c:forEach>