Destroying view-scoped beans when session ends

两盒软妹~` 提交于 2019-12-05 02:50:30

问题


My question is related to this one (and probably others):

@PreDestroy never called on @ViewScoped

As stated there, there's no trivial solution to either have view-scoped beans destroyed upon navigation and the same seems to hold true for when the session expires.

What would a non-trivial approach to release (calling the @PreDestroy method) JSF view-scoped beans look like, or more specifically as soon as the session expires?

I'm using Java EE 6 and Mojarra 2.1.x on GlassFish 3.1.2.


回答1:


Create a @SessionScoped bean to hold the resources (in some collection/array?) and inject it in the @ViewScoped bean and then rely on the @PreDestroy of the session scoped bean.

True, this way the resources live a little longer than you want, but that's the most easy and reliable solution you can get. If you want to keep the @PreDestroy in the view scoped bean, then you'd need to somehow make sure that the enduser always performs navigation by a HTTP POST request on exactly this view scoped bean. You can't reliably guarantee that (the enduser's PC might crash and so on).



来源:https://stackoverflow.com/questions/13082379/destroying-view-scoped-beans-when-session-ends

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!