struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Andrew Hill" <andrew.david.h...@gridnode.com>
Subject RE: extending Action problems
Date Fri, 04 Jul 2003 09:42:38 GMT
Yes, this is a quite annoying feature of the singleton nature of Actions
(and given the efficiency of modern JVMs at instantiating and garbage
collecting there is jolly good argument for changing the RequestProcessor to
instantiate new instances of Action for each request (and do feel free to
try this at home kids!) - though Im getting off topic here).

One way of dealing with this is to create a bean (or bean like object) that
has the getters and setters you need, and to pass this to any method in your
action that needs it. You instantiate the object at the start of perform()
(or execute() method in struts1.1) and then pass the reference to methods.

In my app I have an object for this task which I named ActionContext - this
basically just wrapped a Hashmap into which I could insert/retrieve stuff
with ActionContext.setAttribute, getAttribute, and I also has specific
getters for the perform signature objects you mentioned - ActionForm,
ActionMapping, HttpServletRequest and HttpServletResponse references -
(which are passed to its constructor) - so you still have to pass one
reference around as a parameter - but its a lot less typing than 4 - and its
a great place for putting other stuff as well that is internal to the action
(and for which you would rather not use the request attributes for reasons
of scoping purity). And of course since the object is instantiated in the
action and is only used in that thread and is garbage collected at the end,
it does not suffer the thread safety constraints you encountered.

<btw>
If you want to store other stuff in it you may decide Hashmaps are a bit on
the heavy side - in which case you could have a superclass for your
'ActionContext' that has getters for request,mapping,response,actionform and
instantiate  classes (inner classes perhaps) in your Actions that add extra
properties specific to the needs of the action.
</btw>

<drop-name celebrity="Ted Husted">
Actually I vaguely recall Ted mentioning in a reply to some post of mine
that he used a similar technique quite often - but it may have been someone
else so don't quote me on that!
</drop-name>


-----Original Message-----
From: Billy Ng [mailto:kwokng@earthlink.net]
Sent: Friday, 4 July 2003 17:06
To: Struts Users Mailing List
Subject: extending Action problems


I make a mistake on extending the Action.  I have the following code to set
the parameters in the perform() to the setters.  At first,  I wanted
whichever the classes that extends ActionBase can get the parameters by
simply calling the getters.  However, the Action is a singleton, the
instance variables will be used by all actions.  Anybody can give me
suggestion to make the getters to return the mapping, form, req, and resp as
local variables?

Thanks!

public class ActionBase extends Action {

    private ActionMapping mapping;
    private ActionForward actionForward;
    private ActionForm form;
    private HttpServletRequest req;
    private HttpServletResponse resp;
    private String view;

    protected void process() throws Exception {}

    public ActionForward perform(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest req,
                                 HttpServletResponse resp) {

        setActionMapping();
        setActionForm(data);
        setRequest(req);
        setResponse(resp);
        process();
   }

    protected void setActionMapping(ActionMapping mapping) {
        this.mapping = mapping;
    }

    protected ActionMapping getActionMapping() {
        return this.mapping;
    }

    protected void setRequest(HttpServletRequest req) {
        this.req = req;
    }

    protected HttpServletRequest getRequest() {
        return this.req;
    }

    protected void setResponse(HttpServletResponse resp) {
        this.resp = resp;
    }

    protected HttpServletResponse getResponse() {
        return this.resp;
    }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Mime
View raw message