简体   繁体   English

JSF View Scope:如何检查对象是否在视图树中

[英]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. 我使用Spring + JSF,而View Scope由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. 我注意到在每个请求上,视图都被销毁并再次创建,因此在每个请求上都会调用任何@PostConstruct方法。

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; 我知道视图范围将bean及其对象存储在会话中,并在以后恢复它们。 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/ 使用的视图范围是在此处实现的视图范围: http : //comdynamics.net/blog/109/spring3-jsf2-view-scope/

You should definitely put your Query-heavy processes into a SessionScoped Bean. 您绝对应该将重查询过程放入SessionScoped Bean中。

Then You can use a reference from the ViewScoped beans to the SessionScopedBean, by using @ManagedProperty 然后,可以通过使用@ManagedProperty使用从ViewScoped Bean到SessionScopedBean的引用

Additionally, you can alter your html code to reference the sessionScoped bean properties directly. 另外,您可以更改HTML代码以直接引用sessionScoped bean属性。

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 无论如何,在ViewScoped或RequestScoped Bean的PostConstruct中避免繁重的初始化,查询和大数据处理

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. 您需要将可在这些页面上重用的数据移动到Session范围的Bean上,并在其上放置一个方法,该方法将允许您在必要时重新加载数据。 Then get the data for your View scoped beans from the Session bean. 然后从Session bean获取View范围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. 通过这种方法,您仅应在需要新数据时通过在会话Bean上调用reload方法从数据库中检索数据。

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. 如果要在视图范围之外持久保存数据,则需要会话范围的bean或单例。 Since you should not store session specific data in a singleton bean, you are left with using a session bean. 由于不应将特定于会话的数据存储在单例Bean中,因此只能使用会话Bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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