【问题标题】:JSF Interceptor doesn't fireJSF 拦截器不触发
【发布时间】:2015-02-24 19:13:19
【问题描述】:

为什么我的拦截器不起作用?

MyLog.java

@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface MyLog {
}

MyLogger.java

@Interceptor
@MyLog
@Priority(Interceptor.Priority.APPLICATION)
public class MyLogger {
    @AroundInvoke
    public Object log(InvocationContext context) throws Exception{ 
        System.out.println("begin " + context.getMethod().getName());
        Object obj = context.proceed();
        System.out.println("end " + context.getMethod().getName());
        return obj;
    }
}

PerguntaController.java

import interceptor.MyLog;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
@Named("PerguntaController")
@SessionScoped
public class PerguntaController implements Serializable {

    @EJB
    private PerguntaFacade ejbFacade;

    @MyLog
    public List<> getAll() {
        return ejbFacade.getAll();
    }

    @MyLog
    public void update(Pergunta pergunta) {
        ejbFacade.update(pergunta);
    }
}

PerguntaFacade.java

import interceptor.MyLog;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class PerguntaFacade {
    @PersistenceContext(unitName = "WebApplicationPU")
    private EntityManager em;

    @MyLog
    public List<Pergunta> getAll() {
        return em.createQuery("SELECT p FROM Pergunta p", Pergunta.class).getResultList();
    }

    @MyLog
    public void update(Pergunta pergunta) {
      //do something
    }
}

当在 jsf 页面中使用 getAll 和更新(来自 PerguntaController)时,不会触发拦截器,也不会触发 PerguntaFacade 上的 getAll 和更新。我做错了什么?

【问题讨论】:

  • 请添加您的导入。PerguntaController 是 CDI bean 还是 JSF 托管 Bean?
  • 这是一个 CDI Bean。我正在使用 glassfish。
  • 哪个版本的 GlassFish?
  • 您使用的是 cdi 1.1 吗?如果不在 beans.xml 中注册你的拦截器
  • 是 cdi 1.1 和 beans.xml 与 bean-discovery-mode="annotated"

标签: cdi jsf-2.2 interceptor


【解决方案1】:

解决了。

在带有bean-discovery-mode="annotated" 的 beans.xml 上不起作用。 然后改成bean-discovery-mode="all"就可以了。

【讨论】:

    猜你喜欢
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多