Várias guias ou janelas do navegador com a mesma classe de bean ViewScoped
Usando o Payara Server 4.1.2.174 com o mojarra 2.2.15.
Eu tenho um Bean nomeado simples com escopo 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";
}
}
Então, eu tenho uma página index.xhtml simples com o formulário.
<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>
Posso abrir o index.xhtml em duas guias ou janelas diferentes do navegador. Então, eu tenho o seguinte log:
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}
Como podemos ver, existem duas instâncias diferentes do SimpleBean. Depois disso, envio o formulário da primeira guia.
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: {}
Se eu tentar enviar o formulário da segunda guia, a instância anterior armazenada do SimpleBean (solvo.ee.beans.SimpleBean@49a86248) não será usada. Em vez disso, o ViewScopeContextManager criará uma nova instância da classe SimpleBean, como podemos ver em registro:
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: {}
Verifiquei o código do método com.sun.faces.application.view.ViewScopeContextManager.copyViewScopeContextsFromSession e, pelo que entendi, esse comportamento é normal. No entanto, se eu armazenar parâmetros de solicitação ou outros dados importantes no meu bean, eu os perderei porque a instância será perdida após o envio do primeiro formulário.
Existe uma solução para manter o bean associado principalmente à segunda guia (no meu exemplo solvo.ee.beans.SimpleBean@49a86248)?