2012-01-02 83 views
2

Primefaces tabView activeIndex属性始终为空。Primefaces tabView activeIndex属性始终为空

我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> 

我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> 

支持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; 
    } 
} 

但总是得到的tabIndex = 0;为什么会这样呢? AJAX工作正常。但是,单击查看页面tabIndex中的编辑按钮将变为空。 在view.jsf,我的命令按钮是

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

我primefaces的版本是:primefaces-3.0.M3与谷歌云SQL

+0

你可以发布整个托管bean('EmployeeEdit')吗?现在你只发布了你说的工作部分。 – 2012-01-02 14:18:50

+0

是的,我上面已经发布。 – 2012-01-02 14:24:07

+1

如果你使'EmployeeEdit' @ SessionScoped'发生什么? – 2012-01-02 14:29:06

回答

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

edit上面的方法,重定向到一个新的观点,那么当您的@ViewScoped管理bean EmployeeEdit被销毁时。所以,当它再次被实例化,tabIndex作为顾名思义@ViewScoped是很好的一个观点,当希望将一些PPR的初始化为0(因为0是int S IN Java中的默认值。)

同样的看法。所以,当你重定向到其他视图时,它会被破坏。

在这种情况下,您可以使用@SessionScoped,持续时间长达会话持续时间。

+0

当我把AJAX事件=“tabChange”监听器正在工作。当我没有改变我的标签,那么以前的标签索引即将到来。这是因为''private int tabIndex; '下一次'EmployeeEdit.java'时不会设置为0)。我该如何解决这个问题? – 2012-01-03 11:04:36

1

其实我觉得你可能不需要使用@SessionScoped bean。事实上,如果你不经常在这个bean中重复使用数据,这可能会造成很大的资源浪费。

我想你应该保留你的bean @ViewScoped并利用<f:param>。你可以试试这个:

<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> 

这可以节省你一些资源,你不需要edit功能只是将用户重定向哟编辑页面。

+0

“在您的重定向功能中,在我看来,您正在重定向到相同的编辑页面。”不,他实际上是从'view.jsf'重定向到'edit.jsf'。 – 2012-01-02 15:51:55

+0

@βнɛƨнǤʋяʋиɢ:感谢纠正我^^。我更新了我的答案。 – 2012-01-02 16:02:52

+0

+1。这个比我的好看。 – 2012-01-02 16:05:14