aspose file tools*
The moose likes Struts and the fly likes It Worked! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "It Worked!" Watch "It Worked!" New topic
Author

It Worked!

Sanjay Virkar
Greenhorn

Joined: Apr 03, 2005
Posts: 19
Thanks everyone!

Merril, your following directions really helped. THANK YOU!

>I prefer using the actual name of the form, which in Struts is what you >specified as the name attribute of the action mapping in your struts->config.xml file. Exmple:
>document.myForm.adminaction.value="xyz"

Even though it was first and only form in jsp, and even though I am not using tiles, for some reason, forms[0] did not set values. After changing to the form name, everything is working as expected. For some reason same code (i.e. with forms[0]) is working in other jsp correctly. But for safety there too I will introduce form name.

Thanks again!
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I have to admit that I hate JavaScript. It always seems like there are 10 different ways to write every piece of code and that none of them work on all browsers. I recently ran across some code that worked in IE but not Firefox. During my research I ran across this page:

http://developer.mozilla.org/en/docs/Using_Web_Standards_in_your_Web_Pages#Using_the_W3C_DOM

This page and some links there seem to indicate that using "document.form.element" syntax is a bad practice. hmmm...I am not exactly sure why. I would rather not have to write code like document.forms["FormName"].elements["InputName"]. I have not figured out if the JavaScript gurus think that document.getElementById is the way to go. I did learn that IE is happy to use the name attribute of an element for getElementById, where FireFox requires that the element has an id attribute.

On a related note...and venturing a bit of the Struts topic...the little playing that I have done with the prototype JavaScript library sure seems like it make writing JavaScript less painful. http://prototypejs.org/

- Brent
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I firmly believe in the Extreme Programming principle of "Do the simplest thing that could possibly work." (DTSTTCPW). In a case like this where it's not important to make the code reusable (after all, it's just a simple one-statment event handler), the simplest thing is to write "document.myform.myproperty.value='xyz'". It's simple and it works on all browsers.

I would agree that it's not good code to put in a function, because the form name and property are hard coded. In this case, you'd want to use the other construct. Example:

[ May 10, 2007: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
This is a bit off topic. Should be in the HTML / JavaScript forum.

The way to do this is very simple.
1. document.getElementById( "inputId" ).value = "test";
2. document.getElementsByName( "inputName" )[ 0 ].value = "test";

ID must be unique (IDREF for XML defines this clearly).

IE's behavior may seem convenient, but the inconsistency and hashtable pollution/conflation it causes can be a problem.

Consider:

What would happen if we tried to get the div "foo" in IE?

[ May 12, 2007: Message edited by: Garrett Smith ]

comp.lang.javascript FAQ: http://jibbering.com/faq/
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Garrett Smith:
2. document.getElementsByName( "inputName" ).value = "test";

The above code will result in an error. getElementsByName always returns an array, even if there is only one element by that name. It will work if changed to:

Also, when you refer to "IE's behavior", I'm not sure what you're referring to. If you're referring to the "document.myform.myelement" construct, that has nothing to do with IE. That's a universal JavaScript DOM construct that works in any browser.

That's just the way JavaScript is, though. There are usually at least 2 or 3 different ways of representing something.

What started this conversation was my cautioning against the use of the construct "document.forms[0].myelement" because it relies on a particular form being the first one on the page, and that assumption may not be true, or may be true now, but change later as the page is modified. Aside from such concerns, if you like one construct over another, go for it. That's the beauty (and also the confusion!) of JavaScript.
[ May 12, 2007: Message edited by: Merrill Higginson ]
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
Edited my post. Thanks.

IE behavior: Returning named elements whose name attribute matches the argument in getElementById. ID must be unique, name is not unique. When IE gets more than 1 element with the same ID, it creates an collection.

getElementsByName returns a NodeList not an Array. There is a big difference.
[ May 12, 2007: Message edited by: Garrett Smith ]
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I will admit that my Struts project use the construct document.forms[0] in a lot of places. Well we did until early on when we added a form to the standard page heading and we had to search a replace with document.forms[1] in a lot of places. I guess we did not learn. I don't think any of the original developers (myself included) knew squat about JavaScript so we just went with something that worked.

Lately I have been thinking that the getElementById method is the way to go. The worst part is that you have to add a styleId to each Struts tag, and make sure you test in a non-IE browser. I mention prototype (which I have only played around with) because it reduces "document.getElementById('phone')" to "$('phone')" and "document.getElementById('phone').value" to "$V('phone')"

- Brent
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: It Worked!