简体   繁体   中英

JSF View Scope: How to Check if an object is in the view tree

I use Spring + JSF, with the View Scope being managed by Spring. I've noticed that on every request, the view is destroyed and created again, and so any @PostConstruct method are called on every request.

In most pages this is ok, since in that method are just some object initializations (mainly new calls).

But in others pages this is a problem because I have to make heavy queries to initialize some lists, and the view behavior calls the initialization method on every request... so any request in the page is veeeeeery slow.

I know that the view scope stores the bean and it's objects in the session and later recover them; so I want to know if there's a way to check if these objects are already stored so I don't need to initialize those heavy objects at every request; just get them from the session.

UPDATE

The view scope used is the one implemented here: http://comdynamics.net/blog/109/spring3-jsf2-view-scope/

You should definitely put your Query-heavy processes into a SessionScoped Bean.

Then You can use a reference from the ViewScoped beans to the SessionScopedBean, by using @ManagedProperty

Additionally, you can alter your html code to reference the sessionScoped bean properties directly.

If the query, or tree initialization has to be reset after a specific Action (eg when you are deleting or adding objects that affect the Tree), or After visiting a Page, then you could move your initialization code into a 'refresh' function. By any means, avoid heavy initializations , queries and large Data handling, in the PostConstruct of ViewScoped or RequestScoped Beans

You need to move data that can be reused on these pages to a Session scoped bean and put a method on it that will allow you to reload the data if necessary. Then get the data for your View scoped beans from the Session bean. By going this route, you should only be retrieving the data from the database when you need fresh data by calling your reload method on the session bean.

I really don't see another solution to your problem. If you want to persist data beyond the view scope, you either need a session scoped bean or a singleton. Since you should not store session specific data in a singleton bean, you are left with using a session bean.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM