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

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

问题描述

我有以下代码:

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

但是,当我打开页面时,它的错误如下所示:


组件标识符不能为零长度String

但它是正确地打印在< h:outputText> 中。这是如何造成的,我该如何解决这个问题?

解决方案您可以在 id 属性,但EL变量必须在视图构建时间期间可用,而JSF组件树将被构建。但是,视图渲染时间期间< ui:repeat> 会在基于JSF组件树生成HTML输出的同时运行。在视图构建期间< ui:repeat var> 不可用,并且#{class2.name} 的计算结果为 null 这完全解释了你得到的错误。它在< h:outputText> 中起作用是因为它在视图渲染时间期间运行。



如果替换< ui:repeat> by < c:forEach> ,它在视图编译时运行,你会如你所愿地工作。 < c:forEach> 将在物理上在JSF组件树中生成多个< h:form> 它们分别产生它们自己的HTML输出(与< ui:repeat> 相反,其中与< h:form>
$ p $ < c:forEach var =class2 code $ c>组件被多次重复使用以生成HTML输出)

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

然而,我真的很想知道为什么你需要这样做。通常不需要动态分配组件ID。 JSF已经确保了ID的唯一性。下面的例子中,

 < ui:repeat var =class2value =#{bean.list}varStatus = 状态 > 
< h:form id =form>
< h:outputText value =#{class2.name}/>
< / h:表格>
< / ui:repeat>

将以多种形式结尾,每个形式都有一个唯一的ID,后缀为< UI:重复> 。如果您实际上需要为某些JavaScript / jQuery目的使用#{class2.name} (您没有在此处指出您认为会出现的具体功能要求是正确的解决方案,所以它只是猜测),然后把它包装在一个简单的香草HTML元素:

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

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

 < ui:repeat var =class2value =#{bean.list}varStatus =status> ; 
< h:outputText value =#{class2.name}/>
< / h:表格>
< / 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:

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

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