0

How to get LinkedHashMap in the jsp page which is added in ModelAndView object in the spring controller.Please see the below code:

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

Controller:

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

Please suggest how to get the LinkedHashMap stored in ModelAndView object and get "userDoc.getUser() ", "userDoc.getId()" and "userDoc.getData()" from that map as i have shown in above controller code. I know that this can be achieved using JSTL but as i'm new to it please let me know the solution which is much helpful to me.

user3684675
  • 381
  • 4
  • 8
  • 32
  • 1
    http://stackoverflow.com/questions/924451/el-access-a-map-value-by-integer-key – evanwong Jul 03 '14 at 14:55
  • 1
    Add list into [ModelMap`](http://docs.spring.io/spring/docs/2.5.6/api/org/springframework/ui/ModelMap.html) that is passed in arguments. `model.addObject("myList", myList);` or add it into [Model](http://docs.spring.io/spring/docs/2.5.x/api/org/springframework/ui/Model.html) that is also passed as method arguments. `model.addAttribute("myList", myList);` – Braj Jul 03 '14 at 16:52
  • 1
    model.addAttribute("myList", myList); worked whereas model.addObject("myList", myList); also worked but its an deprecated method. Thanks @Braj . But just curious why not it worked with mav.addObject("myList", myList); . – user3684675 Jul 03 '14 at 17:03
  • 1
    because `mav` is a local variable that is never controlled by spring. Only Model and ModelMap that is passed as method arguments are inspected by spring. Or you can return `ModelAndView` object as method return type in that case it will work. Just set the view name in `ModelAndView` along with list. – Braj Jul 03 '14 at 17:06
  • You're welcome. Try last option as well with ModelAndView. Just call [ModelAndView#setViewName()](http://docs.spring.io/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/ModelAndView.html#setViewName%28java.lang.String%29) and add the object and return it. – Braj Jul 03 '14 at 17:08

1 Answers1

2

You can iterate over your map with JSTL using foreach.

<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
  • 8,856
  • 6
  • 42
  • 57