【问题标题】:primefaces 3.2 menuItem bean variable not set on actionprimefaces 3.2 menuItem bean 变量未在操作中设置
【发布时间】:2012-05-25 05:31:28
【问题描述】:

我有一个带有primefaces 的菜单和选项卡控件(带有数据表)的xhtml 页面。数据表根据“类型”变量(在 bean 中)获取值。单击每个菜单项时会触发一个操作(onType("param")),并且在 bean 中设置类型变量(如下所示)。但是现在当我在 tabView 中选择一个选项卡时,类型变量再次设置为 null。为什么会这样。

xhtml代码:

    <h:form id="frm">
  <p:menu>
    <p:menuitem value="price losers" action='#{equityBean.onType("losers")}'/>
    <p:menuitem  value="price gainers"/>
    <p:menuitem  value="price volume"/>
  </p:menu>
  <p:tabView activeIndex="#{equityBean.activeIndex}">
    <p:ajax event="tabChange" listener="#{equityBean.onChange}" update=":frm"/>
    <p:tab title="NSE">                   

      <p:dataTable value="#{equityBean.scripList}" var="scrip">
        ....                        
      </p:dataTable>
    </p:tab>
    <p:tab title="BSE">
      <p:dataTable value="#{equityBean.scripList}" var="scrip">
        .....
      </p:dataTable>
    </p:tab>
  </p:tabView>
</h:form>

豆码:

public void onType(String type)
{
    this.type=type;
}

public void onChange(TabChangeEvent event) {
    exchange=event.getTab().getTitle();
}
   public List<MasterScrip> getScripList() {
      if(type!=null)
      {
       if(type.equalsIgnoreCase("losers"))
       {
        scripList=new ArrayList<MasterScrip> ();
     scripList=getScripByPriceLosers(exchange);
        return scripList;
       }
       else if(type.equalsIgnoreCase("gainers"))
       {
        scripList=new ArrayList<MasterScrip> ();
     scripList=getScripByPriceLosers(exchange);
        return scripList;
       }
       else
       {
           scripList=new ArrayList<MasterScrip> ();
     scripList=getScripByVolumeType(exchange);
       return scripList;
       }
      }
      else
      {
          scripList=new ArrayList<MasterScrip> ();
     scripList=getScripByVolumeType(exchange);
       return scripList;
      }
    }

我哪里错了?

已编辑(web.xml)

    <context-param>
    <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
    <param-value>true</param-value>
</context-param>

【问题讨论】:

  • 我让 bean viewscoped 仍然无法正常工作
  • 您是否检查过 bean 是否在选项卡更改时重新创建。在 bean 的构造函数中设置断点/日志消息来验证它。
  • 是的,每次选项卡更改都会调用构造函数。

标签: java jsf primefaces


【解决方案1】:

声明为@ViewScoped 的bean 有时表现得像@RequestScoped bean,并在每次请求或回发时重新创建。原因在这篇出色的博文中有所描述:@ViewScoped fails in tag handlers

在参考文章中,列出了一些可能的解决方案。您还可以将值保存在会话范围内,并仅将其注入您的视图/请求范围 bean。

【讨论】:

  • 很棒的博客,但没有解决我的问题。它提供了添加代码以删除部分回发并通过 web.xml 保存视图状态的解决方案(在我的问题中编辑的 web.xml 代码),我这样做了,但问题仍然存在。我哪里错了?
  • 我不会改变状态保存机制。它具有 BalusC 所描述的缺点。您是否确定了导致此问题的标签处理程序?在会话中保存变量不是一种选择吗?
  • 谢谢!会话对我有用(尽管仅在 requestScope 中不在 viewScope 中,但我不知道为什么)。再次感谢你是救世主:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-16
  • 2021-01-07
  • 2013-12-18
  • 1970-01-01
  • 1970-01-01
  • 2013-10-10
相关资源
最近更新 更多