You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by ML...@abusinessware.com on 2007/04/18 10:10:44 UTC

[s2]Would like each action to go throught my parent class

Hi all,

I would like to do as in Struts 1 with DispathAction : create a parent 
Action, which each other action extends, and each request/action passes by 
this parent action, and I could so add there the generic validations (user 
logged, etc...)

How could I do ?

I tried this :

My parent Action : 
public class CRMAction extends ActionSupport implements 
ServletRequestAware {
        private static final long serialVersionUID = 7543537304330132164L;
        private Logger log = Logger.getLogger(CRMAction.class);
 
        private HttpServletRequest request;

        @Override
        public String execute() throws Exception {
                //do all tests (user logged, ...)
                //...
                return super.execute();
        }
}

My others Actions :
public class LoginAction extends CRMAction {
        private static final long serialVersionUID = 
-5438115193615924021L;
        private Logger log = Logger.getLogger(LoginAction.class);
 
        public String myMethod () {
                //do something
                return "myForward";
        }
}

But I never go in CRMAction.execute()...

Thanks,

Regards,

Michaël

Re: [s2]Would like each action to go throught my parent class

Posted by Dave Newton <ne...@yahoo.com>.
--- MLENEVEUT@abusinessware.com wrote:
> I was talking about a "dynamic" mecanism. In Struts
> 1, I never called super.dispathMethod() : the
> CRMAction.dispatchMethod() was first called by 
> Struts, then my "return super.dispatchMethod()" was
> returning the ActionForward returned by my called 
> Action.

That's certainly true, but S2 doesn't have a
DispatchAction, so that's not really going to work.

You may want to consider using interceptors for much
of the cross-cutting functionality you described.

d.


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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


Re: [s2]Would like each action to go throught my parent class

Posted by ML...@abusinessware.com.
I was talking about a "dynamic" mecanism. In Struts 1, I never called 
super.dispathMethod() : the CRMAction.dispatchMethod() was first called by 
Struts, then my "return super.dispatchMethod()" was returning the 
ActionForward returned by my called Action.

public class CRMAction extends DispatchAction {
        protected ActionForward dispatchMethod(ActionMapping mapping, 
ActionForm form, HttpServletRequest request, HttpServletResponse response, 
String name) throws Exception {
                try{
                        log.debug("CRM dispatchMethod.debut");
                        UserBean user = getConnectedUser(request);
                        if(user==null) {
                                log.error("User null !");
                                return mapping.findForward("error");
                        }
                        return super.dispatchMethod(mapping, form, 
request, response, name);
                } catch (Exception e) {
                        log.error(e.getMessage());
                        return mapping.findForward("error");
                }
        }

Regards,

Michaël




--- MLENEVEUT@abusinessware.com wrote:
> My parent Action : 
> public class CRMAction extends ActionSupport
>         @Override
>         public String execute() throws Exception {
>                 //do all tests (user logged, ...)
>                 //...
>                 return super.execute();
>         }
> }
> 
> My others Actions :
> public class LoginAction extends CRMAction {
>         public String myMethod () {
>                 //do something
>                 return "myForward";
>         }
> }
> 
> But I never go in CRMAction.execute()...

You never call super.execute() in your subclass.

d.




Re: [s2]Would like each action to go throught my parent class

Posted by Dave Newton <ne...@yahoo.com>.
--- MLENEVEUT@abusinessware.com wrote:
> My parent Action : 
> public class CRMAction extends ActionSupport
>         @Override
>         public String execute() throws Exception {
>                 //do all tests (user logged, ...)
>                 //...
>                 return super.execute();
>         }
> }
> 
> My others Actions :
> public class LoginAction extends CRMAction {
>         public String myMethod () {
>                 //do something
>                 return "myForward";
>         }
> }
> 
> But I never go in CRMAction.execute()...

You never call super.execute() in your subclass.

d.


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

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


Re: [s2]Would like each action to go throught my parent class

Posted by Mark Menard <ma...@mjm.net>.
On 4/18/07 11:11 AM, "Mark Menard" <ma...@mjm.net> wrote:

> Now, with that example shown, I wouldn't use it. Look at an interceptor to
> do this. I'm working on exactly this right now, a login interceptor. Stay
> tuned for a write up when I get done.

As promised here's a fairly complete write up of the process:

http://www.vitarara.org/cms/struts_2_cookbook/creating_a_login_interceptor

Mark

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


Re: [s2]Would like each action to go throught my parent class

Posted by Mark Menard <ma...@mjm.net>.
On 4/18/07 4:10 AM, "MLENEVEUT@abusinessware.com"
<ML...@abusinessware.com> wrote:

> How could I do ?

Parent action:

Public abstract class MyParentAction extends ActionSupport {

    public String execute () throws Exception {
        // do your security stuff.
        String result = this.myExecute ();
        // do any cleanup.
        return result;
    }

    public abstract String myExecute () throws Exception;
}

Public class MyChildAction extends MyParentAction {

    public String myExecute () throws Exception {
        // Do what ever you want.
    }
}

It's just an implementation of the template pattern.

Now, with that example shown, I wouldn't use it. Look at an interceptor to
do this. I'm working on exactly this right now, a login interceptor. Stay
tuned for a write up when I get done.

Mark

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