wood burning stoves 2.0*
The moose likes Struts and the fly likes Struts value into a javascript function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts value into a javascript function" Watch "Struts value into a javascript function" New topic
Author

Struts value into a javascript function

A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Hi all I am trying to get a value from struts into a javascript function. Basically I am trying to achieve the following:
I am trying to get a string (that holds a dozen addresses in a comma separated list ) from a sting stored in an object into the javascript function. I want to be able to then parse the pieces of the address out of the string and set parts of a form with them, my issue is I can't seem to figure out how to put the string into the javascript part - I tried the following:



This was based on a hacking some items I found in a forum, but essentially all I need is
putting the string from <bean:write name="User" property="defaultLocations"
into the javascript function to be able to parse it.

Any suggestions or thoughts would be greatly appreciated.

thanks
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Use an EL expression instead. Example:

If your web application is a Servlet Version 2.4 application, the above is all you need. If it's a Servlet 2.3 application, you will need to use the Struts-el version of the html tags.


Merrill
Consultant, Sima Solutions
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Thanks for the post.

Unforuntately I am using the 2.3 specification. For those who need info on where to find info on el I went here:
http://struts.apache.org/1.x/struts-el/index.html

The part I am confused about and would love any assitance anyone can suggest is how to substitute in the information.

I tried the following:

only to be met by:
Unterminated <html:select tag. Can anyone shed some light on my problem ?

thanks in advance.

[ October 10, 2007: Message edited by: A knibbs ]
[ October 10, 2007: Message edited by: A knibbs ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You've missed the point. The Struts-el tags were created so that you could use EL expressions in tag attributes. Since it's an html tag for which you want to substitute an attribute, it's the html-el tags you want to use, not the bean-el tags.

To use the struts-el tags, just substitue the following taglib declaration for the one that you're currently using:

Make sure that you have the struts-el.jar file in your WEB-INF/lib directory.

Then, just code:
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Thanks for the suggestions Merrill and for those wondering where to find struts-el.jar it is available here:
http://people.apache.org/repo/m1-ibiblio-rsync-repository/struts/jars/
apparently there was an issue with it not being loaded in some versions of struts.

I tried what you suggest and unfortunately it started by saying that :
"Options tag must be nestedin a Select tag"

So I was hoping it would be as simple as adding -el to the end of the select tag, but apparently it no longer shows the drop down at all. I am guessing it is because the info that was in the select tag also has to use el expressions, in which case I think it's time for me to read.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Make sure your taglib definitions are correct. If they are missing the unknown jsp tags will be ignored. That sounds like it might be your problem.

- Brent
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The struts-el.jar file is included with the download of any Struts version 1.1 or greater. For version 1.2.9 and below, it's in the contrib/struts-el/lib directory of the unzipped download file. For later versions, the name of the file is struts-el-1.3.x.jar and it's in the /lib directory.

Also, let me clarify something: The reason I suggested using EL in the first place is that you cannot embed one Struts tag inside another. For example, you cannot have a <html:select> tag that has a <bean:write> tag in one of its attributes. EL Expressions can be embedded anywhere in a Servlet 2.4 application, but they can only be embedded in the struts-el version of Struts tags for a Servlet 2.3 application.
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Thanks for the information Merrill. As I said unfortunately I am using the
2.3 Servlet specification. Perhaps this is a situation where I am trying to fit a round peg in a smaller round hole.
Basically what I am trying to do is set up a page asking for delivery information. There are a couple of default address that can be used. If they select a default address I want to change all the fields via javascript on the page to match said information. The default names are what is in the dropdown. Once one of them is selected on change fires a javascript function that will take in the string of all the addresses in a comma seperated form, pull out the values and then assign them to their specific address, at this point I am simply trying to add the string assuming that it should be trivial to add the selected value to the list as well - hopefully that is the case.

Does this make sense with what I am attempting ?

Also I didn't realize that the file would be in the contrib/struts-el/lib and for some reason didn't look in there.

Brent - I was missing the tld's, for some reason I just copied over the jar file and completely forgot to include the tld's, so now I have the tld's for struts-html-el, struts-bean-el, and struts-logic-el

Now when I try to run the program I don't get any errors, but my drop down menu is no longer visible either. Any further thoughts ?

*Edit*
I'm an idiot, I had a typo in my tag definition which is why it wasn't being picked up.
Thanks so much.


I currently have the following:


defLocals is a list of the defalt location names, where as defaultLocations is the comma seperated string.

As always I appreciate any help that you have offered up to this point.
[ October 12, 2007: Message edited by: A knibbs ]
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Thanks for all the help to this point. I have (hopefully ) one last question.
How do I pull out the selected value from it and not the entire list ?

I am trying to do something like :


Unfortunately I get the entire arrayList, not just the value that is selected - any thoughts or ideas ?
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Unfortunately I am still fighting with this one - anyone have any ideas as to how I can pull out the selected index to pass into the javascript so I can pull out and set all the necessary info from the string ?


thanks in advance.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'm not 100 percent sure, and I don't have time to test it right now, but...

I believe it's as simple as:

Assuming defaultLocations is either a List or Array of Strings, the above will display the first element of the list or array.

If your goal is to format the list so that it can be put in a JavaScript fucntion, another possibility would be to add a getter to your ActionForm with a name something like getFormattedDefaultLocations that would return a string something like this:

Then you could simply use the expression

[ October 16, 2007: Message edited by: Merrill Higginson ]
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Originally posted by Merrill Higginson:
I'm not 100 percent sure, and I don't have time to test it right now, but...

I believe it's as simple as:

Assuming defaultLocations is either a List or Array of Strings, the above will display the first element of the list or array.

If your goal is to format the list so that it can be put in a JavaScript fucntion, another possibility would be to add a getter to your ActionForm with a name something like getFormattedDefaultLocations that would return a string something like this:

Then you could simply use the expression


[ October 16, 2007: Message edited by: Merrill Higginson ]


Actually I was trying to determine which of the items was selected. In the end I ended up using something along the lines of :

document.formName.optionsName.selectedIndex

and it worked exactly as I needed it to.
Thank you so much for all the assistance you gave me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts value into a javascript function
 
Similar Threads
Pass selected bean property to javascript
Struts +retrieving values from text area
convert struts form value to an integer value
Pass a parameter to an event handler
Arraylist in JSP with Html radio