具有相同ViewScoped bean类的多个浏览器选项卡或窗口

问题描述 投票:2回答:1

使用Payara Server 4.1.2.174和mojarra 2.2.15。

我有一个简单的命名Bean,其范围为javax.faces.view.ViewScoped。

import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;


@Named
@ViewScoped
public class SimpleBean implements Serializable
{
    private final Logger logger = Logger.getLogger(SimpleBean.class.getName());

    @PostConstruct
    private void init()
    {
        logger.log(Level.SEVERE, "{0}.init()", this);
    }

    private String name;

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String action()
    {
        logger.log(Level.SEVERE, "{0}.action()", this);
        logger.log(Level.SEVERE,"====================");
        logger.log(Level.SEVERE, "name: {0}", getName());
        logger.log(Level.SEVERE,"====================");
        return "submit";
    }
}

所以我有一个简单的index.xhtml页面和表单。

<h:form>
  <h:inputText value="#{simpleBean.name}"></h:inputText>
  <h:link value="To submit" outcome="submit"/>
  <h:commandButton value="Welcome Me" action="#{simpleBean.action()}"/>
</h:form>

我可以在两个不同的浏览器选项卡或窗口中打开index.xhtml。所以,我有以下日志:

Severe: [email protected]()
Finest: Handling PostConstructViewMapEvent
Finest: Handling PreDestroyViewMapEvent
Finest: Destroying @viewscoped beans from view map: {simpleBean=solvo.ee.beans.SimpleBean@2adafd68}
Severe: [email protected]()
Finest: Handling PostConstructViewMapEvent
Finest: Handling PreDestroyViewMapEvent
Finest: Destroying @viewscoped beans from view map: {simpleBean=solvo.ee.beans.SimpleBean@49a86248}

我们可以看到SimpleBean有两个不同的实例。之后,我提交第一个标签的表单。

Severe: [email protected]()
Severe: ====================
Severe: name: First tab
Severe: ====================
Finest: Handling PreDestroyViewMapEvent
Finest: Destroying @viewscoped beans from view map: {simpleBean=solvo.ee.beans.SimpleBean@2adafd68}
Finest: Handling PreDestroyViewMapEvent
Finest: Destroying @viewscoped beans from view map: {}

如果我尝试提交第二个选项卡的表单,则不会使用存储的早期SimpleBean实例(solvo.ee.beans.SimpleBean@49a86248),而是ViewScopeContextManager将创建一个SimpleBean类的新实例,我们可以在日志:

Severe: [email protected]()
Severe: [email protected]()
Severe: ====================
Severe: name: Second tab
Severe: ====================
Finest: Handling PreDestroyViewMapEvent
Finest: Destroying @viewscoped beans from view map: {simpleBean=solvo.ee.beans.SimpleBean@4797f115}
Finest: Handling PreDestroyViewMapEvent
Finest: Destroying @viewscoped beans from view map: {}

我已经检查了com.sun.faces.application.view.ViewScopeContextManager.copyViewScopeContextsFromSession方法的代码,据我所知这种行为是正常的。但是,如果我在我的bean中存储请求参数或其他重要数据,我将丢失它,因为在提交第一个表单后实例将丢失。

有没有一个解决方案来保持bean主要与第二个标签相关联(在我的例子中是solvo.ee.beans.SimpleBean@49a86248)?

jsf glassfish jsf-2.2 mojarra view-scope
1个回答
0
投票

看来这是Mojarra的一个错误,根据这篇文章将在2.3.10修复:https://github.com/eclipse-ee4j/mojarra/issues/4509#issuecomment-453188481

在同一个帖子中,看起来Payara已经应用了补丁而没有等待2.3.10版本。升级到修补的Payara是否能为您解决问题?

© www.soinside.com 2019 - 2024. All rights reserved.