12

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?

Community
  • 1
  • 1
Rima
  • 457
  • 2
  • 7
  • 18

1 Answers1

42

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.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • ok awsome, this solves the problem, but only partially :( , after changing the annotation and load the page, it works fine, but when I go to the "reports" menu it starts with creating again and again, and when I go back to "live monitoring" menu it creates one more and stops. any ideas ? – Rima Nov 27 '13 at 19:52
  • 2
    Well, this is awkward. Is the bean now managed by JSF or CDI? Have you tried the other? Are you absolutely positive that rebuild/redeploy is successful and that browser cache is clean and crisp? (Ctrl+Shift+N in Chrome). – BalusC Nov 27 '13 at 20:07
  • I honestly don't know what a CDI is, but I don't have a package javax.inject, so I suppose it's managed by JSF (sorry total noob here). [here](http://speedy.sh/zEdjF/primefaces-test-3.zip) is my project ; I cleared the cache in FF and tried with eclipse built-in browser, they both give the same result. – Rima Nov 27 '13 at 20:27
  • Okay. Given that you don't have CDI at hands, may I assume that you're using Tomcat as server? – BalusC Nov 27 '13 at 20:31
  • Sorry, can't reproduce it on Tomcat 7.0.47 with Mojarra 2.2.4 and the exact code as posted in the question in its current form. Are you familiar with basic HTTP as to passing around request parameters, cookies, etc? Press F12 in Chrome and click *Network* tab to see it. It should give clues as to the problem (e.g. a missing session cookie). – BalusC Nov 27 '13 at 20:43
  • I don't have chrome installed (I'm installing it), but using firebug in firefox, I could see that after clicking the "reports" menu and the new page is displayed, the p:poll is still sending ajax requests, and with each request, two new beans are created, does that make any sens ? I mean why is the poll still working after the it's view has disappeard ? – Rima Nov 27 '13 at 21:02
  • confirmed, it's the poll who is causing the issue, after removing it everything is fine. what's wrong with it ? – Rima Nov 27 '13 at 21:05
  • I tried chrome/tomcat7.0.47 without success, I tried also to play with poll parameters "rendered" and "stop" but still the poll is firing the requests even after the component is not rendered. how can I fix this ? – Rima Nov 28 '13 at 08:34
  • I've posted the same problem in the primefaces [forums](http://forum.primefaces.org/viewtopic.php?f=3&t=35186) but didn't get a solution to the problem ! – Rima Dec 10 '13 at 09:24