z22
z22

Reputation: 10083

primefaces 3.2 menuItem bean variable not set on action

i have an xhtml page with menu and tab controls(with datatable) of primefaces. the datatable gets the value based on 'type' variable(in bean). On click on each menu item an action is fired(onType("param")) and the type variable is set in bean(shown below). but now when i select a tab in tabView, the type variable is again set to null. why does this happen.

xhtml code:

    <h:form id="frm">
  <p:menu>
    <p:menuitem value="price losers" action='#{equityBean.onType("losers")}'/>
    <p:menuitem  value="price gainers"/>
    <p:menuitem  value="price volume"/>
  </p:menu>
  <p:tabView activeIndex="#{equityBean.activeIndex}">
    <p:ajax event="tabChange" listener="#{equityBean.onChange}" update=":frm"/>
    <p:tab title="NSE">                   

      <p:dataTable value="#{equityBean.scripList}" var="scrip">
        ....                        
      </p:dataTable>
    </p:tab>
    <p:tab title="BSE">
      <p:dataTable value="#{equityBean.scripList}" var="scrip">
        .....
      </p:dataTable>
    </p:tab>
  </p:tabView>
</h:form>

bean code:

public void onType(String type)
{
    this.type=type;
}

public void onChange(TabChangeEvent event) {
    exchange=event.getTab().getTitle();
}
   public List<MasterScrip> getScripList() {
      if(type!=null)
      {
       if(type.equalsIgnoreCase("losers"))
       {
        scripList=new ArrayList<MasterScrip> ();
     scripList=getScripByPriceLosers(exchange);
        return scripList;
       }
       else if(type.equalsIgnoreCase("gainers"))
       {
        scripList=new ArrayList<MasterScrip> ();
     scripList=getScripByPriceLosers(exchange);
        return scripList;
       }
       else
       {
           scripList=new ArrayList<MasterScrip> ();
     scripList=getScripByVolumeType(exchange);
       return scripList;
       }
      }
      else
      {
          scripList=new ArrayList<MasterScrip> ();
     scripList=getScripByVolumeType(exchange);
       return scripList;
      }
    }

where am i getting wrong?

edited(web.xml) :

    <context-param>
    <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
    <param-value>true</param-value>
</context-param>

Upvotes: 0

Views: 2882

Answers (1)

Matt Handy
Matt Handy

Reputation: 30025

Beans declared as @ViewScoped sometimes behave like @RequestScoped beans and are recreated at each request or postback. The reason is described in this excellent blog post: @ViewScoped fails in tag handlers.

In the referenced article, there are some possible solutions listed. You could also save the value in session scope and only inject it into your view/request scoped bean.

Upvotes: 2

Related Questions