wood burning stoves*
The moose likes Struts and the fly likes struts 2 action class instantiation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts 2 action class instantiation" Watch "struts 2 action class instantiation" New topic
Author

struts 2 action class instantiation

steve Pentas
Greenhorn

Joined: Mar 05, 2009
Posts: 14
In my struts.xml file, I have two actions, each of them maps to the *SAME* Action class but two different methods. And these two methods don't call each other. e.g. I have a "/getInventory" action link that maps to "getInventory()" method and "/getInvoice" action that maps to "getInvoice()" method of the same Action class "Order". In the "Order" class there is a "prepare" method.

Questions:

1. On my web app, when I click a link of "get inventory", it calls "/getInventory" and thus triggers the "getInventory()" method. I assume that at this time JVM instantiates an instance of "Order" class, right ? Now, there is another link of "getInvoice", when I click this link, it triggers "getInvoice()" method, does JVM instantiate a *separate* instance of "Order" class ? Or does it use the same instance that was created when I clicked "/getInventory" link ?

2. In my Order class, I have

public class Order implements Preparable, .... {

private String var1;
...

public void prepare() {
// initialize variable var1, var2, etc...
}
}

I want to know if "var1" is thread safe. I know in servlet, usually class variable is not thread-safe. How about in struts 2's action class ? For servlet it uses one instance multithread model, how about struts 2 action class ? For each action that appears in the struts xml config file, does JVM automatically generates a new action class instance each time an action link is clicked ? or does it behave the way as servlet which is to create a new thread ? If it behaves like servlet then I better not use any class variables.

I can't find accurate answers myself. ANy help will be appreciated.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

Steve a simple google query like thread safe struts 2 actions could have got you to this page where it is clearly written

Struts 2 Action objects are instantiated for each request, so there are no thread-safety issues.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
steve Pentas
Greenhorn

Joined: Mar 05, 2009
Posts: 14
So, since struts 2 always creates a new instance per request, each time whenit creates a new instance, it calls its prepare() method, and re-initialize its class variables. And these class variables will never be messed up with each other as they belong to different objects. Cool stuff !
Thanks.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It only calls prepare() if (a) the action implements Preparable, and (b) the "prepare" interceptor is in the action's interceptor stack.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: struts 2 action class instantiation