Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How does Struts create ActionForm objects?

 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I bumped into the following piece of code on the Web and was left wondering...

...how does the cast from ActionForm to LoginForm work? How does Struts know from the HTTP request that it needs to instantiate and populate an instance of LoginForm instead of SomeOtherForm?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
LoginForm is a child of ActionForm so the cast is fine. Struts knows to use a LoginForm because the form to use is defined in the struts-config.xml file.
 
Junilu Lacar
Bartender
Posts: 7480
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Struts framework uses the definitions in the struts-config.xml. In it you specify your form classes and give each a logical name. In the action definitions, each action has a name attribute that should be one of the logical form names. This is how Struts maps forms to actions.
Edit: I should have said each action may specify a name attribute.
[ May 29, 2003: Message edited by: Junilu Lacar ]
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This type of information is in the struts-config.xml. You would see something similiar to the following in the action mappings:

The name attribute corresponds to the ActionForm to instantiate.
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should have refreshed before I posted... :roll:
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Junilu, that's what I suspected. How exactly is the form name transmitted over HTTP? As a header? A parameter?
 
David Hibbs
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lasse Koskela:
Thanks Junilu, that's what I suspected. How exactly is the form name transmitted over HTTP? As a header? A parameter?

It's not. The ActionServlet ties them together based on the action requested from the struts-config.xml.
1) Find requested action in config
2) Get form name from action config
3) Find form definition in config
4) Create Form of class specified in config
5) Populate form
6) Pass form etc. to the action
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. So the value in the HTML form has no significance other than developer-friendliness (and that there is something in order to avoid spec-break)?
 
David Hibbs
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lasse Koskela:
Ok. So the value in the HTML form has no significance other than developer-friendliness (and that there is something in order to avoid spec-break)?

Pretty much; just to clarify, though...
The name value *in the rendered HTML* has no significance *to Struts*. That is, beyond the D/HTML uses, there is no significance. The name is quite convenient in the HTML in that you can use JavaScript with the form name to submit it, access field values, etc as needed.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David. That's exactly what I meant with something.
However, I remembered incorrecly that the form element has to have a name attribute. Actually the only attribute required is 'action'. Although I do remember seeing a form working without the 'action' attribute by submitting to the current URL... Must've been IE 5.x.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic