Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
891 views
in Technique[技术] by (71.8m points)

jsf 2 - How (and when?) to remove a Session scoped bean in JSF 2.0

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?

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".

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.

Thanks in Advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

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


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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...