如何彻底结束CDI @ConversationScoped

问题描述 投票:4回答:2

我的项目正在使用JSF2.0和CDI。对于一页,我希望我的支持bean与页面的寿命相匹配。 @ViewScoped似乎很合适,但它不是CDI的一部分,因此使我们的解决方案不一致。然后,我的下一个选择是CDI @ConversationScoped。在我看来,标记对话边界的唯一方法是通过对话.begin和conversation.end进行编程的方式(我使用了Seam 2.x,在那里可以使用注释来标记对话边界)。我的页面位于具有全局导航的通用布局中,这意味着有“无限”的方式可以离开我的页面。如何确定用户选择的哪种对话方式都结束(例如,单击全局导航选项,而这完全不在我的支持Bean的控制范围之内)?我希望该解决方案不会将代码传播到其他模块。如果不可避免,我希望可以跨领域(AOP)实施。

java-ee jsf-2 cdi
2个回答
4
投票

这可以通过自定义ConfigurableNavigationHandler实现。

  1. 实施JSF NavigationHandler

     public class NavigationHandlerTest extends ConfigurableNavigationHandler {
    
     private NavigationHandlerTest concreteHandler;
    
     public NavigationHandlerTest(NavigationHandler concreteHandler) {
          this.concreteHandler = concreteHandler;
     }
    
    
     @Override
     public void handleNavigation(FacesContext context, String fromAction, String outcome) 
     {
        //here, check where navigation is coming from and based on that, retrieve the CDI bean and kill the conversation
         if(fromAction.equals("someAction"){
         BeanManager theBeanManager = getBeanManager(context);
         Bean bean = theBeanManager.getBeans("yourCDIBean").iterator().next() 
         CreationalContext ctx = theBeanManager.createCreationalContext(bean);
         MyBeanType o = theBeanManager.getReference(bean, bean.getClass(), ctx); //retrieve the bean from the manager by name. You're guaranteed to retrieve only one of the same name;
         o.getConversation.end(); //end the conversation from the bean reference
          }
    
         //proceed with normal navigation
         concreteHandler.handleNavigation(context, fromAction, outcome);   
    
     }
    
     //This method provides access to the cdi bean manager. You need it to be able to 
     //gain access to the cdi bean and from that to the injected conversation instance
     public BeanManager getBeanManager(FacesContext facesContext){
       BeanManager cdiBeanManager = (BeanManager)((ServletContext) facesContext.getExternalContext().getContext()).getAttribute("javax.enterprise.inject.spi.BeanManager"); 
        return cdiBeanManager;
     }
    
    
       } 
    
  2. 将自定义导航处理程序注册到faces-config.xml

     <application>
         <navigation-handler>com.foo.bar.NavigationHandlerTest</navigation-handler>
     </application>
    

此方法是集中式且微创的


0
投票

据我所知-您不能。在JSF中几乎不可能(很难)确定是在当前标签页中还是在新标签页中打开了链接(对于新标签页,您需要保持对话处于活动状态)。

但是好消息-闲置10分钟后,对话会自动关闭(默认情况下。)>

© www.soinside.com 2019 - 2024. All rights reserved.