【问题标题】:Viewscoped bean states preserved differently in Mojarra and MyFaces在 Mojarra 和 MyFaces 中以不同方式保存的 Viewscoped bean 状态
【发布时间】:2013-12-10 04:26:10
【问题描述】:

我有一个观点,你可以开始一个昂贵的过程。视图与 @ViewScoped bean 配对,如果进程已启动,则使用 PrimeFaces 定期检查状态。我有以下Runnable 调用具有昂贵操作的网络服务。

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 上显示它。

我的问题是,使用 Mojarra 2.1.6 和 2.1.26 可以正常工作,但使用 MyFaces 2.1.13(我更愿意使用)generateError 变量在 bean 中永远不会是 true,尽管我可以看到在调试中调用了setGenerateError(true)。与视图一起使用的实际 bean 与可从线程访问的 bean 实例不同。我实际上可以在调试中看到这一点:使用 MyFaces,每个轮询请求都会生成一个新的视图范围的 bean 实例,而使用 Mojarra 它始终是同一个实例。

MyFaces 中是否有我缺少的 <context-param /> 设置?根据规范,哪一个实际上是正确的行为?

【问题讨论】:

    标签: java multithreading jsf myfaces mojarra


    【解决方案1】:

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-29
      • 2013-08-27
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 2015-08-03
      相关资源
      最近更新 更多