• 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

How to pass JSTL value to javascript function

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

I am newbie in struts and also JSTL.

I would like to avoid java codes in my jsp. So, how i have to write JSTL code for the give java codes



My requirement is when i type word on the text box this will bring matching words under neath of the text box and more less auto complete.

It urgent to my application. Your suggestion and comments are welcomes

Thanks
Saran.

 
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there, please do not add color tags and size tags (and not other tags) inside your code, it only messes up everything.

Check this:

You are setting to names the name of the current element, imagine a list with three elements [A, B, C], when it finishes your variable 'names' will have value C, why do you append it in Java code and you don't do the same in JSTL?

Also "urgency" is not really taken in account in this forum.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read: EaseUp.
 
Ranch Hand
Posts: 32
Eclipse IDE Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should be something like this:



Then from JavaScript code you can access it simply using EL:

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

I followed as it is what you said. But i did not get the result. I think i made the mistakes while wrote the code in <html:form> itself.

I don't know where should i write the code.

Code should be in javascript itself?

please give me idea.

Here i posted my entire code.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I pass a JSTL object to JavaScript? I see how to pass simple values, but when I try to pass the object so that JavaScript can do something with the object's fields, it gets a syntax error trying to handle the string version of the object.

So I have an Employee object that I want to pass to a JavaScript function and do something with it (clearly this is a bare-bones test, not a useful app).

When I click the checkbox the action fails with"
Employee is not defined
[Break On This Error] testObject(Employee[id = 1, fi... John, lastName = Doe, zip = 12345]);


Depending on what I have in the Employee.toString, I have also seen:
missing ] in index expression

Clearly the function is receiving emp.toString, not emp, the Employee object. How can you instead pass the object to the JS function?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The object is on the server and only exists while the JSP is used to create the HTML page that is sent to the browser. You can see what's being sent to the browser, where JavaScript is evaluated, by doing a View Source.

In other words. the question "how can I pass a Java object to a JavaScript function?" is completely impossible. Please read this article to understand what JSP is and how it operates.

If you need the full object on the client, you'll need to recreate it by using the JSP to create the JavaScript markup that recreates the data for the object. The original object itself cannot be "passed" to the client.
 
Steve Warren
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Bear. I appreciate the reminder that JSP is used only for building the page, not scripting its operation. I knew that but lost my way as I got excited about the JSTL magic. I guess I if I really need the whole object, I would need to build a parser, like something that would let me construct a JavaScript object from the Java/JSP toString (but I'm not sure I need it that bad!). Much thanks for setting me straight!
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out JSON. It's a format that is easily digestible in JavaScript and there are lots of libraries to serialize Java objects as JSON.
 
reply
    Bookmark Topic Watch Topic
  • New Topic