Re: Struts 2 get custom action anotation in interceptors

2014-06-07 Thread Antonios Gkogkakis
Another way to get the annotation associated with the executed action is

private static boolean isAnnotationPresent(ActionInvocation
actionInvocation,
Class 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  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 
> wrote:
>
>
>
> Take a look on com.opensymphony.xwork2.util.AnnotationUtils
>
>
> 2014-06-03 19:37 GMT+02:00 Alireza Fattahi :
> > 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-unsubscr...@struts.apache.org
> For additional commands, e-mail: user-h...@struts.apache.org


Re: Find action method in interceptor ( One action class with lots of action methods in it)

2014-06-07 Thread Antonios Gkogkakis
Hi Alireza,

I believe you can have the method name of the executed action like this
actionInvocation.getProxy().getMethod()


Antonios




On 7 June 2014 06:29, Alireza Fattahi  wrote:

> Please consider below action class:
>
>  public class MyAction extends ActionSupport{
>
>   @Action("action1")
>   public String action1(){
>
>   }
>
>   @Action("action2")
>public String action2(){
> }
>
>   @Action("action3")
>   public String action3(){
>
>   }
> }
>
> Is there any way in an inerceptor which I can find which method was really
> executed. I can find the class with
>
> myActionClass = invocation.getAction().getClass(); As I have many action
> methods in one class, how can I found which method was executed
>
>
> ~Regards,
> ~~Alireza Fattahi