【问题标题】:get elements stored in LinkedHashMap in jsp page在jsp页面中获取存储在LinkedHashMap中的元素
【发布时间】:2014-07-03 14:51:13
【问题描述】:

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

用户DTO:

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;
}

请建议如何获取存储在 ModelAndView 对象中的 LinkedHashMap 并从该映射中获取“userDoc.getUser()”、“userDoc.getId()”和“userDoc.getData()”,如我在上面的控制器代码中所示.我知道这可以使用 JSTL 来实现,但由于我是新手,请告诉我对我很有帮助的解决方案。

【问题讨论】:

标签: java spring jsp spring-mvc jstl


【解决方案1】:

您可以使用 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>

【讨论】:

  • 不,我现在试过了,即使列表包含 @obourgain 的值,它也没有进入
  • @user3684675 对 jsp 中返回的列表大小进行试金石测试:。让我们知道你得到了什么。
  • 它向我显示 jsp 页面上的错误,fn:length 未定义 @Prasad。
  • 在你的jsp中包含这个:java.sun.com/jsp/jstl/functions" %>
  • @user3684675 你需要试试:...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-15
  • 1970-01-01
  • 2013-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多