aspose file tools*
The moose likes Struts and the fly likes How does Struts create ActionForm objects? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How does Struts create ActionForm objects?" Watch "How does Struts create ActionForm objects?" New topic
Author

How does Struts create ActionForm objects?

Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5002
    
    8

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 ]

Junilu - [How to Ask Questions] [How to Answer Questions]
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
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

Joined: Jul 18, 2001
Posts: 2712
I should have refreshed before I posted... :roll:
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Dec 19, 2002
Posts: 374
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


"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Dec 19, 2002
Posts: 374
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How does Struts create ActionForm objects?