Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

jsf 2 - How and when is a @ViewScoped bean destroyed in JSF?

The lifecycle of the @RequestScoped and @SessionScopedBean managed beans are managed by the Servlet container itself since they are basically stored as an attribute of HttpRequest and HttpSession respectively. How do JSF manage the lifecycle of the @ViewScopedBean ? I know it gets created when the view is created and is usable till there is a postback to a different view. But I found out that is not garbage collected immediately after we move from that view.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

It will be destroyed when

  • a postback with a non-null outcome is been performed,

  • or, the number of (logical) views in session has exceeded and the particular view is the first one in LRU chain (in Mojarra, that's configureable by com.sun.faces.numberOfViewsInSession and com.sun.faces.numberOfLogicalViews context parameters, each with a default value of 15),

  • or, the number of actieve view scopes in session has exceeded (in Mojarra, that's a hardcoded limit of 25), see also JSF 2.2 Memory Consumption: Why does Mojarra keep the ViewScoped Beans of the last 25 Views in Memory?

  • or, the session has expired.

It will thus not be destroyed when the page is unloaded as result of clicking a GET link to another page, or refreshing the page, or closing the browser tab/window. The bean will live as long until one of abovelisted conditions is met. To destroy it during unload anyway, consider using OmniFaces @ViewScoped instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...