As a simple rule, if you instantiated the object yourself, @Inject won't
work.

See here:
private final MethodAdvice advice = new MethodAdvice() ...

You've used "new" so it's not under tapestry's control.
 Hello,
I have the following class to handle the execution of methods that are
annotated with a custom @ExceptionDisplay annotation. Everything works as
expected, but I would like to redirect to a page. I'm using
PageRenderLinkSource and Response to do that. I inject them in the same way
I usually inject them into pages, but in this case it does not work,
because the PageRenderLinkSource and Response instances are both null. I
imagine that's because they should be only injected into pages or
components, so I'm wondering if / how is it possible to redirect to a page
from a MethodAdvice. Thanks in advance for any possible suggestion.

------------------------------------
ExceptionDisplayWorker.java
------------------------------------

package tapestry.util.annotations;

import java.io.IOException;

import org.apache.tapestry5.Link;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.model.MutableComponentModel;
import org.apache.tapestry5.plastic.MethodAdvice;
import org.apache.tapestry5.plastic.MethodInvocation;
import org.apache.tapestry5.plastic.PlasticClass;
import org.apache.tapestry5.plastic.PlasticMethod;
import org.apache.tapestry5.services.PageRenderLinkSource;
import org.apache.tapestry5.services.Response;
import
org.apache.tapestry5.services.transform.ComponentClassTransformWorker2;
import org.apache.tapestry5.services.transform.TransformationSupport;

import tapestry.pages.ExceptionDisplayPage;
import tapestry.annotations.ExceptionDisplay;

public class ExceptionDisplayWorker implements
ComponentClassTransformWorker2
{
       private final MethodAdvice advice = new MethodAdvice()
        {
              @Inject
              private PageRenderLinkSource pageRenderLinkSource;

              @Inject
              private Response             response;

            @Override
            public void advise(MethodInvocation invocation)
            {
                System.out.println(this.pageRenderLinkSource + ";" +
this.response); // Both null.

                MethodInvocation result = invocation.proceed();

                if (result.didThrowCheckedException())
                {
                     Exception exception =
result.getCheckedException(Exception.class);

                     exception.printStackTrace();

                     // Redirect to the exception page :

                     result.setCheckedException(null);

                     Link exceptionDisplayPage =
this.pageRenderLinkSource.createPageRenderLink(ExceptionDisplayPage.class);
// Problem here, pageRenderLinkSource is null.

                     try
                    {

 this.response.sendRedirect(exceptionDisplayPage); // Problem here too,
response is null too.

                    }
                    catch (IOException e)
                    {
                           e.printStackTrace();
                    }
                }
            }
        };

       @Override
       public void transform(PlasticClass plasticClass,
TransformationSupport support, MutableComponentModel model)
       {
              for (PlasticMethod method :
plasticClass.getMethodsWithAnnotation(ExceptionDisplay.class))
              {
                     method.addAdvice(this.advice);
              }
       }
}


-----

Reply via email to