如何将 EL 与 <ui:repeat var> 一起使用在 JSF 组件的 id 属性中 [英] How to use EL with &lt;ui:repeat var&gt; in id attribute of a JSF component

查看:20
本文介绍了如何将 EL 与 <ui:repeat var> 一起使用在 JSF 组件的 id 属性中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

<h:form id="#{class2.name}"><h:outputText value="#{class2.name}"/></h:form></ui:repeat>

但是,当我打开页面时,出现如下错误:

<块引用>

组件标识符不能是零长度的字符串

但它在中正确打印.这是怎么引起的,我该如何解决?

解决方案

您可以在 JSF 组件的 id 属性中使用 EL,但 EL 变量必须在 view 期间可用构建时间,而 JSF 组件树将被构建.但是,视图渲染时间期间运行,而 HTML 输出将基于 JSF 组件树生成. 在视图构建期间不可用,并且 #{cl​​ass2.name} 评估为 null,这完全解释了你得到的错误.它在 中工作是因为它在视图渲染时间运行.

如果您将 替换为在视图构建期间运行的 ,那么它将按您的意图工作. 将在 JSF 组件树中物理生成多个 组件,每个组件单独生成自己的 HTML 输出(与,其中完全相同的 组件被多次重用以生成 HTML 输出).

<h:form id="#{class2.name}"><h:outputText value="#{class2.name}"/></h:form></c:forEach>

但是,我真的很想知道您为什么需要这样做.通常不需要动态分配组件 ID.JSF 已经会确保 ID 的唯一性.下面的例子,

<h:form id="表单"><h:outputText value="#{class2.name}"/></h:form></ui:repeat>

将以多种形式结束,每个形式都有一个唯一的 ID,后缀为 的迭代索引.如果您确实需要将 #{cl​​ass2.name} 用于某些 JavaScript/jQuery 目的(您没有在问题中说明具体的功能要求,您认为这将是正确的解决方案,所以这只是猜测),然后将其包装在一个普通的 HTML 元素中:

<div id="#{class2.name}"><h:form id="表单"><h:outputText value="#{class2.name}"/></h:form>

</ui:repeat>

或者将其设置为 JSF 组件的样式类,也可以通过 CSS 选择器选择:

<h:form id="form" styleClass="#{class2.name}"><h:outputText value="#{class2.name}"/></h:form></ui:repeat>

另见:

I have following code:

<ui:repeat var="class2" value="#{bean.list}" varStatus="status">
  <h:form id="#{class2.name}"> 
    <h:outputText value="#{class2.name}" />
  </h:form>
</ui:repeat>

However, when I open the page, it errors as follows:

component identifier must not be a zero-length String

But it is properly printed in the <h:outputText>. How is this caused and how can I solve it?

解决方案

You can use EL in the id attribute of a JSF component, but the EL variable has to be available during view build time, while the JSF component tree is to be built. However, the <ui:repeat> runs during view render time, while the HTML output is to be generated based on JSF component tree. The <ui:repeat var> is not available during view build time and #{class2.name} evaluates to null which totally explains the error you got. That it works in <h:outputText> is because it runs during view render time.

If you replace <ui:repeat> by <c:forEach>, which runs during view build time, then it'll work as you intented. The <c:forEach> will namely generate physically multiple <h:form> components in the JSF component tree which each generate individually their own HTML output (in contrary to <ui:repeat>, wherein the very same <h:form> component is been reused multiple times to generate HTML output).

<c:forEach var="class2" items="#{bean.list}" varStatus="status">
  <h:form id="#{class2.name}"> 
    <h:outputText value="#{class2.name}" />
  </h:form>
</c:forEach>

However, I really wonder why you need to do that. There's usually no need to dynamically assign component IDs. JSF will already ensure the uniqueness of the ID. The below example,

<ui:repeat var="class2" value="#{bean.list}" varStatus="status">
  <h:form id="form"> 
    <h:outputText value="#{class2.name}" />
  </h:form>
</ui:repeat>

will end up in multiple forms with each an unique ID, suffixed with iteration index of the <ui:repeat>. If you actually need to use #{class2.name} for some JavaScript/jQuery purposes (you did nowhere state the concrete functional requirement in the question for which you thought that this would be the right solution, so it's merely guessing), then just wrap it in a plain vanilla HTML element:

<ui:repeat var="class2" value="#{bean.list}" varStatus="status">
  <div id="#{class2.name}"> 
    <h:form id="form">
      <h:outputText value="#{class2.name}" />
    </h:form>
  </div>
</ui:repeat>

Or set it as style class of a JSF component, which is also just selectable via a CSS selector:

<ui:repeat var="class2" value="#{bean.list}" varStatus="status">
  <h:form id="form" styleClass="#{class2.name}">
    <h:outputText value="#{class2.name}" />
  </h:form>
</ui:repeat>

See also:

这篇关于如何将 EL 与 <ui:repeat var> 一起使用在 JSF 组件的 id 属性中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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