Announcement Announcement Module
Collapse
No announcement yet.
How to add multiple objects into the ModelAndView from Controller (Spring 3) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to add multiple objects into the ModelAndView from Controller (Spring 3)

    In my controller, I have a simple

    mav.addObject("users", userService.loadUsers());

    (The loadUsers() method returns a Set.)

    ----

    In my listUsers.jsp page, I have JSTL like this (to try to set up a combo with a list of user names):

    <select>
    <c:forEach var="usr" items="${users}" >
    <option value="<c:out value="${usr.firstname}"/>">
    <c:out value="${usr.firstname}" /></option>
    </c:forEach>
    </select>

    However, I don't seem to be able to access any of these values put there in the Controller. I did check in the Controller and the values are indeed populated as a Set there.

    Also, I added two objects to in the controller so I'm not sure if this is a problem:

    mav.addObject("obj", userList);
    mav.addObject("obj", someOtherList);

    ???

    Any suggestions about why I'm not able to access the "users" object in the JSP would be highly welcomed!

    L

  • #2
    First you put them in with the name 'obj' and you seem to add them both with the same name so the second overrides the first that isn't going to work...

    Also you are adding things to the ModelAndView but are you also returning that from your controller method? Next to that make sure EL is enabled (use a correct version for the web.xml else it isn't enabled by default).

    And please next time use [ code][/code ] tags when posting that way it remains readable....

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      First you put them in with the name 'obj' and you seem to add them both with the same name so the second overrides the first that isn't going to work...

      Also you are adding things to the ModelAndView but are you also returning that from your controller method? Next to that make sure EL is enabled (use a correct version for the web.xml else it isn't enabled by default).

      And please next time use [ code][/code ] tags when posting that way it remains readable....

      In the JSP, one of the Map objects for the domain "Users" is correctly displaying user information back from the Controller, so EL is enabled.

      Then, in the actual MAV, I'm adding the object like this:

      Code:
          mav.addObject("obj", userList);  
          mav.addObject("depts", someOtherList);
      But, in the JSP, I cannot seem to access the "depts" in a select statement like this:

      Code:
      <select>
      <c:forEach var="usr" items="${depts}" >
      <option value="<c:out value="${usr.departmentName}"/>">
      <c:out value="${usr.departmentName}" /></option>
      </c:forEach>
      </select>
      The "depts" is a Set of Department objects which is different from the domain object Users that the Controller code is running in.

      I'm not sure if this is part of the problem -- that you (maybe?) need to have a field in the domain object for anything put into the MAV maps objects.

      Hope this clarifies my initial posting.

      Look forward to your follow up.

      Thanks

      L

      Comment

      Working...
      X