ManagedProperty SessionScope inside ViewScoped Bean - Transient?

I have a JSF Beans structure like this:

@ManagedBean @ViewScoped public class ViewBeany implements Serializable { .... @ManagedProperty(value='#{sessionBeany}) transient private SessionBeany sessionBeany; ... public getSessionBeany() { ... }; public setSessionBeany(SessionBeany sessionBeany) { ... }; } 

The reason for transient is that the bean session has some non-serializable elements and cannot be Serializable.

Will this work?
If not, how can I solve the problem of the inability to serialize SesionBeany , but save it as a managed property under the bean scope?

Thanks!

+6
source share
1 answer

This will not work. If the bean view is serialized, all transient fields are skipped. After deserialization, JSF does not restore managed properties, so you fall into the scope of a bean without the scope bean property, which only calls NPE.

In this particular design, it is best to introduce lazy loading into the getter and get the bean session by the receiver instead of directly accessing the field.

 private transient SessionBeany sessionBeany; public SessionBeany getSessionBeany() { // Method can be private. if (sessionBeany == null) { FacesContext context = FacesContext.getCurrentInstance(); sessionBeany = context.getApplication().evaluateExpressionGet(context, "#{sessionBeany}", SessionBeany.class); } return sessionBeany; } 
+12
source

All Articles