You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by xianwinwin <xi...@gmail.com> on 2009/02/09 16:43:09 UTC

howto implement a @before annotation ?

Hi all,
here's a scenario:I have some methods that before executed, they should run
an interceptor --to verify if the user is authorized to do something.

I read that struts2 has a @before @after interceptors that could help here.

here's my though

@before 
public String activateProcess()
{
     //do something here
}


so in this case, since the method has a @before - it will execute the
@before's interceptor first and only then the method activateProcess
(assuming all is well with the interceptor)

how can I implement the @before???
Thanks for any pointers. 


-- 
View this message in context: http://www.nabble.com/howto-implement-a-%40before-annotation---tp21915451p21915451.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: howto implement a @before annotation ?

Posted by xianwinwin <xi...@gmail.com>.
YES! thank you so much this really helps!







Piero Sartini-3 wrote:
> 
> On Monday 09 February 2009 16:43:09 xianwinwin wrote:
>> Hi all,
>> here's a scenario:I have some methods that before executed, they should
>> run
>> an interceptor --to verify if the user is authorized to do something.
> 
> Write your own interceptor and check for your annotation. Something like
> this 
> should work (I am using a custom annotation named @Secured):
> 
>     public String intercept(ActionInvocation actionInvocation) throws 
> Exception {
>             Object action = actionInvocation.getAction(); // get the
> action on 
> which the interceptor is fired
>             Class[] ca = {};
>             Method m = 
> action.getClass().getMethod(actionInvocation.getProxy().getMethod(), ca);
> // 
> get executed method
>             Class c = action.getClass();
>             boolean classAnnotation =
> c.isAnnotationPresent(Secured.class);
>             boolean methodAnnotation =
> m.isAnnotationPresent(Secured.class);
>             // check if either method or class is annotated with @Secured
>             if (classAnnotation || methodAnnotation) {
> ...
> ...
> 
> 
> Have fun :-)
> 
> 	Piero
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/howto-implement-a-%40before-annotation---tp21915451p21916477.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: howto implement a @before annotation ?

Posted by Piero Sartini <li...@pierosartini.de>.
On Monday 09 February 2009 16:43:09 xianwinwin wrote:
> Hi all,
> here's a scenario:I have some methods that before executed, they should run
> an interceptor --to verify if the user is authorized to do something.

Write your own interceptor and check for your annotation. Something like this 
should work (I am using a custom annotation named @Secured):

    public String intercept(ActionInvocation actionInvocation) throws 
Exception {
            Object action = actionInvocation.getAction(); // get the action on 
which the interceptor is fired
            Class[] ca = {};
            Method m = 
action.getClass().getMethod(actionInvocation.getProxy().getMethod(), ca); // 
get executed method
            Class c = action.getClass();
            boolean classAnnotation = c.isAnnotationPresent(Secured.class);
            boolean methodAnnotation = m.isAnnotationPresent(Secured.class);
            // check if either method or class is annotated with @Secured
            if (classAnnotation || methodAnnotation) {
...
...


Have fun :-)

	Piero

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