如何(以及何时?)在JSF 2.0中删除Session作用域的bean [英] How (and when?) to remove a Session scoped bean in JSF 2.0

查看:120
本文介绍了如何(以及何时?)在JSF 2.0中删除Session作用域的bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我有一个使用MyFaces的JSF 2.0应用程序.使用此应用程序,用户将通过浏览许多不同的JSF页面来创建复杂的窗口小部件".我将托管bean存储在会话范围中,并且当用户浏览每个页面时,属性将被填充.一旦完成了小部件"的组装,他们便希望创建一个全新的小部件"并重复该过程.我的问题是,如何在不中断JSF生命周期的情况下从会话中完全(安全地)删除托管bean?

Background: I have a JSF 2.0 application using MyFaces. Using this application, the users will create a complex "Widget" by going through a number of different JSF pages. I am storing the managed bean in the session scope, and the properties get filled in as the user goes through each page. Once they are done assembling the "widget", they would then want to create a brand new "widget" and repeat the process. My question is, how do I completely (and safely) remove the managed bean from the session without disrupting the JSF lifecycle?

对于类似的问题,我还看到了其他答复,这些建议表明您可以使会话无效,也可以通过FacesContext访问HttpSession并以这种方式删除Bean.我不想用户必须注销或再次登录,并且我不想使整个会话无效.假设我需要通过FacesContext访问HttpSession并删除Bean,那么在JSF中,lifecyle是安全地执行此操作的最合适的位置,这样在整个周期的剩余时间内问题就不会消失了吗?我想确保当用户开始创建下一个窗口小部件"的过程时,JSF创建新会话bean不会有问题.

I have seen other responses for similar questions suggesting that you can invalidate the session, and also you can access the HttpSession through the FacesContext and remove the bean that way. I don't want the user to have to log out or in again, and I don't want to invalidate the entire session. Assuming that I need to go through FacesContext to access the HttpSession and remove the bean, when in the JSF lifecyle is the most appropriate place to do this safely so that problems don't cascade through the rest of the cycle? I want to make sure JSF will have no problems creating a new session bean when the user starts the process of creating the next "widget".

我也很好奇,为什么JSF中没有一种机制可以简化此过程.还有其他我应该采用的方法,该方法与预期的JSF模式更好吗?我不能在这里使用View Scope,因为托管bean在完成之前会经历几个不同的页面和视图.

I am also curious to understand why there isn't a mechanism in JSF to make this easier. Is there some other approach that I should be taking which is better in line with the intended JSF pattern? I can't use View Scope here because the managed bean will go through several different pages and views before it is completed.

预先感谢!

推荐答案

创建一个页面,您可以有条件地呈现多个向导步骤.

Create a single page wherein you render the multiple wizard steps conditionally.

<h:panelGroup rendered="#{wizard.step == 1}">
   <ui:include src="/WEB-INF/wizard/step1.xhtml" />
</h:panelGroup>
<h:panelGroup rendered="#{wizard.step == 2}">
   <ui:include src="/WEB-INF/wizard/step2.xhtml" />
</h:panelGroup>
<h:panelGroup rendered="#{wizard.step == 3}">
   <ui:include src="/WEB-INF/wizard/step3.xhtml" />
</h:panelGroup>

通过这种方式,您可以在单个页面上使用@ViewScoped托管bean,而不会带来很多麻烦.

This way you can just use a @ViewScoped managed bean on this single page without much hassle.

无关与具体问题 PrimeFaces 具有

Unrelated to the concrete question, PrimeFaces has a <p:wizard> component which does almost exactly like that. You may find it useful in order to save yourself from some painful boilerplate code as to validation and such.

这篇关于如何(以及何时?)在JSF 2.0中删除Session作用域的bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆