How and when is the bean scope removed from the LRU cache in the JSF?

I read a great answer when the bean scope is destroyed. (see How and when did the @ViewScoped bean be destroyed in JSF? ), and I automatically assumed that the destroyed bean was also removed from the scope cache. But I could see that the bean is still in the cache, so I would like to know if I should also remove the remote bean view from the cache of the LRU scope?

In our application, we open all the details in the sepeare bookmarks / windows. After some opening / closing (depending on numberOfViewsInSession), we could see a ViewExpiredException in the case when the first detailed window is still open and the user opens and closes other detail windows, and after a while he wants to perform some operation in the first window. I did some debugging, and I see that the private view was not removed from the LRU cache.

So, is this the expected behavior or is there something wrong in my code? And if this is the expected behavior, is there any useful strategy how to work with multitabs / multiwindow without a lot of ViewExpiredException caused by the LRU cache ?. I know I can change numberOfViewsInSession, but this is the last choice I want to use.

I prepared a simple test file, and when I open / close view.xhtml more times, I see that LRUMap is growing.

Environment: JDK7, mojarra 2.2.4, tomcat 7.0.47

Thank you in advance

View.xhtml

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html">
    <head>
        <title>View Bean</title>
        <meta charset="UTF-8"/>
    </head>
    <body>
        <h:form id="viewForm">
        <div>#{viewBean.text}</div>
        <h:commandButton id="closeButton" value="Close" action="/ClosePage.xhtml"/>
        </h:form>
    </body>
</html>

index.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Session bean</title>
    </h:head>
    <h:body>
        <h:form id="sessionForm">
            <h:outputText value="#{sessionBean.text}"/>
            <br/>
            <h:link id="linkView" value="Open view.xhmtl" outcome="/view.xhtml" target="_blank"/>
        </h:form>
    </h:body>
</html>

ClosePage.xhmtl

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">               
    <head><title>Window will be closed</title></head>
    <body>
        <script>window.close();</script>
    </body>
</html>

ViewBean.java

package com.mycompany.mavenproject2;

import com.sun.faces.util.LRUMap;
import java.io.Serializable;
import java.util.Map;
import javax.annotation.PreDestroy;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name = "viewBean")
@ViewScoped
public class ViewBean implements Serializable {

    private static final long serialVersionUID = 13920902390329L;

    private int lruMapSize;

    /**
     * Creates a new instance of ViewBean
     */
    public ViewBean() {

        Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        LRUMap<String, LRUMap> lruMap = (LRUMap) sessionMap.get("com.sun.faces.renderkit.ServerSideStateHelper.LogicalViewMap");
        lruMapSize = lruMap == null ? 0 : lruMap.size();
    }

    @PreDestroy
    void destroyed() {
        System.out.println("View bean destroyed");
    }

    public String getText() {
        return "ViewBean LRU cache size:" + Integer.toString(lruMapSize);
    }

} 

SessionBean.java

package com.mycompany.mavenproject2;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "sessionBean")
@SessionScoped
public class SessionBean implements Serializable {
    private static final long serialVersionUID = 1777489347L;

    /**
     * Creates a new instance of SessionBean
     */
    public SessionBean() {
    }

    public String getText() {
        return "Session bean text";
    }

}
+4
source share
1 answer

, JSF . , - , ViewScoped bean, , bean . JSF LRU, , , .

, , ViewScoped bean, , bean. , , , .

, , - javascript ajax ViewScoped bean. ( 30 .) ViewScoped bean (s), , LRU, , .

, primface, , ViewScoped beans. , .

+1

All Articles