aspose file tools*
The moose likes JSP and the fly likes traversing map unsing taglib Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "traversing map unsing taglib" Watch "traversing map unsing taglib" New topic
Author

traversing map unsing taglib

babu mohan
Greenhorn

Joined: Sep 29, 2005
Posts: 7
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?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
babu mohan
Greenhorn

Joined: Sep 29, 2005
Posts: 7
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

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]}
Brock Yates
Greenhorn

Joined: Mar 06, 2007
Posts: 2
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

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

Joined: Mar 06, 2007
Posts: 2
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

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 agree. Here's the link: http://aspose.com/file-tools
 
subject: traversing map unsing taglib