Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jakarta ee 如何干净地结束CDI@ConversationScoped_Jakarta Ee_Jsf 2_Cdi - Fatal编程技术网

Jakarta ee 如何干净地结束CDI@ConversationScoped

Jakarta ee 如何干净地结束CDI@ConversationScoped,jakarta-ee,jsf-2,cdi,Jakarta Ee,Jsf 2,Cdi,我的项目使用JSF2.0和CDI。对于一个页面,我希望我的支持bean与页面的寿命相匹配@ViewScope似乎非常适合,但它不是CDI的一部分,因此使我们的解决方案不一致。然后我的下一个选项是CDI@conversationscope。在我看来,标记对话边界的唯一方法是通过conversation.begin和conversation.end(我已经使用了Seam 2.x,在这里您可以使用注释来标记对话边界)的程序方式。我的页面位于一个具有全局导航的通用布局中,这意味着有“无限”的方式离开我的

我的项目使用JSF2.0和CDI。对于一个页面,我希望我的支持bean与页面的寿命相匹配@ViewScope似乎非常适合,但它不是CDI的一部分,因此使我们的解决方案不一致。然后我的下一个选项是CDI@conversationscope。在我看来,标记对话边界的唯一方法是通过conversation.begin和conversation.end(我已经使用了Seam 2.x,在这里您可以使用注释来标记对话边界)的程序方式。我的页面位于一个具有全局导航的通用布局中,这意味着有“无限”的方式离开我的页面。我如何确保对话以用户可能选择的任何方式结束(例如,单击完全不在我的支持bean控制范围内的全局navi选项)?我希望解决方案不会将代码传播到其他模块;如果这是不可避免的,我希望它能够以交叉方式(AOP)实现

我知道,你不能。在JSF中,几乎不可能(很难)确定链接是在当前选项卡中打开的还是在新选项卡中打开的(对于新选项卡,您需要保持对话处于活动状态)


但好消息是,会话将在10分钟不活动后自动关闭(默认情况下)。

这可以通过自定义
ConfigurableNavigationHandler
实现

  • 实现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;
     }
    
    
       } 
    
  • 在faces-config.xml中注册自定义导航处理程序

    
    com.foo.bar.NavigationHandlerTest
    

  • 这种方法是集中和微创的

    好吧,把设计问题放在一边。从技术上讲,您可以使所有导航请求通过导航服务对象。每个对话都有一个id。一旦目标页面打开,我们可以将id保存在服务对象中,如果以下任何导航请求不是针对目标页面的,那么我们知道需要结束对话。由于CDI在某种程度上基于Seam,我希望我能有一个更优雅的解决方案。您可以尝试创建特殊的commandLink,而不是简单的commandLink(例如一些带有“target”参数的组合)。这样的链接将调用@ConversationScope bean上的“closeConversation”方法,并将您重定向到“target”链接……CODI的客户端窗口处理程序可以正确地检测到新的选项卡/窗口。这是一个糟糕的解决方法。我会改用CODI对话
     <application>
         <navigation-handler>com.foo.bar.NavigationHandlerTest</navigation-handler>
     </application>