Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to add multiple objects to ModelAndView?

 
Bartender
Posts: 1752
17
  • Mark post as helpful
  • send pies
  • 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: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • 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: 1752
17
  • Mark post as helpful
  • send pies
  • 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: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • 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: 1752
17
  • Mark post as helpful
  • send pies
  • 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
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic