aspose file tools*
The moose likes Struts and the fly likes Accessing HashMap value in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Accessing HashMap value in JSP" Watch "Accessing HashMap value in JSP" New topic
Author

Accessing HashMap value in JSP

Madhav Kumar
Greenhorn

Joined: May 31, 2011
Posts: 12
Hi,

I've map set on the requestAttribute. I want to get the value from the map based on the key provided by the user(option).

I tried many things, But its not working out for me..

Thanks in Advance
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

Madhav Kumar wrote: I want to get the value from the map based on the key provided by the user(option).
I tried many things, But its not working out for me..
Thanks in Advance


are you using any frameworks ? what all things did you try ? how does your hashmap look <String,String> <Long,String> <String,Object>?

give some code... there can be lot of answers...


Time is what we want the most, but what we use the worst. -- William Penn
Madhav Kumar
Greenhorn

Joined: May 31, 2011
Posts: 12
Hi John,
Yes, I'm using Struts1 framework. My part of Action class look like


In JSP, populating the keys to dropdown using below code,



these things works perfectly. I'm trying to get the value of key(option selected by user) in JavaScript to update the textbox dynamically. Here im struggling.

Thanks,
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

why dont you set the map value to the "value" attribute of the options tag and onchange pass the value? and if you dont want to refresh the page then go for some Ajax library...
Madhav Kumar
Greenhorn

Joined: May 31, 2011
Posts: 12
It seems <htmlptions> tag doesn't have value attribute. And, now I'm able to get the key(option) selected by user in javascript through onChange method using below code

I tried below code to access the map, got the value in alert {product 1=001, product 2=002}. But have no idea how to get value from it using the key(ss).




Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

I oversighted it as optionsCollections.. anyways..

if you have the key now, I think the below should work..



I didn't test it.. let me know if it worked
Madhav Kumar
Greenhorn

Joined: May 31, 2011
Posts: 12
That works partially dude .. The issue is, Its working when I hard code like this

var product = '<c:out value="${productDetailsMap['Product 1']}" />';

But, When I go for dynamic key, some thing like this

var product = '<c:out value="${productDetailsMap['"+ss+"']}" />';

I'm getting exception

Unable to create an xml attribute from name [+ss+] value [']}]

Is there anyway that I can append key dynamically???
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

You can't use a string expression which results in an EL expression and then have that EL expression evaluated, if that's what you meant by "dynamically". But I think in your case you don't have to. Try this:

That's assuming that the ss variable is in some scope.
Madhav Kumar
Greenhorn

Joined: May 31, 2011
Posts: 12
Actually ss variable is created to get the key( user selected option) using below code


So, When I try the above code you provided, I'm getting empty value
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

But that's Javascript code, isn't it? Recall I said "That's assuming that the ss variable is in some scope"? You certainly can't expect Javascript variables to be of any use at all, since they don't even get assigned a value until the browser executes them.
Madhav Kumar
Greenhorn

Joined: May 31, 2011
Posts: 12
Yep, I got the point John.

Is there something that I can bring the js variable ss in to the EL expression. If so, could you please post the sample code...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

Madhav Kumar wrote:Yep, I got the point John.


I don't think you did. (And I'm not "John" either.)

The JSP runs on the server. It generates HTML, including Javascript, and sends the result off to another computer, where it will be executed at a later time. To imagine that there is some way that executing code on a different computer, after the JSP has finished, can somehow affect what that JSP had already generated earlier, is surely nonsensical.

But you surely don't believe that running a computer program can affect the result of something which has already taken place. Which means that you didn't get the point. Whatever it is you're trying to do with this HashMap, you're going to have to do it on the server if you want it to affect what the server produces.
Madhav Kumar
Greenhorn

Joined: May 31, 2011
Posts: 12
Thanks for given the clear idea on this.

You are right, Certainly in JS we couldn't inset the value of ss in EL expression which is already constructed.

I guess using <cut> I could not find the solution so, I'm going for another way. I've HashMap in requestAttribute and got it in a variable using below code.

which alerted me like this.. product1={key1=value1, key2=value2}
So, I' m trying to get the value from HashMap using JS iteself. Please let me know If there is something I could do for this.

Once again thanks Paul, hope this time I got it clearly
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

Madhav Kumar wrote:It seems <htmlptions> tag doesn't have value attribute. And, now I'm able to get the key(option) selected by user in javascript through onChange method using below code

I tried below code to access the map, got the value in alert {product 1=001, product 2=002}. But have no idea how to get value from it using the key(ss).



but you said already that you are getting the key (ss) and product map in the above post in javascript ?? didn't you ? so we were telling you how to get the value for that key.. Paul has given you the exact code that you should use to get the user selected option's value.

and again I dont know if its the right idea to do all these things in jsp...


Madhav Kumar
Greenhorn

Joined: May 31, 2011
Posts: 12
John,

My req is, There is Id & Name pair. On the page, Id has to be listed in dropdown and based on the user selection value has to be populate in a read-only text box.

So, What I did, In java side have put Id&Name pair in Map and passed to JSP.

Answer for your ques : whenever user changes the dropdown value, I'm getting the option in ss variable using onChange method. So as Paul said, I could not use ss in <cut>.

Anyway I've found the soln using below code


But It would be great, If I can get the value directly from map instead of Iterating it.
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

getting it directly...

aah.. that is impossible since you are trying to access a different thing altogether..

anyways... you can remove the <cut> tags from the above code.. you don't need it..

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing HashMap value in JSP