问题描述
我的项目正在使用JSF2.0和CDI.对于一页,我希望我的支持bean与页面的寿命相匹配. @ViewScoped似乎很合适,但它不是CDI的一部分,因此使我们的解决方案不一致.然后,我的下一个选项将是CDI @ConversationScoped.在我看来,标记对话边界的唯一方法是通过对话.begin和conversation.end进行编程的方式(我使用了Seam 2.x,在那里可以使用注释来标记对话边界).我的页面位于具有全局导航的通用布局中,这意味着有无限"的方式可以离开我的页面.如何确定用户选择哪种对话方式都可以结束(例如,单击全局导航选项,而这完全不在我的支持Bean的控制范围之内)?我希望该解决方案不会将代码传播到其他模块.如果这是不可避免的,我希望可以以跨领域(AOP)的方式实施.
My Project is using JSF2.0 and CDI. For one page, I want my backing bean to match the lifespan of the page. @ViewScoped seems a perfect fit but it's not part of CDI and then make our solution not consistent. Then my next option would be the CDI @ConversationScoped. Seems to me the only way to mark the boundary of a conversation is the program way via conversation.begin and conversation.end (I have used Seam 2.x, there you can use annotations to mark conversation boundary). My page is sitting in a common layout with global navigation, which means there are "unlimited" ways to leave my page. How can I make sure the conversation is ended whichever way the user might choose (e.g. clicking on a global navi option which is totally outside of my backing bean's control)? And I hope the solution would not spread the code to other modules; and if that's inevitable, I hope it could be implemented in a cross-cutting manner (AOP).
推荐答案
这可以通过自定义ConfigurableNavigationHandler
来实现.
This can be achieved with a custom ConfigurableNavigationHandler
.
-
实现JSF NavigationHandler
Implement a 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
<application>
<navigation-handler>com.foo.bar.NavigationHandlerTest</navigation-handler>
</application>
这种方法是集中式且微创的
This approach is centralized and minimally invasive
这篇关于如何干净地结束CDI @ConversationScoped的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!