使用id =“#{...}"导致java.lang.IllegalArgumentException:不允许使用空的id属性 [英] Using id="#{...}" causes java.lang.IllegalArgumentException: Empty id attribute is not allowed
问题描述
我需要通过为JSF中的某些组件生成动态ID来解决我的问题.
I need to solve my problem with generating dynamic ID for some components in JSF.
看看这个例子:
<h:outputText id="#{bean.id}" value="#{bean.value}" />
我的问题是出现此错误:
My problem is that I am getting this error:
java.lang.IllegalArgumentException: Empty id attribute is not allowed
当我查看生成的HTML输出时,组件的ID为空.这是怎么引起的,我该如何解决?
The ID of the component is empty when I look at generated HTML output. How is this caused and how can I solve it?
推荐答案
您是正确的.我在数据表中使用它.
然后,如果#{bean}
表示var
属性中声明的当前迭代对象,则可能发生这种情况,
Then this can happen if the #{bean}
represents the currently iterated object as declared in var
attribute like so:
<h:dataTable value="#{someBean.beans}" var="bean">
<h:column>
<h:outputText id="#{bean.id}" value="#{bean.value}" />
JSF组件的id
(和binding
)属性在视图构建期间(即需要组成JSF组件树的那一刻)进行评估.但是,#{bean}
仅在视图渲染期间可用,此时<h:dataTable>
需要遍历所有对象并为每个对象生成HTML表行.因此,#{bean}
在视图构建期间不可用,并评估为null
,最终将EL强制为空字符串.因此是例外java.lang.IllegalArgumentException: Empty id attribute is not allowed
.
The id
(and binding
) attribute of a JSF component is evaluated during view build time, that moment when the JSF component tree needs to be composed. However, the #{bean}
is only available during view render time, that moment when <h:dataTable>
needs to iterate over all objects and generate HTML table rows for each of them. The #{bean}
is thus not available during view build time and evaluates to null
which ultimately gets EL-coerced to an empty string. And hence the exception java.lang.IllegalArgumentException: Empty id attribute is not allowed
.
您基本上有3个选择:
-
使用视图构建时间标签来遍历集合.您只需要自己编写所有HTML样板即可:
Use a view build time tag instead to iterate over a collection. You'd only need to write all HTML boilerplate yourself:
<table>
<c:forEach items="#{someBean.beans}" var="bean">
<tr>
<td>
<h:outputText id="#{bean.id}" value="#{bean.value}" />
使用纯HTML元素:
Use a plain HTML element:
<h:dataTable value="#{someBean.beans}" var="bean">
<h:column>
<span id="#{bean.id}">#{bean.value}</span>
不设置动态ID,而是固定ID. JSF将通过在表的行索引前添加HTML输出来确保其唯一性:
Don't set a dynamic ID, but a fixed ID. JSF will ensure of uniqueness in HTML output by prepending it with row index of the table:
<h:dataTable value="#{someBean.beans}" var="bean">
<h:column>
<h:outputText id="id" value="#{bean.value}" />
另请参见:
- JSF2 Facelets中的JSTL ...有意义吗?(您可以用动态ID"替换"JSTL")
- JSTL in JSF2 Facelets... makes sense? (you can substitute "JSTL" with "dynamic ID")
See also:
这篇关于使用id =“#{...}"导致java.lang.IllegalArgumentException:不允许使用空的id属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!