【问题标题】:Add loop index in c:forEach tag在 c:forEach 标签中添加循环索引
【发布时间】:2012-07-11 03:58:39
【问题描述】:

在我使用渲染时间标签(如 a4j:repeat、ui:repeat)之前,我基于项目集合创建面板。所有这些都改变了,现在我必须使用 c:forEach 代替。 在每个面板上,我都进行了大量的 AJAX 调用和页面的部分更新。这就是为什么我使用自定义 ID 来识别我想要更新的组件。所以我的渲染属性是这样的:

#{cc.clientId}:pnlRepeat:#{row}:radioAplicar

其中 pnlRepeat 是 id 属性,{#row} 是同一标签中的 rowKeyVar 属性。现在......在使用 c:forEach 时它们都不存在,因此,我得到了重复的 id 异常。我知道我可以使用 varStatus,并使用 #{row} Id 创建一个面板,但另一方面。 JSF 不允许 id 属性评估 EL 表达式。什么是解决方法?非常感谢。

【问题讨论】:

    标签: java jsf-2 primefaces richfaces jstl


    【解决方案1】:

    您不应尝试将任何 ManagedBean 逻辑或 EL 表达式基于生成的重复组件的 ID,例如 <ui:repeat><c:forEach>。就像您已经提到的那样,EL 表达式不会让您动态评估 Id 表达式,因此处理重复组件中的单个项目触发事件的这些情况的适当方法是以以下形式传递唯一标识值<f:attribute> 标签。

    使用<f:attribute>标签会将指定的值放入请求属性中,以便在您的动作监听器中检索。

    <ui:repeat value="..." var="repeatedVar">
      <h:inputText id="newTxt" value="#{repeatedVar}" >
         <f:attribute name="unique_id" value="#{repeatedVar.uniqueId}" />
      </h:inputText>
    </ui:repeat>
    <h:commandButton actionListener="#{managedBean.someMethod}" ...
    

    在 action 方法中,我们可以通过检索动态组件属性来确定要执行的操作或业务逻辑。

    public void someMethod() {
        String uniqueId = (String) UIComponent.getCurrentComponent(FacesContext.getCurrentInstance()).getAttributes().get("unique_id");
        //Get the unique data object
        //Do some business logic and other stuff...
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-23
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 2014-07-23
      • 2015-11-04
      • 2018-10-29
      • 2021-01-22
      相关资源
      最近更新 更多