File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes struts 1.X Action   vs  Actionmappings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts 1.X Action   vs  Actionmappings" Watch "struts 1.X Action   vs  Actionmappings" New topic
Author

struts 1.X Action vs Actionmappings

lalit upadheyay
Ranch Hand

Joined: Jun 20, 2005
Posts: 110
Hi All,

Supoose I have action mappings defined in struts-config.xml as:



If I add a cutomer via /addCustomer.jsp and then update that customer via /editCustomer.jsp, how many instances of actionForm, action, actionmappings will be created ?


SCJP1.4, SCWCD1.4, SCBCD5.0(working on...)
Gui Liu
Greenhorn

Joined: Mar 21, 2010
Posts: 2
A servlet only has one instances in the server, but it will create a new thread to handle every new request.
lalit upadheyay
Ranch Hand

Joined: Jun 20, 2005
Posts: 110
That's true. As the struts controller (ActionServlet is actually a servlet) and it tries to re-use instances of existing components (actionforms, action classes etc). Keeping this in mind, just want to know how many action instances will be craeted if we have mupltiple action mappings for the same action (mywebapp.actions.ManageCustomerAction) ?
How many action form instances will be created if we have multiple action form mappings for the same action form(mywebapp.forms.CustomerForm) ?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

There's an action created per action mapping (if I'm remembering correctly). This is trivial to verify by simply logging action creations. ActionForms are only re-used if they're session-scoped, otherwise there's an instance created per request.

Why do you want to know?
lalit upadheyay
Ranch Hand

Joined: Jun 20, 2005
Posts: 110
Thanks David.
This is trivial to verify by simply logging action creations.

Should I create public constructor of my action and write log statements in it for this ?

ActionForms are only re-used if they're session-scoped, otherwise there's an instance created per request.


What if I have action mappings modified as :


Still will the same instance of CustomerForm be used for serving both the requests ?

Just want to resolve the puzzle around singleton behaviour of action instances. If they are truely singleton or not ?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No, they won't be the same form--how could they be? One is created per-request, one is per-session.

Like I said-I'd just check. I honestly don't remember if it's an action for every specific mapping--I suspect it's per-mapping, but that's a guess.
Shailesh Narkhede
Ranch Hand

Joined: Jul 10, 2008
Posts: 368
Hi,

Action instance is single per application per Action.only one instance of any action class is created.
That are actually maintained in HashMap named "actions" which is instance variable of RequestProcessor class. and only one instance of request processor is created per application per module.
In hashmap key is full path of Class value is instance of action class.
every time before creation action first struts framework will check it is in hashmap or not, if it is there it is reused else newly created and put it in hashmap.
mapping is not considered for putting data in hashmap hence I think only one instance of action class will be created.


Thanks,
Shailesh
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(That seems to be correct; I just looked at the RequestProcessor.processActionCreate, and it looks up only by class name.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: struts 1.X Action vs Actionmappings