@ViewScoped bean recreated on every postback request when using JSF 2.2

I'm having an issue similar to this post and the answer from @BalusC with 3 solutions but:

  • I'm not using of the mentioned EL expressions
  • I don't want to go with the second solution (it's complex enough for me like this)
  • and partial state saving is set to false.

My code is as follows:

index.xhtml:

<?xml version="1.0" encoding="windows-1256" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>Insert title here</title>
    </h:head>
    <h:body>
        <h:form>
            <p:panelMenu id="westMenu">
                <p:submenu id="sub1" label="System Monitor">
                    <p:menuitem id="menu1" value="live monitoring" 
                            action="#{menusBean.activateMenu('sub1_menu1')}" 
                            update=":centerPane,westMenu" 
                            disabled="#{menusBean.active['sub1_menu1']}" />
                    <p:menuitem id="menu2" value="reports" 
                            action="#{menusBean.activateMenu('sub1_menu2')}"
                            update=":centerPane,westMenu" 
                            disabled="#{menusBean.active['sub1_menu2']}" />
                </p:submenu>
                <p:submenu id="sub2" label="Charging System Nodes" />
                <p:submenu id="sub3" label="Additional Nodes" />
            </p:panelMenu>
        </h:form>
        <h:panelGroup id="centerPane">
            ...
        </h:panelGroup>
    </h:body>
</html>

MenusBean.java:

package menus;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.view.ViewScoped;

@ManagedBean
@ViewScoped
public class MenusBean implements Serializable{

    private static final long serialVersionUID = -7793281454064343472L;
    private String mainPage="sub1_menu1";
    private Map<String, Boolean> active;

    public MenusBean(){
        System.out.println("MenusBean created");
        active = new HashMap<>();
        active.put(mainPage, true);
        active.put("sub1_menu2", false);
    }

    public boolean activateMenu(String page){
        active.put(mainPage, false);
        active.put(page, true);     
        mainPage = page;
        for (Map.Entry<String, Boolean> e : active.entrySet())
            System.out.println(e.getKey()+":"+e.getValue());

        return true;
    }

    public Map<String, Boolean> getActive() {
        return active;
    }
}

When executed, I get:

MenusBean created
MenusBean created
MenusBean created

How is this caused and how can I solve it?


Solution 1:

This,

import javax.faces.view.ViewScoped;

is the JSF 2.2-introduced CDI-specific annotation, intented to be used in combination with CDI-specific bean management annotation @Named.

However, you're using the JSF-specific bean management annotation @ManagedBean.

import javax.faces.bean.ManagedBean;

You should then be using any of the scopes provided by the very same javax.faces.bean package instead. The right @ViewScoped is over there:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class MenusBean implements Serializable{

If you use the wrong combination, the bean behaves as a @RequestScoped bean and be recreated on each call.

Alternatively, if your environment supports CDI (GlassFish/JBoss/TomEE with Weld, OpenWebBeans, etc), then you could also replace @ManagedBean by @Named:

import javax.inject.Named;
import javax.faces.view.ViewScoped;

@Named
@ViewScoped
public class MenusBean implements Serializable{

It's recommended to move to CDI. The JSF-specific bean management annotations are candidate for deprecation in future JSF / Java EE versions as everything is slowly moving/unifying towards CDI.