Meaningless Drivel is fun!*
The moose likes JSP and the fly likes iterating with a scriptlet, can't get the values into js arr Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "iterating with a scriptlet, can Watch "iterating with a scriptlet, can New topic
Author

iterating with a scriptlet, can't get the values into js arr

chris gar
Ranch Hand

Joined: Jul 20, 2002
Posts: 45


I don't understand why I cant load the javascript array with the values of the java array in the scriptlet.

I'm open to any other ways to get the data into the javascript array ??? I'm sure this is something stupid.

You help is appreciated.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

And what's the output of that scriptlet? If you're using a browser to look at the result, then use View Source to actually see the source.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61657
    
  67

... and when you do, chances are that you'll see that you are not generating valid JavaScript syntax.

(And really, scriptlets? In 2009?)


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Bear Bibeault wrote:(And really, scriptlets? In 2009?)

Being retro is cool... now lemme alone; I have some disco to take care of.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61657
    
  67

David Newton wrote:Being retro is cool... now lemme alone; I have some disco to take care of.

Some retro is cool. A classic car, for example. But some is not, platform boots for example.

Scriptlets are the platform boots of JSP.
chris gar
Ranch Hand

Joined: Jul 20, 2002
Posts: 45
Bear Bibeault wrote:... and when you do, chances are that you'll see that you are not generating valid JavaScript syntax.

(And really, scriptlets? In 2009?)


I'm quite aware that I'm not generating valid javascript syntax, thus my post. As for the scriptlets, I'm simply trying to get to the root of my problem.

I'm calling an action with asynchronous javascript. Based on a user's selection in selectBox1 (onSelect()), the options for selectBox2 are supposed to be populated. I'm able to target the action just fine with the appropriate data(option selected in selectBox1), but I don't know how to get the results (a String array) back in my javascript (where I'm manipulating the DOM) to get the correct options in selectBox2.

Any suggestions?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61657
    
  67

There is no valid reason to be using scriptlets for new code in 2009.

You haven't really explained what you are doing very well. If you're just making an Ajax call to get the data to populate a select element, you can just return the data as a JSON construct and have the JavaScript use it to reconstruct the select element.

chris gar
Ranch Hand

Joined: Jul 20, 2002
Posts: 45
Bear Bibeault wrote:There is no valid reason to be using scriptlets for new code in 2009.

You haven't really explained what you are doing very well. If you're just making an Ajax call to get the data to populate a select element, you can just return the data as a JSON construct and have the JavaScript use it to reconstruct the select element.



OK. I've abandoned the scriptlet route and tried to utilize json.

I'm still getting the results back from the action but I can't get the desired results to appear as the options in the select.





Here's the action


When a user selects a value from the dropdown att1, I want the dropdown op1 to display the results of the ajax call. The dropdown op1 contains operators for queries (=, !=, Like, etc.) Based on the type of attribute selected in the att1 dropdown, I want valid operators to appear (so for a float selection in att1, I don't want to display LIKE, etc). I will eventually do the same for the dropdown attval1 (which will require db lookup) but that should fall into place once I get op1 working.

I assume I need to do something else around "dojo.byId("op1").innerHTML = response ; " but I don't know what.
The results I get are
{"operators":[["= ","0"],["!= ","1"],["LIKE","3"],["~","4"]],"ops":"success"}

which certainly contains the data I need, but I don't know how to get [["= ","0"],["!= ","1"],["LIKE","3"],["~","4"]] of the result into the options of my select box. Certainly there is some call I can utilize rather than manually parsing.

Thanks again for your assistance.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61657
    
  67

You do not need to parse JSON yourself. At worst, you use the JavaScript eval() method. However, jQuery and Prototype have the means to automatically parse the returning JSON for you and I'd be surprised (and dismayed) if Dojo did not. I'd check out what values can be supplied for the handleAs option.

Once parsed, you just have a normal JavaScript data structure what you can use to adjust the contents of the select element. Example code for doing so is all over the net as this sort of "dependent dropdown" capability is sort of the poster child for Ajax.
 
 
subject: iterating with a scriptlet, can't get the values into js arr