【问题标题】:Primefaces tabView activeIndex property is always getting nullPrimefaces tabView activeIndex 属性总是为空
【发布时间】:2012-01-02 14:07:36
【问题描述】:

Primefaces tabView activeIndex 属性总是为空。

我的观点.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 with Google Cloud SQL

【问题讨论】:

  • 你能发布整个托管 bean (EmployeeEdit) 吗?现在你只发布了你说有效的部分。
  • 如果你把你的EmployeeEdit@SessionScoped写成@SessionScoped会发生什么?
  • @ βнɛƨн Ǥʋяʋиɢ:现在工作正常。非常感谢!
  • 我已将其发布为答案。您可以接受它以将您的帖子标记为已解决。
  • 呃,这不是解决方案,而是解决方法。在同一会话中的不同浏览器选项卡/窗口中打开同一页面,并在每个页面中播放。你会明白为什么这不是一个解决方案。

标签: java google-app-engine jsf-2 primefaces


【解决方案1】:

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

我认为你应该保留你的 bean @ViewScoped 并利用 &lt;f:param&gt;。你可以试试这个:

<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 函数来将用户重定向到编辑页面。

【讨论】:

  • "在您的重定向功能中,在我看来,您正在重定向到同一个编辑页面。"不,他实际上是从view.jsf 重定向到edit.jsf
  • @βнɛƨнǤʋяʋиɢ:感谢您纠正我^^。我更新了我的答案。
【解决方案2】:
public void edit() throws IOException {
    FacesContext.getCurrentInstance().getExternalContext().redirect("/employeeedit.jsf");
}

您上面的 edit 方法重定向到一个新视图,此时您的 @ViewScoped 托管 bean EmployeeEdit 被销毁。所以,当它再次实例化时,tabIndex 被初始化为 0(因为 0 是 Java 中ints 的默认值。)

顾名思义,@ViewScoped 适用于一个视图,当想要在同一视图上进行一些 PPR 时。因此,当您重定向到其他视图时,它会被破坏。

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

【讨论】:

  • 当我把 AJAX event="tabChange" 监听器工作。当我没有更改我的标签时,之前的标签索引就来了。这是因为`private int tabIndex; 下次EmployeeEdit.java)没有设置为0。我该如何解决这个问题?
猜你喜欢
  • 2011-07-10
  • 2018-03-11
  • 1970-01-01
  • 2015-01-06
  • 2013-01-14
  • 2015-12-20
  • 1970-01-01
  • 2013-11-17
  • 2012-02-28
相关资源
最近更新 更多