Results 1 to 3 of 3

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

  1. #1
    Join Date
    Jun 2011
    Posts
    21

    Post 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    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....
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jun 2011
    Posts
    21

    Default

    Quote 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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •