• 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

Struts2 - User ID and Name - Map

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To my friends out there,

I am using struts2 with jsp pages.

I have a requirement where once a user enters his userid in the form , the next field should be populated with the user's name.

I am planning on having a map in my action class that consists of all the userids and their respective names.
After the user enters the userid, call a javascript function that gets the value for this key from my map and displays it.

How can I access this map in my javascript? Any help is really appreciated !

Thank you !
 
Mathew Geraldo
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello again,

Tried this in my jsp page, where idNameMap is a hashmap set in my action class.

In my action Class;

HashMap<String, String> idNameMap = new HashMap<String, String>();

idNameMap.put("1", "Name 1");
idNameMap.put("2", "Name 2");
idNameMap.put("3", "Name 3");


In my jsp:

<s:textfield name="myId" onchange="document.getElementById('myName').value='%{idNameMap[this.value]}';" />

<s:textfield id="myName" name="myName" disabled="true" />


The above code didn't work but if I hardcode the key, it works.
Can anyone let me know what I am doing wrong?
Hard coded key in my jsp. This works.

<s:textfield name="myId" onchange="document.getElementById('myName').value='%{idNameMap[\"1\"]}';" />

<s:textfield id="myName" name="myName" disabled="true" />
 
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Geraldo,

How can I access this map in my javascript


You cannot access a java object on client side. There is a work-around this, you can store the map on the jsp in a hidden field and then retrieve the value of this field via javascript, that way you will be able to get the map object in the javascript but this is not the recommended way to solve your issue.
A better solution would be to fire an ajax request to an action class on entering the userID, the action class would get the userID and then return the respective name which you can populate on your textfield on ajax success.
 
Mathew Geraldo
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Asif,

Thank you for your response.
As shown in my jsp code snippet, I am able to access the map in my JavaScript.
The code below does display the correct value from the 'idNameMap' in 'myName' element.

<s:textfield name="myId" onchange="document.getElementById('myName').value='%{idNameMap[\"1\"]}';" />

However, it works only if I hard code the key value not if the key value is dynamic.

<s:textfield name="myId" onchange="document.getElementById('myName').value='%{idNameMap[this.value]}';" />

There is. Something I am doing wrong here. Any thoughts ???
 
Marshal
Posts: 28193
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
What that snippet demonstrates is that you are generating Javascript which, when run later in the browser, appears to access the map. However what is actually happening is this:

1. Your JSP accesses the map and inserts the value from the map into the Javascript code. This happens on the server.

2. Your Javascript runs. It doesn't access the map, it just uses the value which the JSP made it have. This happens much later, in the browser, after the JSP has finished.

So if you're working on some other mental model of Javascript in JSP, you are going to fail. Javascript on the client cannot access any data from the server; if the Javascript code needs that data, then the JSP code must access the data and build it into the Javascript. This has implications for your design, as you can surely see.
 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic