`
yangzhanchun
  • 浏览: 2919 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JSF中managebean的拦截(interceptor)

阅读更多
需求是在JSF中需要对managebean的方法进行拦截,首先jsf本身没找到支持的方式,和spring集成的话,貌似managebean可能用spring来管理,但是这个没找到切却的方式,最好找到使用CDI beans来替代managebean,然后对bean的方法进行拦截。

版本要求:jsf2.2,j2ee6,好像tomcat7才支持cdi,或者weblogic,websphere之类,这些具体还没验证.

代码:
一、bean上原来使用@ManagedBean的地方替换为@Named
对需要拦截的方法加上注解
import javax.inject.Named; 
@Named  

...


 @Interceptors(HelloInterceptor. class)
 public void getSomething() {

 }



二、实现拦截类
public class HelloInterceptor {

      @AroundInvoke
      public Object hello(InvocationContext ctx) throws Exception {
            System.out.println( "AroundInvoke");
            try {
                   return ctx.proceed();
            } catch (Exception e) {
               System.out.println( "Error calling ctx.proceed in hello()");
               return null;
            }
      }

}

这样,页面上调用getSomething时,会进去到拦截的方法中,ctx里面可以获取到具体拦截的方法等信息
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics