This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Struts uses the getLocale() method of the HttpServletRequest object to get its locale.
Isn't an Action object already associated with a request object implicitly?
The request object is passed into the Action's execute method, but that doesn't mean that the Action class is implicitly associated with the request. Any time the request object needs to be used by any method other than execute, it still has to be passed as a parameter.
... which gives you pretty much control over it (same goes for response, mapping and form)...
even though i have to admit that i never used it for anything which couldn't have been achieved with getters like:
honestly dont know why struts makes us passing these object-refs around all day long... :-)
Joined: Feb 15, 2005
The reason having a "myAction.getRequest()" method isn't practical is that the Action class is used in a threaded environment and must be coded as thread-safe. This means that an Action class cannot have instance variables that aren't the same for every user.
For example, an Action could have an instance variable referring to a logger because that is the same for every user. The HttpServletRequest object is definitely not the same for every user, and would have disastrous effects if put in an instance variable.
Joined: Feb 03, 2004
i thought each request (= thread) gets its own action-instance? am i wrong about this is?
jan [ October 06, 2006: Message edited by: Jan Groth ]
Joined: Feb 15, 2005
Actually, yes, you are wrong about this. There is only one instance of each Action Class per container. It's very much like a Servlet in that multiple threads all go through the same instance of the Class. That's why it's very important that it be coded as thread-safe.
The ActionForm, on the other hand is not multi-threaded. A new instance gets created for each session or request depending on the scope specified in the action mapping. No need to worry about this object being thread-safe. [ October 06, 2006: Message edited by: Merrill Higginson ]