0 Replies Latest reply on Feb 22, 2012 1:40 AM by Feng Jiang

    Can the Interceptor change the intercepted method's return value?

    Feng Jiang Newbie

      Hi, guys:


           I have a try to do with the Interceptor.  I want to intercepte the mothed and do some judgments , then invoke the mothed and change it's return value.


           In a practical application , such as  a delete button , I want to detemine whether to render it based on the role that the current user has.


           My some code follows:


           1. the page:


                <h:form id="myform">
                          <h:commandButton id="test" value="Test" rendered="#{testAction.test(true)}" />


           2. the CDI bean


      public class TestAction {
                public boolean test(boolean flag){
                          return flag;


           3. the interceptor binding type


      @Target({ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
      public @interface Role {
                @Nonbinding String[] roleName() default {"all"};


           4.the interceptor


      @Role @Interceptor  
      public class RoleInterceptor {
                @Inject private UserSession userSession;
                public Object isUserHasRole(InvocationContext ctx){
                                    String[] roleNames = ctx.getMethod().getAnnotation(Role.class).roleName();
                                      // TODO: do some judgments
                                    ctx.getMethod().invoke(ctx.getTarget(), new Object[]{false});
                                    Object o = ctx.proceed();
                                    return o;
                          }catch (Exception e) {
                                    // TODO: handle exception
                          return null;


           5. enable the interceptor in the         beans.xml




           Now, the are two problem .  First, the Interceptor  method will be invoked six times  and all the times in the RenderResponse phase.   I don't know why it be invoked six times in my code.


      Second, after the interceptor method return , the intercepted method will invoke one time.   so now i can't change the inercepthed method's return value with the Interceptor .    Can it be possable?  and how should i do?