2012-01-02 56 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