Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

How to pass Object to Javascript function in JSP  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to dynamically populate a div in my jsp.

I've a list of objects (which is nothing but a Map<String, Object>)

The following code works:



But I need to pass the object as it is so that I've better control of what should be inserted in the 'panel_info' div.

Hence I need to send myObject to doSomething() somthing like :



But when I do this is displays : undefined.

Please guide me on how i can pass it as an object.

Please help.
 
Marshal
Posts: 67191
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the HTML that is sent to the browser, is it what you expect? Does it look right?

Remember that the JSP is executing on the server to create the HTML page to be sent to the browser. So Java objects can never be passed to JavaScript.

Maybe this article will be helpful.
 
Tony Ghy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Look at the HTML that is sent to the browser, is it what you expect? Does it look right?

Remember that the JSP is executing on the server to create the HTML page to be sent to the browser. So Java objects can never be passed to JavaScript.

Maybe this article will be helpful.



Oh yeah...silly of me. I got your point.

So the only solution would be to pass some ID to the Javascript and then do an Ajax call to fetch the required data without reloading the page.

Okies...my motive is solved. Thanks for the direction. Have a nice day!!
 
Bear Bibeault
Marshal
Posts: 67191
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, use the JSP to create a JavaScript object with the same properties. JSON is an easy way to handle that,
 
Tony Ghy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Or, use the JSP to create a JavaScript object with the same properties. JSON is an easy way to handle that,



Yes,I've used JSON to fetch some of the data in my jsp. (e.g. I've used JSON to hold the city names list for a particular statename)

But for this case, I don't want to fetch all the data in some JSON at a go. If the user clicks on the 'more details' link, it should fetch the data...so Ajax seems better in this case. What say?
 
Bear Bibeault
Marshal
Posts: 67191
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes, if it's the result of an event, it's probably better to fetch the data as it is needed.
 
Yes, my master! Here is the tiny ad you asked for:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!