• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

want to access an arraylist of jsp in javascript

 
amit daundkar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
i am getting an arraylist in jsp.(scriptlet). i want to access the same arraylist object( reference) in a javascript function to give ajax calls through dwr.
please guide me how i can do this as we can do with strings.

please reply.

thank you.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amit daundkar wrote: i want to access the same arraylist object( reference) in a javascript function to give ajax calls through dwr.

Simple you can't, Its not possible to pass an Java run time object to JavaScript, You have to set those arraylist values to some hidden input element and then access those in JS OR create arraylist in JS itself.
 
amit daundkar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar Rohankar wrote:
amit daundkar wrote: i want to access the same arraylist object( reference) in a javascript function to give ajax calls through dwr.

Simple you can't, Its not possible to pass an Java run time object to JavaScript, You have to set those arraylist values to some hidden input element and then access those in JS OR create arraylist in JS itself.


offcourse sagar.thats how i do it normally.
that works fine with Strings but not withcollection object.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amit daundkar wrote:
that works fine with Strings but not withcollection object.

Strings are universal, they are single entity that has not any complex structure and nay script can understand it, thus we can pass strings values to JS, but not the reference of a same.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just let JSP print it out as a JS variable. This way JS can access it when it is about to run.
 
amit daundkar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bauke Scholtz wrote:Just let JSP print it out as a JS variable. This way JS can access it when it is about to run.


i did not get it bauke. i mean i didnt understand by printing it as a JS variable.
please explain.
thhank you.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, what Bauke suggest is something like this,



And in JS



Correct me here, If I were wrong
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I didn't mean that.

You know, JSP runs at the server machine and produces HTML/CSS/JS output.

Just let JSP print it out as a JS variable. Literally.


If SomeBean#getSomeProperty() returns for instance "foo", this would produce

which is perfectly valid JS after all when sent to the client side and running over there.

Using hidden input elements is whacky and plain cumbersome.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bauke Scholtz wrote:
Using hidden input elements is whacky and plain cumbersome.

Thanks buddy, I didn't know that we can do something simple as that, what I used to do(very rarely) is something like I posted previously
 
amit daundkar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bauke Scholtz wrote:No, I didn't mean that.

You know, JSP runs at the server machine and produces HTML/CSS/JS output.

Just let JSP print it out as a JS variable. Literally.


If SomeBean#getSomeProperty() returns for instance "foo", this would produce

which is perfectly valid JS after all when sent to the client side and running over there.

Using hidden input elements is whacky and plain cumbersome.


hey Bauke ,
thank you buddy.
but i can do it easily with a DWR call and asign a JS variable to a bean or a collection. and i have done it quite a few times. what i want to ask is,
i am getting the ArrayList in the scriptlet part of a JSP and not in a JS function. but for some reason i want to access this ArrayList in a JS function.
thats what i want to do.

i can get the same list in JS function with a DWR call but i dont want to do it as i need the list in the scriptlet part of JSP.
i hope you understand what i mean.
please reply.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer remains the same.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic