lofa in
lofa in

Reputation: 317

Primefaces tabView activeIndex property is always getting null

Primefaces tabView activeIndex property is always getting null.

My view.jsf:

<h:form>
<p:growl id="growl" showDetail="true" />
<p:tabView >
<p:ajax event="myForm" listener="#{employeeEdit.onTabChange}" /> 
<p:tab id="basic" title="Login">
</p:tab>
<p:tab id="personal" title="Personal">
</p:tab>
<p:tab id="contact" title="Contact">
</p:tab>
</p:tabView>

My edit.jsf:

<h:form prependId="false" >
<p:tabView id="myid" activeIndex="#{employeeEdit.tabIndex}" >
<p:tab id="basic" title="Login">
</p:tab>
<p:tab id="personal" title="Personal">
</p:tab>
<p:tab id="contact" title="Contact">
</p:tab>
</p:tabView>

Backing bean: EmployeeEdit.java:

@Component("employeeEdit")
@ViewScoped
@Repository
public class EmployeeEdit implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = -2784529548783517864L;
    private EmployeeDTO employeeDTO = new EmployeeDTO();
    public static HibernateTemplate hibernateTemplate;
    private int tabIndex;   

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory)
    {
        System.out.println("in SessionFactory" +sessionFactory);
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
        System.out.println("in SessionFactory" +hibernateTemplate);

    }


    public int onTabChange(TabChangeEvent event) {   

        System.out.println("tab id = " + event.getTab().getId()+event.getTab().getTitle());
        if(event.getTab().getId().equals("personal"))
        {
            tabIndex =0;
        }
        else if(event.getTab().getId().equals("address"))
        {
            tabIndex =1;
        }
        else
        {
            tabIndex =2;
        }
        System.out.println("tabIndex = "+tabIndex);
        return tabIndex;
    }



    public void edit() throws IOException
    {

        FacesContext.getCurrentInstance().getExternalContext().redirect("/employeeedit.jsf");
    }



    public void cancel() throws IOException
    {
        FacesContext.getCurrentInstance().getExternalContext().redirect("/employeelist.jsf");
    }

    public EmployeeDTO getEmployeeDTO() {
        return employeeDTO;
    }

    public void setEmployeeDTO(EmployeeDTO employeeDTO) {
        this.employeeDTO = employeeDTO;
    }

    public int getTabIndex() {
        return tabIndex;
    }

    public void setTabIndex(int tabIndex) {
        this.tabIndex = tabIndex;
    }
}

But always getting tabIndex=0; Why this happening? The AJAX is working fine. But on clicking the Edit button in the view page tabIndex is getting null. in view.jsf, my command button is

<p:commandButton value="Edit" actionListener="#{employeeEdit.edit}" />

My primefaces version is: primefaces-3.0.M3 with Google Cloud SQL

Upvotes: 2

Views: 5305

Answers (2)

Mr.J4mes
Mr.J4mes

Reputation: 9266

Actually, I think you may not need to use @SessionScoped bean. In fact, it may be a big waste of resources if you don't reuse data in this bean very often.

I think you should keep your bean @ViewScoped and take advantage of <f:param>. You can try this:

<p:tabView id="myid" activeIndex="#{param.tabIndex}" >
    ...
</p:tabView>

<p:commandButton value="Edit" action="employeeedit" ajax="false">
   <f:param name="tabIndex" value="#{employeeEdit.tabIndex}" />
</p:commandButton>

This may save you some resources and you don't need the edit function just to redirect the user yo the edit page.

Upvotes: 1

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

public void edit() throws IOException {
    FacesContext.getCurrentInstance().getExternalContext().redirect("/employeeedit.jsf");
}

Your edit method above, redirects to a new view, that's when your @ViewScoped managed bean EmployeeEdit is destroyed. So, when it is instantiated again, the tabIndex is initialized to 0 (since 0 is the default value for ints in Java.)

As the name suggests @ViewScoped is good for one view, when want to some PPR on the same view. So, it is destroyed when you redirect to some other view.

In such a case, you can use @SessionScoped, which lasts as long as the session lasts.

Upvotes: 0

Related Questions