• Post Reply Bookmark Topic Watch Topic
  • New Topic

assign a jsp bean to a javascript array

 
Uma Vinodh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to assign a jsp bean to an array in javascript function.

here is my bean
<bean:define id="CntList" name="identificationActionForm" property="countyData" type="java.util.Collection"/>

I want to pass "CntList" to an array in javascript.

My code for this is

function dis1(stateid)
{
if(stateid.value!=24)
{
countyArray=new Array(<%=CntList %>); }
}


it is giving syntax error.
Can anyone help me in this?

Thanks,
Uma.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't just mix Java and JavaScript. this article to understand how JSP operates.
 
Uma Vinodh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can you give me an idea of how to fill an array in javascript with the values of a bean?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read the article?
 
Albareto McKenzie
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Bear has told you it's not a good idea to mix Java and JavaScript, but if you are going to do it I think you have to surround the java assignament with double quotes (I am not sure of this).
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's less of "not a good idea" rather than an impossibility. The JSP executes on the server, the JavaScript much later on the client.

The best you can do is to use the JSP mechanisms to create JavaScript markup that recreates the values on the client when the HTML and JavaScript are interpreted in the browser after the JSP has executed and sent the response.

 
Albareto McKenzie
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:It's less of "not a good idea" rather than an impossibility. The JSP executes on the server, the JavaScript much later on the client.

The best you can do is to use the JSP mechanisms to create JavaScript markup that recreates the values on the client when the HTML and JavaScript are interpreted in the browser after the JSP has executed and sent the response.



But imagine you have this dirty code:



This will be translated in the server as 3 and a will have value "3", isn't it?

Even more I have seen things like this:



Of course when you see the page source this is all static JS...
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your first example is exactly what I'm talking about; using JSP mechanisms to create valid JavaScript markup that will be interpreted once the page is shipped off ot the browser.

Your second example will not produce valid client-side JavaScript.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!