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

查看:14
本文介绍了将 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: Empty id attribute is not allowed.

This doesn't work as I get: javax.servlet.ServletException: Empty id attribute is not allowed.

由于 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 组件中,idbinding 属性在视图构建期间进行评估,解析 XHTML/JSP 文件中的 XML 树结构并将其转换为可用 FacesContext#getViewRoot() 的 JSF 组件树的时刻.然而,<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.

基本上有 3 种解决方案:

There are basiclly 3 solutions:

  1. 使用像 JSTL 这样的视图构建时间标签,以便 #{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}" ... />

另请参阅 JSF2 Facelets 中的 JSTL ......有意义吗?

不要将其打印为 JSF 组件的 ID,而是将其打印为纯 HTML 元素.

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

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

请注意,根据 HTML 规范第 6.2 章.你可能想用这样的字符串作为前缀:

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" ... />
    

    如果它位于 <h:form id="formId" 内,这将类似于 <span id="formId:tableId:0:foo">><h:dataTable id="tableId">.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天全站免登陆