2013-11-25 27 views
3

我有一个视图,可以开始一个昂贵的过程。该视图与@ViewScoped bean配对,如果该进程已启动,则会使用PrimeFaces定期检查状态。我有以下Runnable调用一个昂贵的操作web服务。在Mojarra和MyFaces中,Viewscoped bean状态的保存方式不同

public class Generator extends AsyncCaller { 

    private Viewer bean; 
    private String id; 

    public Generator(Viewer bean, Client client, String id) { 
     super(client); 
     this.bean = bean; 
     this.id = id; 
    } 

    @Override 
    public void run() { 
     ClientResponse response = getClient().generate(this.id); 

     boolean error = 
(response.getClientResponseStatus().getFamily() != Family.SUCCESSFUL); 
     if (error) { 
      Exception e = new UniformInterfaceException(response); 
      this.bean.setGenerateException(e); 
     } 
     this.bean.setGenerateError(error); 
    } 

} 

我开始这个Runnable作为单独Thread,所以UI不会被阻断。正如你所看到的,我将视图范围的管理bean实例传递给Runnable,所以如果有错误,我可以在bean中设置它,然后在UI上显示它。

我的问题是,随着钻嘴鱼科2.1.6和2.1.26这工作得很好,但MyFaces的2.1.13 - 我宁愿使用 - 该generateError变量是从未在豆true,虽然我可以看到setGenerateError(true)调用调试。与视图一起使用的实际bean与可从该线程访问的bean实例不同。实际上,我可以在调试中看到这一点:对于MyFaces,每个轮询请求都会生成一个新的视图范围的bean实例,而对于Mojarra,它始终是同一个实例。

在MyFaces中是否存在我缺少的<context-param />设置?哪一个实际上是按照规范的正确行为?

回答

2

发现在OmniFaces showcase's web.xml答案:

<context-param> 
    <!-- MyFaces and Mojarra don't agree on the default setting for actually 
     serializing state in the session as opposed to just storing a reference. 
     Mojarra's default is false, but can be switched to true. MyFaces' default 
     is true, and can be switched to false, which we thus do below. See http://arjan-tijms.omnifaces.org/p/jsf-22.html#1127 --> 
    <param-name>org.apache.myfaces.SERIALIZE_STATE_IN_SESSION</param-name> 
    <param-value>false</param-value> 
</context-param>