2

@ViewScopedJSF和存在以下问题@ManagedProperty:我们的ManagedBeans 基本上如下所示:

@ManagedBean
@SessionScope
public class SessionConfig implements Serializable
{
    // ...
}

@ManagedBean
@ViewScope
public class SomeController implements Serializable
{
    @ManagedProperty( value="#{sessionConfig}" )
    private SessionConfig sessionConfig;
    // public getter and setter

    // ...
}

正如预期的那样,在处理请求后,控制器将被序列化。我希望@ManagedProperty sessionConfig在序列化中特别处理它,特别是在反序列化后它将被“重新链接”。然而,事实证明,反序列化后sessionConfig只是实际 SessionConfig-Bean 的过时克隆。

问题:

  1. 这是预期的行为吗?
  2. 我们可以做些什么来让 JSF 重新评估@ManagedProperty反序列化后的结果?

目前,我们在反序列化后“手动”重新评估所有托管属性。它有效,但显然似乎不正确。

谢谢!

4

1 回答 1

0

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.

于 2012-07-19T10:30:51.163 回答