This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Struts and the fly likes please help me to pass form bean values to the script function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "please help me to pass form bean values to the script function" Watch "please help me to pass form bean values to the script function" New topic
Author

please help me to pass form bean values to the script function

suresh sai
Ranch Hand

Joined: Jul 16, 2007
Posts: 62
how can i pass the values from body to script fucntion while loading.i,e using onload attribute. I have tried all the ways which I know but there javascript errors like undefined, null.... .I have tried to write my code here.But there are errors while loading the post.Thats why I am unable to write the code.
Please help me to resolve the issue.
Thanks in advance
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
It's hard to diagnose your problem without seeing your code and knowing what specific errors you're getting. Please try again to post your code.

Here is a bit of information about posting JavaScript code on JavaRanch

For the sake of safety, Javaranch "filters" certain JavaScript constructs in a post. For example, you can't post the word "onclick" inside a pair of <> brackets. The same is true of other words such as onload. To get around this restriction, just misspell the words (onklick, onlode, etc.) and we'll know what you mean.


Merrill
Consultant, Sima Solutions
suresh sai
Ranch Hand

Joined: Jul 16, 2007
Posts: 62
Hi All.,

I need to pass the form bean (struts) String array values to the script function.I have tried all the ways which I know.But I didnt get the solution.Here I am showing the scenario in short.

MyForm.java
-------------

private String records[]; and their getters and setters.

MyJsp.jsp
------------
<script>
function toDoList(records)
{
..........
return true;
}
</script>
<body onloadfunction="return toDoList(records)">
<html:form>
<html:hidden property="records">
</html:form>
</body>
for this one I am getting java script error---- records undefined.
and also
<body onloadfunction="return toDoList(<%= session.getAttirbute("records")%> ">
for this one I am getting java script error---- ] expected at <body>
but the values are setting in to the bean in both ways
and in another one I am getting null like that.
Please help me to solve the problem.
Thanks in advance.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
It's important to understand that the only way you can pass Java values that exist on the server to JavaScript values that exist on the browser is by having your JSP become a sort of JavaScript code generator that will produce the JavaScript code that you need.

In your particular case, you want to take an array of values that exists on the server and use it to create a JavaScript array. I'd suggest you not try to pass it as a parameter, but just use it to create the JavaScript array. Here's an example


This way the javascript array "records" gets regenerated every time the page is refreshed from the server-side Java array "records". Note that the above assumes your web application is Servlet version 2.4. If not, you will have to enclose ${record} with a cut tag. Also, the above code assumes that each element of records is a String. If it's a complex data type, you will have to further break down each element into string components and create a corresponding JavaScript object with the same properties.
[ December 10, 2007: Message edited by: Merrill Higginson ]
suresh sai
Ranch Hand

Joined: Jul 16, 2007
Posts: 62
Thank you Merill. Thank you very much.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please help me to pass form bean values to the script function