• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

traversing map unsing taglib

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Hashmap in the classscope. I wanna traverse through the key part which has ids, and find a id that i am interested in and get the value for that id from the value part.

i wanna do this in the jsp.

is there any taglib to do this? can someone helpme?

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have no idea what you mean by "in the classscope", but I'm sure that the EL and JSTL can handle what you are after. Once we figure out what that is.
 
babu mohan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't worry about the class scope, i need to traverse through the map using JSTL.

My aim is to pass a id to the map and retireve the corresponding value for that from the map and through the JSTL.

can anyone help me with this?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you already know the key, no traversal is necessary. Not even any JSTL.

If you know the key, you can retrieve the value by treating it as a simple property. For example, if the map is in scoped variable abc, and the key is xyz, you can use either of:

${abc.xyz}

or

${abc['xyz']}

which are equivalent.

If the key is not to be hard-coded and instead is in another scoped variable, let's say mno, the reference would be:

${abc[mno]}
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm trying to do something very similar. I have created a java.util.Map in a struts action class and saved it in the request scope like so:

request.setAttribute("map", myMap);

Now I would like to access this map in a java script located in my JSP. I can retrieve values by hardcoding the key like this:

testVar.value = "<bean:write name="map" property="test" />";

(key "test" is mapped to value "test successful")

However, I don't want to hardcode the key. I would like to do something like:

function myScript(field) {
var myKey = field.value;
testVar.value = "<bean:write name="map" property=myKey />";
}

There are obvious problems with trying to do this, but I'm having a hard time finding the solution. Any help would be greatly appreciated, and please let me know if more details are required.


-Brock
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brock, welcome to the Ranch.

What you have done here is what is known as a thread hi-jack. Even though your question may be similar, it is not the same. The OP is using the EL, you are using Struts tags.

Am I correct in that you want to do this with proprietary Struts tags rather than the standardized EL?
 
Brock Yates
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not 100% sure as I'm new to JSP and a little out of my element. I thought that since my hashmap is located in a struts action class I would have to use struts tags to retrieve it in the JSP.

I'm certainly not opposed to using EL-tags if its a more suitable solution.

Sorry about the hi-jack.


-Brock
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Brock Yates:
Sorry about the hi-jack.



No prob, it just makes it difficult to handle different issues differntly when they're ganged up in the same topic.

I thought that since my hashmap is located in a struts action class I would have to use struts tags to retrieve it in the JSP.



Not so. Scoped variables are available to the JSP in the same way regardless of how they are set by the server-side code.

I'm certainly not opposed to using EL-tags if its a more suitable solution.



There's the rub. It's not an issue whether you are using Struts actions or not, but it might be an issue about how things play together on the JSP. Mixing Struts and non-Struts might have consequences.


I'm not 100% sure as I'm new to JSP and a little out of my element.



If you are new to JSP, then (in my opinion, of course) you should not be using Struts until you have a much better idea about how Servlets and JSP work. At which point you are in a much better position to use a framework, or even decide if you need to use one at all. Assuming you have a choice, of course.
 
I've been selected to go to the moon! All thanks to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic