File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes How to pass JSTL value to javascript function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to pass JSTL value to javascript function" Watch "How to pass JSTL value to javascript function" New topic
Author

How to pass JSTL value to javascript function

saravanan sambandam
Ranch Hand

Joined: Aug 30, 2010
Posts: 42
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.

Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

Please read: EaseUp.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Marcin Kwiatkowski
Ranch Hand

Joined: Aug 06, 2007
Posts: 32

It should be something like this:



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

saravanan sambandam
Ranch Hand

Joined: Aug 30, 2010
Posts: 42
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.

Steve Warren
Greenhorn

Joined: Aug 26, 2011
Posts: 2
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

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

Joined: Aug 26, 2011
Posts: 2
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to pass JSTL value to javascript function