简体   繁体   中英

JSF 2.0 - ViewScope and SessionScope advantages?

It would be great if someone can help me understand, what's the advantage of ViewScoped bean when we have SessionBean injected into it.

Can we still save on session memory usage?

If we use only SessionScoped bean or ViewScoped bean with injected SessionBean, I believe there is no difference in session memory footprint.

Why do we go through so much hassles of using View and Session scoped beans when everything is achieved so smoothly with SessionScoped beans.

Thanks, Sundeep

It's a very common situation when you need to keep the data just for one page and then destroy it when you navigate to another page. That makes @ViewScoped bean a reasonable choice. @SessionScoped managed bean will keep all the data in session. So, why pollute your session map, when the data is no longer needed?

Also note, that the @ViewScoped annotation is not available in CDI. So if you're using beans with @Named annotation (rather than @ManagedBean ), then you're out of luck. However, there are some alternatives.

When you place everything in your session you are using more memory. The session usally expires after 35 minutes or your default.

Viewscoped beans are free for garbage collection after you switched the views.

For smaller objects it probably doesn't matter much in smaller applications. However if you for example store the return values from a database in your session you will have to care about the used up memory.

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