Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Display HashMap contents  RSS feed

 
laura mccord
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help displaying my HashMap contents using <c:forEach>.

The problem is the value of a key is a Class Object so I'm unsure what the syntax would be. Usually I can view the content in our of my classes like this:



Any suggestions? If you have any good documentation that you can point me to that would be great also.

Thanks.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as your properties follow the JavaBean standard you are fine.

If you want to retrieve the name property on your resource object, use ${r.name }

This way you naviagte through the object graph. Check out the EL spec too.
 
laura mccord
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this kind of what you're talking about me doing?



Thanks for the suggestion.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need the <cut because EL directly writes to the output.
Other than that that is what I'm talking about.
 
laura mccord
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm.. that didn't work out.

Maybe I'm missing a step.

So I have a class object called Resource that contains the attributes(name, parent, owner, size, contenttype). I pass the HashMap() in which the key is the name of the file and the value is the Resource object of that file. I'm still confused as to how using jstl knows about the Resource object. Normally I would assign the value of
to an object like this: . I would then simply use but I can't do that in jstl (at least I don't think). I guess I'm confused as to how do I tie this all together. I haven't found many example like this when searching.

Thanks,
Laura
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not explaining your situation very well.

But if a Map implementation (it's a map, not a "hash") is established as a scoped variable, its values can be accessed simply by referring to its keys in the same way that you'd refer to bean properties.

So let's say that the Map is established as a scoped variable named myMap, and it had an entry with a key of someKey. It would be referenced simply as:



or, if the key is a non-identify such as some key:



or, if the key is the value of another scoped variable named xyz:



 
laura mccord
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, I'm probably not explaining this correctly. So, I'll try giving you the full story.

I have a class Object called Resource:


I have my method called getContents() that is called from my controller along with listFiles() which calls another method called collectAttributes()


Here is my Controller:


My controller is passing my contents HashMap(String, Resource Object) to my jsp page.

So, I guess my question is how do I access the Resource getName() method or better yet retrieve my Resource object attributes(name, parent, owner, size,contenttype) using jstl. Is this possible, or am I needing to configure my contents hashmap better to allow me to access this information?

Many Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's pretty much covered by my previous response.

If the Map is established as scoped variable myMap, and the Resource instance is an entry in the Map with key myResource, you'd reference the name property of the instance with:


 
laura mccord
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this in my jsp file:


But I'm still receiving an error "Caused by: javax.servlet.ServletException: Unable to find a value for "name" in object of class "java.lang.String" using operator".""

Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That means that whatever is in the Map with the key of key is a String not a Resource.

Check your code to make sure that the Map is being created as you intend.
 
laura mccord
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I guess I don't know how to tell it that it's a Resource type since normally I cast the value of the key to be of type Resource. I don't know Spring well enough to get around this. Though I've read that I shouldn't have to cast the value as an object in EL.

 
laura mccord
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another little test I performed to display the output I received the following: As you can see the value of the content is a Resource type if that helps at all.



The output:

0
roster.txt
directory.domain.Resource@13a848
class java.lang.String


Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
laura mccord wrote:Well, I guess I don't know how to tell it that it's a Resource type since normally I cast the value of the key to be of type Resource. I don't know Spring well enough to get around this. Though I've read that I shouldn't have to cast the value as an object in EL.

There's no casting -- nor is there a need for it.

This message is telling you that the instance is a String. Not a Resource masquerading as a String, or anything else like that.

If you expected the Map entry's valye to be a Resource, then you've got a problem somewhere in your code where it's being set to a String rather than a Resource.

You cannot cast an instance of an object to something that it is not. Find out why your Map is badly formed.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Per your example, it actually sounds like you are confused by the difference between iterating over a Map, and directly accessing its values.

${content.value.name}

should be the name of the resource in the body of the iterator.

Bear in mind that when iterating over a Map, you get the Map.Entry instances -- hence you need the key and value de-references.

When you directly access Map values, you get the value, not a Map.Entry. Hence no need for key and value.

 
laura mccord
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YES! That was it! Geez, I feel like a fool. Thanks so much for the help and understanding.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's OK. Maps are a very very important tool in the world of JSPs and the JSTL/EL, and gaining a good understanding of them will take you far.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!