jump to navigation

Struts form names October 11, 2007

Posted by Phill in Struts.
Tags: ,
comments closed

One of our clients recently wanted to integrate some tracking software into an application which uses Struts. Basically what you need to do is include some JavaScript into your page, and the tracking software will do the rest (a bit like Google Analytics). Unlike Google Analytics, however, this tracking software had some fairly specific requirements as to things like form field names and form names. One of them was that each form had to have a unique name, and the form name had to change when someone went back to edit their details… the problem is, in Struts the form takes its name from the form bean you’re using. You can’t actually explicitly set the form name using the <html:form> tag.

Fortunately, there’s an easy solution: each input element (i.e., <html:text>) has a “name” attribute which you can use to set the name of the form bean that you’re using. Do that for all of your input elements, and then replace <html:form> with a conventional <form> tag, and everything should work OK.

Of course, having to specify a form name is probably a rather unusual requirement, but just in case it helps someone I publish it here for posterity!