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
Your
editmethod above, redirects to a new view, that’s when your@ViewScopedmanaged beanEmployeeEditis destroyed. So, when it is instantiated again, thetabIndexis initialized to 0 (since 0 is the default value forints in Java.)As the name suggests
@ViewScopedis 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.