• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Javascript and struts

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,

The problem I have touches both scripting and struts. Since I though it was most closely related to scripting I have put it in this forum but please move the thread if this is not the right forum.



The alerted output is


How can I substring a to get the key 1 and 5. I have tried a[0] and other options. I guess I need someone to point me to the right API.


 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you change the data in colorList?

If you can change it to look like this:



and change your code to look like this:



You can do this



Eric
 
Jay Abrahm
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I understood where you are heading. Are you thinking about pushing it into a JSON.

The problem there is that the data in the colorList is a linkedHashMap. Unfortunately design prevents me from changing the Map.

 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And you can not write a method that converts it into another format?

Eric
 
Jay Abrahm
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The display in the form a = {1=blue, 5=Green} is done by javascript when it processes the Map.

My use case needs it to be a Map since it is used across screens but I guess I can process it at the server side and have an additional request variable with the same data in JSON format.

Basically my question would be - if javascript is able to process the HashMap and alert it properly. Wouldn't there be an API to access it.
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would have to write a function that takes the string and splits it up.



It would be a lot more efficient if you do it on the server if the hash map is large.

Eric
 
Jay Abrahm
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Eric,

Thanks for replying, yep that did work. Also, I got it to work from the server side...

Sorry for being persistent but I just cannot find an explanation from anywhere on how javascript was able to convert the collection into a var. Again, sorry for repeating myself - If javascript can do that so wouldnt it have some way to access the complex object that it has created.

Anyways, Eric, thanks for the help...
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JavaScript does not know what a hashmap is. It is like me trying to read a book in German. I know there is a bunch of text there, I have no clue what it says until I get it translated.

Eric
 
Jay Abrahm
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eric Pascarello wrote:JavaScript does not know what a hashmap is



How was it able to 'translate' it and get a = {1=blue, 5=Green}. It should have just failed but javascript realized that the collection contents needs to be processed and it did process to give a set instead of undefined or object or something else.

Maybe I am pushing it without understanding the core of what javascript does...


 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem is that {1=blue, 5=Green} is not a valid object syntax, hence it has no clue what to do with it. When I read the German book, I am undefined too.

If you were to run this
var a = {1=blue, 5=Green}

it should give you the error SyntaxError: missing : after property id

Proper JSON notation is
var a = {"1":"blue", "5":"Green"}
which JS knows how to process it.


If you return it as
var a = '{1=blue, 5=Green}';
It is a plain old string and plain old strings are just that.

So you had two choices, you either make it work from the server spitting out JSON, or you hire an interpreter on the client [that function I wrote] that converts a string over. You need to remember that you are trying to make Apple Juice from an Orange. It is impossible to make JavaScript do something that Java understands. I wish my wife would understand that it is impossible for me to do the dishes, but she insists I make Apple juice from Oranges!

Eric
 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally I am a fan of doing things like this server side, especially since the JSON api is actually very good (granted it's not doing anything especially complicated) but still it is good.

Are there actually any advantages doing it one way over another?
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eric Pascarello wrote:You would have to write a function that takes the string and splits it up.




It would be a lot more efficient if you do it on the server if the hash map is large.

Eric



Dear Eric,

Your code can be simplified using an inline replace() method as:
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did not feel like messing with reg exp when I wrote it.

Eric
 
Cloudgen C.F. Wong
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eric Pascarello wrote:I did not feel like messing with reg exp when I wrote it.

Eric



Yes, Regular Expression make code less "readability". I wonder which code will run faster, using split or using regex, any clue? Maybe I do the benchmark test later.
 
Jay Abrahm
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow !! I didnt check my mails for two days and look what happened...

Thanks Eric for helping me understand.

Since the Map was pretty huge, I moved the translation to the server and transported it as a JSON. Taking a quick look at it I though it was quicker but I haven't actually generated any stats to check if there really was any performance improvement.
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JavaScript is normally slow with large loops so it is always better to do it on the server that is built to handle that.

Eric
 
reply
    Bookmark Topic Watch Topic
  • New Topic