We have the following problem with JSF's @ViewScoped
and @ManagedProperty
: we have ManagedBean
s that basically look as follows:
@ManagedBean
@SessionScope
public class SessionConfig implements Serializable
{
// ...
}
and
@ManagedBean
@ViewScope
public class SomeController implements Serializable
{
@ManagedProperty( value="#{sessionConfig}" )
private SessionConfig sessionConfig;
// public getter and setter
// ...
}
The controller is serialized after the request has been processed, as expected.
I expected that the @ManagedProperty
sessionConfig
would be handled specially in serialization, in particular, that it will be "relinked" after deserialization. However, it turns out that after deserialization sessionConfig
is merely a stale clone of the actual SessionConfig-Bean.
Questions:
- Is this the expected behavior?
- What can we do to make JSF re-evaluate the
@ManagedProperty
after deserialization?
Currently, we "manually" re-evaluate all managed properties after deserialzation. It works but, clearly does not seem right.
Thank you!
A solution is to avoid @ManagedProperty in @ViewScoped beans and evaluate the EL expression on demand. @ViewScoped beans will be stored in Session (and thus serialized) at the end of every request.
FacesContext ctx = FacesContext.getCurrentInstance();
ctx.getApplication().evaluateExpressionGet(ctx, "#{sessionConfig}", SessionConfig.class)
Note that this may seriously affect perfromance if the expression has to be evaluated frequently.
A better aproach might be to provide custom serialization methods as described here: http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html This way the expression could be resolved automatically every time the bean is deserialized.
来源:https://stackoverflow.com/questions/11141052/serialization-of-managedproperty