私のプロジェクトはJSF2.0とCDIを使用しています。1つのページについて、バッキングBeanをページの存続期間と一致させたいと思います。@ViewScopedは完璧に適合しているように見えますが、CDIの一部ではないため、ソリューションの一貫性が失われます。次に、私の次のオプションはCDI@ConversationScopedです。会話の境界をマークする唯一の方法は、conversation.beginとconversation.endを介したプログラムの方法のようです(私はSeam 2.xを使用しましたが、注釈を使用して会話の境界をマークできます)。私のページはグローバルナビゲーションと共通のレイアウトになっています。つまり、私のページを離れる「無制限の」方法があります。ユーザーが選択する方法(たとえば、バッキングBeanの制御の完全に外れているグローバルナビオプションをクリックするなど)で会話が終了することを確認するにはどうすればよいですか?そして、このソリューションがコードを他のモジュールに広めないことを願っています。
3721 次
2 に答える
4
これは、カスタムで実現できますConfigurableNavigationHandler
。
JSFNavigationHandlerを実装する
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>
このアプローチは一元化されており、侵襲性が最小限に抑えられています
于 2013-03-15T20:44:14.950 に答える
0
私が知っているように-あなたはできません。リンクがJSFの現在のタブで開かれたのか新しいタブで開かれたのか(新しい場合は会話をアクティブのままにする必要があります)を判断することはほとんど不可能です(難しい)。
ただし、朗報です。会話は、10分間操作がないと自動的に閉じられます(デフォルト)。
于 2013-03-13T21:46:50.770 に答える