具有相同ViewScoped bean类

时间:2017-12-20 13:25:12

标签: jsf glassfish jsf-2.2 mojarra view-scope

将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: solvo.ee.beans.SimpleBean@2adafd68.init()
Finest: Handling PostConstructViewMapEvent
Finest: Handling PreDestroyViewMapEvent
Finest: Destroying @viewscoped beans from view map: {simpleBean=solvo.ee.beans.SimpleBean@2adafd68}
Severe: solvo.ee.beans.SimpleBean@49a86248.init()
Finest: Handling PostConstructViewMapEvent
Finest: Handling PreDestroyViewMapEvent
Finest: Destroying @viewscoped beans from view map: {simpleBean=solvo.ee.beans.SimpleBean@49a86248}

正如我们所看到的,SimpleBean有两个不同的实例。之后,我提交了第一个标签的表格。

Severe: solvo.ee.beans.SimpleBean@2adafd68.action()
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: solvo.ee.beans.SimpleBean@4797f115.init()
Severe: solvo.ee.beans.SimpleBean@4797f115.action()
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)?

1 个答案:

答案 0 :(得分:0)

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

在同一线程中,Payara似乎已应用补丁,而没有等待2.3.10版本。升级到修补的Payara是否可以为您解决问题?