将JSF dataTable中的组件的id设置为数组中当前项的值 [英] Set id of a component within JSF dataTable to value from current item in the array

查看:196
本文介绍了将JSF dataTable中的组件的id设置为数组中当前项的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表,我想在每个行的id中设置数组中当前项目(具有id字段的对象)的ID。

I'm having a datatable where I would like to set the id of each row to the id of the current item (object that has an id field) in the array that builds the table.

示例:

<h:dataTable
   value="#{bean.list}"
   var="item">
      <h:column>
         <h:outputText id="#{item.id}" .... />
      </h:column>
</h:dataTable>

这不起作用: javax.servlet.ServletException:不允许空id属性

由于JSF如何构建其ID,或者是我是不可能设置id做错了?

Is it not possible to set the id this way due to how JSF builds its id's, or am I doing something wrong?

推荐答案

从JSF UI组件中, id 绑定属性在视图构建时评估,XHTML / JSP文件中的XML树结构将被解析并转换为JSF组件树, FacesContext#getViewRoot()。然而,在视图呈现时间内,< h:dataTable> 在JSF组件树需要通过 UIViewRoot#encodeAll生成HTML代码的时刻进行迭代()。所以,在这个时候, id 属性被评估,#{item} 在EL范围中无处可用,评估为 null ,最终打印一个空字符串。

From the JSF UI components, the id and binding attributes are evaluated during view build time, the moment when the XML tree structure in the XHTML/JSP file is to be parsed and converted to a JSF component tree as available by FacesContext#getViewRoot(). However, the <h:dataTable> iterates during view render time, the moment when the JSF component tree needs to produce HTML code by UIViewRoot#encodeAll(). So, at that moment the id attribute is evaluated, the #{item} is nowhere available in the EL scope and evaluates to null which ultimately prints an empty string.

有basic 3解决方案:

There are basiclly 3 solutions:


  1. 使用视图构建时间标签,如JSTL < c:forEach> code>#{item} 在视图构建期间也可用。

  1. Use a view build time tag like JSTL <c:forEach> so that the #{item} is available during view build time as well.

<table>
    <c:forEach items="#{bean.list}" var="item">
        <tr><td><h:outputText id="#{item.id}" ... />

另请参见 JSTL在JSF2 Facelets ...有意义吗?

不要打印为一个JSF组件的ID,但是一个纯HTML元素。

Don't print it as ID of a JSF component, but of a plain HTML element.

<span id="#{item.id}">

请注意,以数字开头的ID为 HTML规范第6.2章,HTML中的.org>无效。您可能希望使用一些字符串前缀,如下所示:

Please note that IDs starting with a digit are invalid in HTML as per HTML spec chapter 6.2. You might want to prefix it with some string like so:

<span id="item_#{item.id}">


  • 不要使用动态ID。只需使用固定的ID。 JSF将根据行索引自动生成唯一的ID。

  • Don't use a dynamic ID. Just use a fixed ID. JSF will autogenerate an unique ID based on row index anyway.

    <h:outputText id="foo" ... />
    

    这将像< span id =formId:tableId :0:foo> 只要它位于< h:form id =formId>< h:dataTable id =tableId> / code>。 0 是基于0的行索引,每行增加。这样就可以保证每一行都有一个独一无二的ID,而无需自己担心。

    This will end up in like <span id="formId:tableId:0:foo"> provided that it's inside a <h:form id="formId"><h:dataTable id="tableId">. The 0 is the 0-based row index which increments every row. This thus guarantees an unique ID in every row without the need to worry about it yourself.

    这篇关于将JSF dataTable中的组件的id设置为数组中当前项的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆