• 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

Accessing HashMap value in JSP

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Madhav Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems <html:options> 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
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 <c:out> 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
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Madhav Kumar wrote:It seems <html:options> 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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 <c:out>.

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
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getting it directly...

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

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

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic