title: Unexpected survival of a @ViewScoped bean
body: <p>I'm developing a <strong>view</strong> which consists of an object creation form and the existing object list, <em>creablocco.xhtml</em>: </p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
  &lt;ui:composition xmlns="http://www.w3.org/1999/xhtml"
                  xmlns:ui="http://java.sun.com/jsf/facelets"
                  xmlns:ace="http://www.icefaces.org/icefaces/components" 
                  xmlns:ice="http://www.icesoft.com/icefaces/component"
                  xmlns:h="http://java.sun.com/jsf/html"
                  xmlns:f="http://java.sun.com/jsf/core"
                  template="/resources/includes/templates/template.xhtml"&gt;
      &lt;ui:param name="pageTitle" value="Crea blocco" /&gt;
      &lt;ui:define name="page-content"&gt;
          &lt;ace:panel id="personalInfoInputPanel" header="Crea blocco"&gt;
              &lt;div class="div_creablocco"&gt;
                  &lt;h:form id="creabloccoform"&gt;
                      &lt;h:panelGrid id="creabloccoInputGrid" columns="1" styleClass="table_creablocco"&gt;
                          &lt;ace:textEntry id="numeroFile" value="#{creaBloccoBean.numeroFile}" label="Numero file:" labelPosition="left" required="true" requiredIndicator="(*)" indicatorPosition="right"&gt;
                          &lt;/ace:textEntry&gt;
                          &lt;ace:textEntry id="numeroColonne" value="#{creaBloccoBean.numeroColonne}" label="Numero colonne:" labelPosition="left" required="true" requiredIndicator="(*)" indicatorPosition="right"&gt;
                          &lt;/ace:textEntry&gt;
                          &lt;ace:textEntry id="posizioneX" value="#{creaBloccoBean.posizioneX}" label="Coordinata X:" labelPosition="left" required="true" requiredIndicator="(*)" indicatorPosition="right"&gt;
                          &lt;/ace:textEntry&gt;
                          &lt;ace:textEntry id="posizioneY" value="#{creaBloccoBean.posizioneY}" label="Coordinata Y:" labelPosition="left" required="true" requiredIndicator="(*)" indicatorPosition="right"&gt;
                          &lt;/ace:textEntry&gt;
                          &lt;ace:pushButton id="pushBttnCreaBlocco"  value="Crea blocco" action="creaBlocco"/&gt;
                      &lt;/h:panelGrid&gt;
                  &lt;/h:form&gt;
              &lt;/div&gt;
          &lt;/ace:panel&gt;
  
          &lt;ace:panel id="blocchiPanel" header="Blocchi"&gt;
              &lt;h:form id="blocchiTableForm"&gt;
                  &lt;ace:menuBar id="iconeBloccoMenu" rendered="#{!blocchiBean.modificaInCorso}"&gt;
                      &lt;ace:menuItem id="dettagliBloccoButton" value="Dettagli" action="dettagliBlocco" icon="ui-icon ui-icon-search" /&gt;
                      &lt;ace:menuItem id="modificaBloccoButton" value="Modifica" action="modificaBlocco" icon="ui-icon ui-icon-pencil" /&gt;
                      &lt;ace:menuItem id="cancellaBloccoButton" value="Cancella" action="cancellaBlocco" icon="ui-icon ui-icon-closethick" /&gt;
                  &lt;/ace:menuBar&gt;
                  &lt;ace:menuBar id="iconeModificaBloccoMenu" rendered="#{blocchiBean.modificaInCorso}"&gt;
                      &lt;ace:menuItem id="annullaModificheButton" value="Annulla" action="annullaModificheBlocco" icon="ui-icon ui-icon-arrow-1-w" /&gt;
                      &lt;ace:menuItem id="salvaModifiche" value="Salva" action="salvaModificheBlocco" icon="ui-icon ui-icon-disk" /&gt;
                  &lt;/ace:menuBar&gt;
                  &lt;ace:dataTable id="blocchiTable" value="#{blocchi}"
                      binding="#{blocchiBean.table}" stateMap="#{blocchiBean.stateMap}"
                      selectionMode="multiple" doubleClickSelect="false"
                      rowSelectListener="#{blocchiBean.selectListener}"
                      rowUnselectListener="#{blocchiBean.deselectListener}" var="blocco"
                      lazy="true" rows="10" paginator="true" paginatorPosition="bottom"&gt;
                      &lt;ace:column id="idBloccoColumn" headerText="ID" styleClass="id"&gt;
                          &lt;h:outputText id="idBloccoText" value="#{blocco.id}" /&gt;
                      &lt;/ace:column&gt;
                      &lt;ace:column id="numeroFileBloccoColumn" headerText="Numero file"
                          styleClass="numFile"&gt;
                          &lt;ace:cellEditor&gt;
                              &lt;f:facet name="output"&gt;
                                  &lt;h:outputText id="numeroFileBloccoText"
                                      value="#{blocco.numeroFile}" /&gt;
                              &lt;/f:facet&gt;
                              &lt;f:facet name="input"&gt;
                                  &lt;h:inputText id="numeroFileBloccoInput"
                                      value="#{blocco.numeroFile}" /&gt;
                              &lt;/f:facet&gt;
                          &lt;/ace:cellEditor&gt;
  
                      &lt;/ace:column&gt;
                      &lt;ace:column id="numeroColonneBloccoColumn"
                          headerText="Numero colonne" styleClass="numColonne"&gt;
                          &lt;ace:cellEditor&gt;
                              &lt;f:facet name="output"&gt;
                                  &lt;h:outputText id="numeroColonneBloccoText"
                                      value="#{blocco.numeroColonne}" /&gt;
                              &lt;/f:facet&gt;
                              &lt;f:facet name="input"&gt;
                                  &lt;h:inputText id="numeroColonneBloccoInput"
                                      value="#{blocco.numeroColonne}" /&gt;
                              &lt;/f:facet&gt;
                          &lt;/ace:cellEditor&gt;
  
                      &lt;/ace:column&gt;
                      &lt;ace:column id="posizioneXBloccoColumn" headerText="Posizione X"
                          styleClass="posX"&gt;
                          &lt;ace:cellEditor&gt;
                              &lt;f:facet name="output"&gt;
                                  &lt;h:outputText id="posizioneXBloccoText"
                                      value="#{blocco.posizioneX}" /&gt;
                              &lt;/f:facet&gt;
                              &lt;f:facet name="input"&gt;
                                  &lt;h:inputText id="posizioneXBloccoInput"
                                      value="#{blocco.posizioneX}" /&gt;
                              &lt;/f:facet&gt;
                          &lt;/ace:cellEditor&gt;
  
                      &lt;/ace:column&gt;
                      &lt;ace:column id="posizioneYBloccoColumn" headerText="Posizione Y"
                          styleClass="posY"&gt;
                          &lt;ace:cellEditor&gt;
                              &lt;f:facet name="output"&gt;
                                  &lt;h:outputText id="posizioneYBloccoText"
                                      value="#{blocco.posizioneY}" /&gt;
                              &lt;/f:facet&gt;
                              &lt;f:facet name="input"&gt;
                                  &lt;h:inputText id="posizioneYBloccoInput"
                                      value="#{blocco.posizioneY}" /&gt;
                              &lt;/f:facet&gt;
                          &lt;/ace:cellEditor&gt;
  
                      &lt;/ace:column&gt;
                  &lt;/ace:dataTable&gt;
              &lt;/h:form&gt;
          &lt;/ace:panel&gt;
      &lt;/ui:define&gt;
  &lt;/ui:composition&gt;
  </code></pre>
  
  <p>The "object creation form" (<code>&lt;h:form id="creabloccoform"&gt;</code>) is made up of text inputs and a button (<code>&lt;ace:pushButton id="pushBttnCreaBlocco" ... /&gt;</code>).<br>
  The "existing object list" is a table (<code>&lt;ace:dataTable id="blocchiTable" .. /&gt;</code>) and serves as a starting point to edit the selected object attributes or to delete the selected objects.  </p>
  
  <p>The removal and the editing process is accomplished by selecting some rows in the table and pressing the proper button in a menu bar (look at <code>&lt;ace:menuBar id="iconeBloccoMenu" .. /&gt;</code> and <code>&lt;ace:menuBar id="iconeModificaBloccoMenu" ... /&gt;</code>).</p>
  
  <p>This is the <strong>flow</strong> defined using Spring Webflow, <em>creablocco-flow.xml</em>:  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;flow xmlns="http://www.springframework.org/schema/webflow"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/webflow
          http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd"&gt;
  
      &lt;var name="creaBloccoBean" class="com.infoone.siglo.viewbeans.CreaBloccoBean" /&gt;
  
      &lt;view-state id="creablocco"&gt;
          &lt;on-entry&gt;
              &lt;evaluate expression="blocchiBean.getBlocchi()" result="flowScope.blocchi" /&gt;
          &lt;/on-entry&gt;
          &lt;transition on="creaBlocco" to="fine"&gt;
              &lt;evaluate expression="gestoreBlocchiCtr.creaBlocco(creaBloccoBean)" /&gt;
          &lt;/transition&gt;
          &lt;transition on="cancellaBlocco" to="decidiCancellazione"&gt;
              &lt;evaluate expression="blocchiBean.getRigheSelezionate()" result="flowScope.righeSelezionate"/&gt;
          &lt;/transition&gt;
          &lt;transition on="modificaBlocco" to="decidiModifica"&gt;
              &lt;evaluate expression="blocchiBean.getRigheSelezionate()" result="flowScope.righeSelezionate"/&gt;
          &lt;/transition&gt;
      &lt;/view-state&gt;
  
      &lt;decision-state id="decidiCancellazione"&gt;
          &lt;if test="!(flowScope.righeSelezionate).isEmpty()" then="cancellaBlocco" else="fine" /&gt; 
      &lt;/decision-state&gt;
      &lt;decision-state id="decidiModifica"&gt;
          &lt;if test="!(flowScope.righeSelezionate).isEmpty()" then="modificaBlocco" else="fine" /&gt; 
      &lt;/decision-state&gt;
  
      &lt;action-state id="cancellaBlocco"&gt;
          &lt;evaluate expression="gestoreBlocchiCtr.rimuoviBlocchi(flowScope.righeSelezionate)" /&gt;
          &lt;transition to="fine"/&gt;
      &lt;/action-state&gt;
      &lt;action-state id="modificaBlocco"&gt;
          &lt;evaluate expression="blocchiBean.abilitaModifica()"/&gt;
          &lt;transition to="fine"/&gt;
      &lt;/action-state&gt;
  
      &lt;end-state id="fine" /&gt;
  
  &lt;/flow&gt;
  </code></pre>
  
  <p>This is the <strong>bean</strong> which is causing the problem mentioned in the title, <em>BlocchiBean.java</em>:  </p>
  
  <pre><code>package com.infoone.siglo.viewbeans;
  
  import java.io.Serializable;
  import java.util.Arrays;
  import java.util.List;
  import java.util.Set;
  import java.util.concurrent.ConcurrentSkipListSet;
  
  import javax.annotation.PostConstruct;
  import javax.faces.bean.ViewScoped;
  
  import org.icefaces.ace.component.column.Column;
  import org.icefaces.ace.component.datatable.DataTable;
  import org.icefaces.ace.event.SelectEvent;
  import org.icefaces.ace.event.UnselectEvent;
  import org.icefaces.ace.model.table.RowState;
  import org.icefaces.ace.model.table.RowStateMap;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Component;
  
  import com.infoone.siglo.viewbeans.lazyLoaders.BlocchiLazyLoader;
  
  @Component
  @ViewScoped
  public class BlocchiBean implements Serializable {
  
      /**
       * 
       */
      private static final long serialVersionUID = -7007046069743684498L;
  
      @Autowired
      private transient BlocchiLazyLoader blocchi;
      private DataTable table;
      private RowStateMap stateMap;
      private Set&lt;CreaBloccoBean&gt; righeSelezionate;
      private boolean modificaInCorso;
  
      public BlocchiBean()
      {
      }
  
      @SuppressWarnings("unused")
      @PostConstruct
      private void initBlocchi()
      {
          stateMap = new RowStateMap();
          stateMap.setAllSelectable(true);
  
          righeSelezionate = new ConcurrentSkipListSet&lt;CreaBloccoBean&gt;();
  
          blocchi.setRowCount(blocchi.getPageSize());
  
          modificaInCorso = false;
      }
  
      //BlocchiLazyLoader blocchi
      public void setBlocchi(BlocchiLazyLoader blocchi) {
          this.blocchi = blocchi;
      }   
      public BlocchiLazyLoader getBlocchi() {
          return blocchi;
      }
  
      //RowStateMap stateMap
      public RowStateMap getStateMap() {
          return stateMap;
      }
      public void setStateMap(RowStateMap stateMap) {
          this.stateMap = stateMap;
      }
  
      //DataTable table
      public DataTable getTable() {
          return table;
      }
      public void setTable(DataTable table) {
          this.table = table;
      }
  
      //Selection listeners
      public void selectListener(SelectEvent event) {
          Object[] oggettiSelezionati = event.getObjects();
          for (Object oggettoSelezionato : oggettiSelezionati) {
              CreaBloccoBean bloccoSelezionato = (CreaBloccoBean) oggettoSelezionato;
              righeSelezionate.add(bloccoSelezionato);
          }
      }
  
      public void deselectListener(UnselectEvent event) {
          Object oggettoDeselezionato = event.getObject();
          CreaBloccoBean bloccoDeselezionato = (CreaBloccoBean) oggettoDeselezionato;
          righeSelezionate.remove(bloccoDeselezionato);
      }
  
      //List&lt;CreaBloccoBean&gt; righeSelezionate
      public List&lt;CreaBloccoBean&gt; getRigheSelezionate() {
          CreaBloccoBean[] array = new CreaBloccoBean[righeSelezionate.size()];
          array = righeSelezionate.toArray(array);
          return Arrays.asList(array);
      }
      public void setRigheSelezionate(List&lt;CreaBloccoBean&gt; righeSelezionate) {
          this.righeSelezionate.clear();
          this.righeSelezionate.addAll(righeSelezionate);
      }
  
      public boolean isModificaInCorso() {
          return modificaInCorso;
      }
  
      public void setModificaInCorso(boolean modificaInCorso) {
          this.modificaInCorso = modificaInCorso;
      }
  
      //Abilitazione dell'editing
      public void abilitaModifica() {
          if (!righeSelezionate.isEmpty())
          {
              modificaInCorso = true;
              List&lt;Column&gt; colonne = table.getColumns();
              for (Object oggettoCorrente : stateMap.getSelected()) {
                  CreaBloccoBean beanCorrente = (CreaBloccoBean) oggettoCorrente;
                  RowState statoCorrente = stateMap.get(beanCorrente);
                  if (righeSelezionate.contains(beanCorrente)) {
                      for (Column colonna : colonne) {
                          statoCorrente.addActiveCellEditor(colonna.getCellEditor());
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Now, let's assume I'm accessing the flow using the following URL: <code>http://&lt;project base URL&gt;/&lt;flow path&gt;?execution=&lt;execution key 1&gt;</code> and I select one of the rows. Obviously, the <em>stateMap</em> and <em>righeSelezionate</em> attributes of the <em>BlocchiBean</em> object will be properly update by the underlying engine (the latter one through the (de)selection listeners). After that, I click on the <em>Modifica</em> button (the Italian for <em>Edit</em>) and the *cellEditor*s get properly activated. It's all ok, by far.  </p>
  
  <p>Here comes the problem. If I change the execution key in the address bar, obtaining something like <code>http://&lt;project base URL&gt;/&lt;flow path&gt;?execution=&lt;execution key 2&gt;</code>, and I press <em>Return</em> (therefore starting another instance of the flow), the starting view of the flow gets opened again, but the "state of the table" doesn't look reset. The row I selected is still selected and the <em>stateMap</em> and <em>righeSelezionate</em> attributes of the <em>BlocchiBean</em> object look unchanged. This is pretty strange, especially because <em>BlocchiBean</em> is declared as <strong>@ViewScoped</strong>:  </p>
  
  <pre><code>@Component
  @ViewScoped
  public class BlocchiBean implements Serializable {  
  </code></pre>
  
  <p>therefore it's expected to be recreated from scratch when a new instance of a flow starts.  </p>
  
  <p>I already tried to play with the scopes, but it didn't solve the problem.</p>
  
  <p>Just for completeness, I mention the previous annotations of the <em>BlocchiBean</em> object:  </p>
  
  <pre><code>@Component("blocchiBean")
  @ViewScoped
  public class BlocchiBean implements Serializable {  
  </code></pre>
  
  <p>My aim was to match the component name with the bean name used in the view and in the flow. Unfortunately, this annotation doesn't solve the problem, either.</p>