You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Alireza Fattahi <af...@yahoo.com.INVALID> on 2014/06/03 19:37:18 UTC

Struts 2 get custom action anotation in interceptors

Consider below action class with three action mappings. Two of them are annotated with a custom annotation `@AjaxAction`

    public class MyAction extends ActionSupport{
    
      @Action("action1")
      @AjaxAction  //My custom anotation
      public String action1(){    
      }    
    
      @Action("action2")
       public String action2(){    
        }

      @Action("action3")
      @AjaxAction  //My custom anotation
      public String action3(){    
      }    
    }
     

In an interceptor I want to access the `@AjaxAction` annotation. Is there any built in support for this?! 

If not can I shall read the action name with `ActionContext.getContext().getName();` and save a list of `ajaxAction` names in interceptor as an array and compare action name with this array! any better way?!

    private static final String[] AJAX_ACTIONS = new String[] {"action1", "action3"}
    
    //in interceptor
    String actionName = ActionContext.getContext().getName();
    if (Arrays.asList(AJAX_ACTIONS).contains(actionName)) {
               // do something
            }

 
~Regards,
~~Alireza Fattahi

Re: Struts 2 get custom action anotation in interceptors

Posted by Dave Newton <da...@gmail.com>.
Just get the action and check for the annotation.
 On Jun 3, 2014 1:41 PM, "Alireza Fattahi" <af...@yahoo.com.invalid>
wrote:

> Consider below action class with three action mappings. Two of them are
> annotated with a custom annotation `@AjaxAction`
>
>     public class MyAction extends ActionSupport{
>
>       @Action("action1")
>       @AjaxAction  //My custom anotation
>       public String action1(){
>       }
>
>       @Action("action2")
>        public String action2(){
>         }
>
>       @Action("action3")
>       @AjaxAction  //My custom anotation
>       public String action3(){
>       }
>     }
>
>
> In an interceptor I want to access the `@AjaxAction` annotation. Is there
> any built in support for this?!
>
> If not can I shall read the action name with
> `ActionContext.getContext().getName();` and save a list of `ajaxAction`
> names in interceptor as an array and compare action name with this array!
> any better way?!
>
>     private static final String[] AJAX_ACTIONS = new String[] {"action1",
> "action3"}
>
>     //in interceptor
>     String actionName = ActionContext.getContext().getName();
>     if (Arrays.asList(AJAX_ACTIONS).contains(actionName)) {
>                // do something
>             }
>
>
> ~Regards,
> ~~Alireza Fattahi

Re: Struts 2 get custom action anotation in interceptors

Posted by Antonios Gkogkakis <gk...@tcd.ie>.
Another way to get the annotation associated with the executed action is

private static boolean isAnnotationPresent(ActionInvocation
actionInvocation,
            Class<? extends Annotation> annotationClass) {
        return
getMethodFromInvocation(actionInvocation).isAnnotationPresent(
                annotationClass);

    }

 private static Method getMethodFromInvocation(ActionInvocation
actionInvocation) {
        Method method;
        try {
            method = actionInvocation.getAction().getClass()
                    .getMethod(actionInvocation.getProxy().getMethod());

            // should never happen because struts would have failed before
this method call
            //if the method didn't exist
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
        return method;
    }

Antonios


On 7 June 2014 05:57, Alireza Fattahi <af...@yahoo.com.invalid> wrote:

> Thanks!
> A complete code sample at:
>
>
> http://stackoverflow.com/questions/24021534/struts-2-get-custom-action-anotation-in-interceptors
>
>
>
> ~Regards,
> ~~Alireza Fattahi
>
>
> On Wednesday, 4 June 2014, 12:22, Lukasz Lenart <lu...@apache.org>
> wrote:
>
>
>
> Take a look on com.opensymphony.xwork2.util.AnnotationUtils
>
>
> 2014-06-03 19:37 GMT+02:00 Alireza Fattahi <af...@yahoo.com.invalid>:
> > Consider below action class with three action mappings. Two of them are
> annotated with a custom annotation `@AjaxAction`
> >
> >     public class MyAction extends ActionSupport{
> >
> >       @Action("action1")
> >       @AjaxAction  //My custom anotation
> >       public String action1(){
> >       }
> >
> >       @Action("action2")
> >        public String action2(){
> >         }
> >
> >       @Action("action3")
> >       @AjaxAction  //My custom anotation
> >       public String action3(){
> >       }
> >     }
> >
> >
> > In an interceptor I want to access the `@AjaxAction` annotation. Is
> there any built in support for this?!
> >
> > If not can I shall read the action name with
> `ActionContext.getContext().getName();` and save a list of `ajaxAction`
> names in interceptor as an array and compare action name with this array!
> any better way?!
> >
> >     private static final String[] AJAX_ACTIONS = new String[]
> {"action1", "action3"}
> >
> >     //in interceptor
> >     String actionName = ActionContext.getContext().getName();
> >     if (Arrays.asList(AJAX_ACTIONS).contains(actionName)) {
> >                // do something
> >             }
> >
> >
> > ~Regards,
> > ~~Alireza Fattahi
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org

Re: Struts 2 get custom action anotation in interceptors

Posted by Alireza Fattahi <af...@yahoo.com.INVALID>.
Thanks!
A complete code sample at:

http://stackoverflow.com/questions/24021534/struts-2-get-custom-action-anotation-in-interceptors


 
~Regards,
~~Alireza Fattahi 


On Wednesday, 4 June 2014, 12:22, Lukasz Lenart <lu...@apache.org> wrote:
 


Take a look on com.opensymphony.xwork2.util.AnnotationUtils


2014-06-03 19:37 GMT+02:00 Alireza Fattahi <af...@yahoo.com.invalid>:
> Consider below action class with three action mappings. Two of them are annotated with a custom annotation `@AjaxAction`
>
>     public class MyAction extends ActionSupport{
>
>       @Action("action1")
>       @AjaxAction  //My custom anotation
>       public String action1(){
>       }
>
>       @Action("action2")
>        public String action2(){
>         }
>
>       @Action("action3")
>       @AjaxAction  //My custom anotation
>       public String action3(){
>       }
>     }
>
>
> In an interceptor I want to access the `@AjaxAction` annotation. Is there any built in support for this?!
>
> If not can I shall read the action name with `ActionContext.getContext().getName();` and save a list of `ajaxAction` names in interceptor as an array and compare action name with this array! any better way?!
>
>     private static final String[] AJAX_ACTIONS = new String[] {"action1", "action3"}
>
>     //in interceptor
>     String actionName = ActionContext.getContext().getName();
>     if (Arrays.asList(AJAX_ACTIONS).contains(actionName)) {
>                // do something
>             }
>
>
> ~Regards,
> ~~Alireza Fattahi

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

Re: Struts 2 get custom action anotation in interceptors

Posted by Lukasz Lenart <lu...@apache.org>.
Take a look on com.opensymphony.xwork2.util.AnnotationUtils

2014-06-03 19:37 GMT+02:00 Alireza Fattahi <af...@yahoo.com.invalid>:
> Consider below action class with three action mappings. Two of them are annotated with a custom annotation `@AjaxAction`
>
>     public class MyAction extends ActionSupport{
>
>       @Action("action1")
>       @AjaxAction  //My custom anotation
>       public String action1(){
>       }
>
>       @Action("action2")
>        public String action2(){
>         }
>
>       @Action("action3")
>       @AjaxAction  //My custom anotation
>       public String action3(){
>       }
>     }
>
>
> In an interceptor I want to access the `@AjaxAction` annotation. Is there any built in support for this?!
>
> If not can I shall read the action name with `ActionContext.getContext().getName();` and save a list of `ajaxAction` names in interceptor as an array and compare action name with this array! any better way?!
>
>     private static final String[] AJAX_ACTIONS = new String[] {"action1", "action3"}
>
>     //in interceptor
>     String actionName = ActionContext.getContext().getName();
>     if (Arrays.asList(AJAX_ACTIONS).contains(actionName)) {
>                // do something
>             }
>
>
> ~Regards,
> ~~Alireza Fattahi

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