• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pass bean data to javascript function

 
Keri Mathis
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65122
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Keri Mathis
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65122
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The steps from the FAQ already linked to?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65122
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 15
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 65122
    91
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 317
    Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please try with expression tag (<%= variablenmae %>) instead of el where you want to use the value of your variable.
     
    Harpreet Singh janda
    Ranch Hand
    Posts: 317
    Eclipse IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have tried something like below:


    You can try the same logic in your code.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 65122
    91
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Don't get me started about those stupid light bulbs.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic