• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

How to add multiple objects to ModelAndView?

 
Bartender
Posts: 1938
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
KIf I want to add multiple objects, say, with different Lists to display in the corresponding JSP, do I need to add the subsequent objects to the same MAV object like this:

Method 1:


OR like this:

Method 2:



???

I'm having the problem where I cannot access the second object back in the JSP I'm adding to the MAV using either of the two methods above.

An additional question -- Does each object added to the MAV also have to be defined in the domain class?

Thanks in advance for any suggestions.

- mike
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ModelAndView object is basically a Map. What is the semantics of a Map when you add two objects with the same key? That is what you are doing in your first example. You are using the same key twice. Only one object will be in the Map.

Mark
 
Mike London
Bartender
Posts: 1938
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Spritzler wrote:The ModelAndView object is basically a Map. What is the semantics of a Map when you add two objects with the same key? That is what you are doing in your first example. You are using the same key twice. Only one object will be in the Map.

Mark



Thanks Mark,

Actually, I am doing it the second way where there are two object names, but I am not able to see the "otherobj" object in the JSP.



I posted a related message to this here on the forum in the last few days.

Is there another way to access this object in the JSP directly ("${PageContext..., or something like that)?

-m
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should just be ${otherobj.someProperty}

Have done that many times before and it works. Are you positive that someOtherList is not null?

Mark
 
Mike London
Bartender
Posts: 1938
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Spritzler wrote:It should just be ${otherobj.someProperty}

Have done that many times before and it works. Are you positive that someOtherList is not null?

Mark



Thanks Mark.

I was just going to post back. The problem turned out to be I had the code in the wrong @RequestMapping method. It was confusing since my test code seemed to output to the console from that method.

It's working fine now.

Thanks very much for your replies.

- m
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic