This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes pass bean data to javascript function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "pass bean data to javascript function" Watch "pass bean data to javascript function" New topic
Author

pass bean data to javascript function

Keri Mathis
Greenhorn

Joined: Jul 23, 2009
Posts: 15
In my struts application, within my JSP, I need to look up some information if my session user bean is not null. Here is what I have:

My trouble is getting the 'username' value to pass into the javascript function. I have tried the following syntax:
var name = '${user.username}'; > PASSED ${user.username} AS STRING
var name = "${user.username}"; > PASSED ${user.username} AS STRING
var name = ${user.username}; > ERROR IN FIREBUG "missing ; before statement"
var name = ${'user.username'}; > ERROR IN FIREBUG "missing ; before statement"

What's the key to make this work?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You seem to have some misconceptions about the relationship between JSP (server-side) and JavaScript (client side).

JavaScript does not have access to Java objects (barring solutions like DWR). It just doesn't work like that: everything in the JSP is rendered before the page is even sent to the client: the only thing that's sent to the client is the rendered HTML. (That's true even for DWR, which gives the *illusion* of transparent interoperability.)

The key to making it work is defining your problem in such a way that the browser can actually support what you're trying to do. This is not a Struts issue; moving to JSP.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60804
    
  65

While it's true that JavaScript has no direct access to Java, what you are doing should work to create JavaScript markup to send to the browser.

var name = '${user.username}'; > PASSED ${user.username} AS STRING


This tells me that your web app is misconfigured and that the EL is not being properly evaluated.

If the string ${user.username} is being sent the browser as part of the HTML, you need to figure out why your web app is not working correctly.

Did you follow the steps in the JSP FAQ to verify that the web app is properly configured?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Keri Mathis
Greenhorn

Joined: Jul 23, 2009
Posts: 15
Pretty sure everything is set up correctly. The following line is populating correctly, and only displays when !(empty user).

As an alternative, thinking there was an issue specifically with the bean 'user,' I put the following code at the top of my page
and changed the script lines to

But what gets passed is still '${testvar}' as a literal string. I'm using beans and other variables successfully in other parts of my page, but I just can't seem to generate a value to pass to JavaScript. If there is instead a way to call the JavaScript, and pull a data value from that field instead, maybe that's what I should try (a hidden input field or something). But it seems like it should work passing the data.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60804
    
  65

This has nothing to do with calling JavaScript. Nothing. At all.

Do a View Source on the HTML being sent to the browser. If that source still contains the ${whatever} notation, it means that the expression is not being properly evaluated on the server.

What do you see in the View Source?
Keri Mathis
Greenhorn

Joined: Jul 23, 2009
Posts: 15
Yes, you are correct (as you knew). View source shows the ${} notation on that variable line. I understand this is not a JavaScript issue, as it is getting called correctly. I'm specifically trying to get the right data to pass from a bean that is sometimes there, sometimes not. If that notation is correctly resolved everywhere else, albeit within JSTL tags, what is there to try differently in this case?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The steps from the FAQ already linked to?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60804
    
  65

If the EL works with the JSTL and not elsewhere (and you are using a JSP 2 container), then your web app and the JSTL are likely misconfigured.

Here are the things to check:
  • Are you using a JSP 2 container? e.g. Tomcat 5 or 6
  • Is your web.xml declared using the 2.4 or 2.5 XML Schema? Or are you using the old 2.3 DTD?
  • Are you using the appropriate version of the JSTL to match?


  • Details are in the FAQ.
    Keri Mathis
    Greenhorn

    Joined: Jul 23, 2009
    Posts: 15
    Ok, I'll peruse the FAQ in more detail. This existing application is using OC4J, with which I'm not as familiar as I am Tomcat. Thanks for the replies and information.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60804
        
      65

    First things is to check which version of JSP oc4j supports. If it's not JSP 2, all bets are off!
    Harpreet Singh janda
    Ranch Hand

    Joined: Jan 14, 2010
    Posts: 317

    Please try with expression tag (<%= variablenmae %>) instead of el where you want to use the value of your variable.
    Harpreet Singh janda
    Ranch Hand

    Joined: Jan 14, 2010
    Posts: 317

    I have tried something like below:


    You can try the same logic in your code.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60804
        
      65

    Harpreet Singh janda wrote:Please try with expression tag (<%= variablenmae %>) instead of el where you want to use the value of your variable.

    NO! Do not!

    Resorting to technology that's been discredited for over 8 years to deal with a configuration issue is not a good suggestion.
     
    Consider Paul's rocket mass heater.
     
    subject: pass bean data to javascript function
     
    Similar Threads
    JSP does not call the right method in the controller class
    How to pass JSTL value to javascript function
    Implementation of google charts in JSP
    Enumeration is not returning any data
    pagination exception in spring