title: JSF @ViewScoped Bean State is Lost
body: <p>I am using @ViewScoped Bean for small CRUD application I have a edit and view page but when I click buttons (edit) it will render edit form. After edit form appears the save button or cancel button does not call the function but renders the whole page. The actionListener's function is not called at all and everthing is initialized. Is something wrong with my bean and page?? I am using JSF 2 with richfaces and facelet.</p>
  
  <pre><code>          //ViewScoped Bean   
  
              /*
       * To change this template, choose Tools | Templates
       * and open the template in the editor.
       */
      package com.legendMgr.Legend;
  
      import java.io.Serializable;
      import java.sql.SQLException;
      import java.util.List;
  
  
      import java.util.logging.Logger;
      import javax.annotation.PostConstruct;
  
      import javax.faces.application.FacesMessage;
      import javax.faces.bean.ManagedBean;
      import javax.faces.bean.ViewScoped;
  
  import javax.faces.context.FacesContext;
  
  /**
   *
   * @author kitex
   */
  @ManagedBean(name = "legendbean")
  @ViewScoped
  public class LegendController implements Serializable {
  
      LegendDTO legendDTO;
      String selectedLegend;
      List&lt;LegendDTO&gt; legendDTOs;
      boolean edit;
  
      public List&lt;LegendDTO&gt; getLegendDTOs() {
          return legendDTOs;
      }
  
      public void setLegendDTOs(List&lt;LegendDTO&gt; legendDTOs) {
          this.legendDTOs = legendDTOs;
      }
  
      @PostConstruct
      void initialiseSession() {
          FacesContext.getCurrentInstance().getExternalContext().getSession(true);
      }
  
      public LegendController() {
          if (!edit) {
              legendDTO = new LegendDTO();
              legendDTO.getList().add(new Legend());
              legendDTOs = getLegends();
          }
      }
  
  
      public String getSelectedLegend() {
          return selectedLegend;
      }
  
      public void setSelectedLegend(String selectedLegend) {
          this.selectedLegend = selectedLegend;
      }
  
      public boolean isEdit() {
          return edit;
      }
  
      public void setEdit(boolean edit) {
          this.edit = edit;
      }
  
      public LegendDTO getLegendDTO() {
          return legendDTO;
      }
  
      public void setLegendDTO(LegendDTO legendDTO) {
          this.legendDTO = legendDTO;
      }
  
      public void addLegendRange() {
          Logger.getLogger(LegendController.class.getName()).warning("List Size " + legendDTO.getList().size());
          legendDTO.getList().add(new Legend());
          Logger.getLogger(LegendController.class.getName()).warning("List Size " + legendDTO.getList().size());
  
      }
  
      public void removeLegendRange(Legend legend) {
          if (legendDTO.getList().size() != 1) {
              legendDTO.getList().remove(legend);
          }
      }
  
      public String saveLegend() {
          Logger.getLogger(LegendController.class.getName()).warning("Save Legend Edit" + edit);
          LegendDAO dao = new LegendDAO();
          if (dao.addLegend(legendDTO, edit)) {
              edit = false;
              Logger.getLogger(LegendController.class.getName()).warning("Save Legend Edit" + edit);
          } else {
  
              FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Could Not Save Confim if you have already defined Legend " + legendDTO.getLegendName() + "!"));
          }
          return "";
      }
  
      public String cancel() {
          edit = false;
          legendDTO = new LegendDTO();
          legendDTO.getList().add(new Legend());
           return "";
      }
  
      public List&lt;LegendDTO&gt; getLegends() {
          LegendDAO dao = new LegendDAO();
          return dao.getLegendDTO();
      }
  
      //All function from here are for legend  delete
      public void deleteLegendType(LegendDTO dto) {
          LegendDAO dao = new LegendDAO();
          if (dao.deleteLegendType(dto.getLegendName())) {
              FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Deleted !"));
          } else {
              FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Deleted Error !"));
          }
      }
  
      //All function from here is to legend edit
      public void editLegendType(LegendDTO dto) {
          edit = true;
          Logger.getLogger(LegendController.class.getName()).warning("DTO : " + dto.legendName);
          legendDTO = dto;
          LegendDAO dao = new LegendDAO();
          Logger.getLogger(LegendController.class.getName()).warning("Edit dto set");
          try {
              List&lt;Legend&gt; legends = dao.getDetailForEditLegend(dto.getLegendName());
              if (legends == null || legends.isEmpty()) {
                  dto.getList().add(new Legend());
              } else {
                  dto.setList(legends);
              }
          } catch (SQLException ex) {
              Logger.getLogger(LegendController.class.getName()).warning("SQL EXception has occoured");
          }
          Logger.getLogger(LegendController.class.getName()).warning("In Edit Legend Function The size of list" + dto.getList().size());
  
      }
  }
  </code></pre>
  
  <p>//xhtml code</p>
  
  <pre><code>       &lt;?xml version='1.0' encoding='UTF-8' ?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:rich="http://richfaces.org/rich"
        xmlns:a4j="http://richfaces.org/a4j"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:ui="http://java.sun.com/jsf/facelets"&gt;
      &lt;h:head&gt;
          &lt;title&gt;Facelet Title&lt;/title&gt;
      &lt;/h:head&gt;
      &lt;h:body&gt;
          &lt;ui:composition template="/legendTemplate.xhtml"&gt;
              &lt;ui:define name="windowTitle"&gt;Change Legend&lt;/ui:define&gt;
              &lt;ui:define name="content"&gt;
  
  
                  &lt;h:messages globalOnly="true"/&gt;
                  &lt;rich:panel id="firstPanel"&gt;
                      &lt;h:form id="nis_viewLegend"&gt;
                          &lt;rich:dataTable id="data_tbl" value="#{legendbean.legendDTOs}" var="legendDTOvar" style="width:100%" rendered="#{!legendbean.edit and not empty legendbean.legendDTOs}"&gt;
                              &lt;rich:column&gt;
                                  &lt;f:facet name="header"&gt;
                                      &lt;h:outputText value="Description"/&gt;
                                  &lt;/f:facet&gt;
                                  &lt;h:outputText value="#{legendDTOvar.desc}"/&gt;
                              &lt;/rich:column&gt;
  
                              &lt;rich:column&gt;
                                  &lt;f:facet name="header"&gt;
                                      &lt;h:outputText value="Legend Type"/&gt;
                                  &lt;/f:facet&gt;
                                  &lt;h:outputText value="#{legendDTOvar.legendName}"/&gt;
                              &lt;/rich:column&gt;
  
                              &lt;rich:column&gt;
                                  &lt;f:facet name="header"&gt;
                                      &lt;h:outputText value="Legend Type"/&gt;
                                  &lt;/f:facet&gt;
                                  &lt;h:outputText value="#{legendDTOvar.legendFor}"/&gt;
                              &lt;/rich:column&gt;
  
                              &lt;rich:column&gt;
  
                                  &lt;a4j:commandLink value="Delete" actionListener="#{legendbean.deleteLegendType(legendDTOvar)}" render=":firstPanel"/&gt;
                                  &lt;h:outputText value="/"/&gt;
                                  &lt;a4j:commandLink value="Edit" actionListener="#{legendbean.editLegendType(legendDTOvar)}" render=":secondPanel :editLegendForm :nis_viewLegend"/&gt;
  
                              &lt;/rich:column&gt;
  
                          &lt;/rich:dataTable&gt;
                      &lt;/h:form&gt;
                  &lt;/rich:panel&gt;
  
                  &lt;rich:panel id="secondPanel"&gt;
                      &lt;h:form id="editLegendForm" rendered="#{legendbean.edit}"&gt;
                          &lt;h:outputText value="Legend Name"/&gt;&lt;br/&gt;
                          &lt;h:inputText value="#{legendbean.legendDTO.legendName}"  readonly="true"/&gt;&lt;br/&gt;
  
                          &lt;h:outputText value="Description"/&gt;&lt;br/&gt;
                          &lt;h:inputText value="#{legendbean.legendDTO.desc}"/&gt;&lt;br/&gt;
  
                          &lt;h:outputText value="Legend For"/&gt;&lt;br/&gt;
                          &lt;h:inputText value="#{legendbean.legendDTO.legendFor}"/&gt;&lt;br/&gt;
                          &lt;br/&gt; 
                          &lt;h:outputText value="Range"  /&gt;  
                          &lt;rich:dataTable id="editDataPnl" value="#{legendbean.legendDTO.list}" var="legend" style="width:100%"&gt;
  
                              &lt;rich:column&gt;
                                  &lt;f:facet name="header"&gt;
                                      &lt;h:outputText value="SN"/&gt;
                                  &lt;/f:facet&gt;
                                  &lt;h:inputText value="#{legend.sn}"/&gt;
                              &lt;/rich:column&gt;
  
                              &lt;rich:column&gt;
                                  &lt;f:facet name="header"&gt;
                                      &lt;h:outputText value="Description"/&gt;
                                  &lt;/f:facet&gt;
                                  &lt;h:inputText value="#{legend.desc}"/&gt;
                              &lt;/rich:column&gt;
  
                              &lt;rich:column&gt;
                                  &lt;f:facet name="header"&gt;
                                      &lt;h:outputText value="Lower Range"/&gt;
                                  &lt;/f:facet&gt;
                                  &lt;h:inputText value="#{legend.lowerRange}"/&gt;
                              &lt;/rich:column&gt;
  
                              &lt;rich:column&gt;
                                  &lt;f:facet name="header"&gt;
                                      &lt;h:outputText value="Upper Range"/&gt;
                                  &lt;/f:facet&gt;
                                  &lt;h:inputText value="#{legend.upperRange}"/&gt;
                              &lt;/rich:column&gt;
  
                              &lt;rich:column&gt;
                                  &lt;f:facet name="header"&gt;
                                      &lt;h:outputText value="Color"/&gt;
                                  &lt;/f:facet&gt;
                                  &lt;h:inputText value="#{legend.color}"/&gt;
                              &lt;/rich:column&gt;
  
                              &lt;rich:column&gt;
                                  &lt;a4j:commandLink value="Add" actionListener="#{legendbean.addLegendRange}" render=":secondPanel"/&gt;
                                  &lt;h:outputText value=" / "/&gt;
                                  &lt;a4j:commandLink value="Remove" actionListener="#{legendbean.removeLegendRange(legend)}" render=":secondPanel"/&gt;
                              &lt;/rich:column&gt;
                          &lt;/rich:dataTable&gt;
  
                          &lt;br/&gt;
                          &lt;center&gt;
                              &lt;a4j:commandButton value="SAVE" action="#{legendbean.saveLegend()}" render=":firstPanel :secondPanel"/&gt;
                              &lt;a4j:commandButton value="CANCEL" action="#{legendbean.cancel()}" render=":firstPanel :secondPanel"/&gt;
                          &lt;/center&gt;
                      &lt;/h:form&gt;
                  &lt;/rich:panel&gt;
  
              &lt;/ui:define&gt;
  
          &lt;/ui:composition&gt;
      &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>