English 中文(简体)
如何使用 ui:repeat 标签在数据表格中动态生成 ID?
原标题:How to dynamically generate id in dataTable using ui:repeat tag
  • 时间:2012-05-23 06:14:18
  •  标签:
  • jsf-2
  • el

我正在使用 ui: repeat tag 来生成图像。 我有五张图像, 我要在每次迭代上, 我的图像得到像图像1、 imp2、 imp2、 imp3.... 图像。 我尝试过这个, 但是没有效果 。

<div id="imageGallery">
    <ui:repeat value="#{countryPages_Setup.images}" var="image" varStatus="status">
        <tr>
            <td>
                <a href="javascript:void()" class="launchLink">
                    <p:graphicImage id="image#{status.index}"         //problem
                                    value="/resources/images/#{image}"
                                    width="100"
                                    height="100"
                                    rendered="#{countryPages_Setup.renderImages}"/>
                </a>
            </td>
        </tr>
    </ui:repeat>
</div>

我也尝试了 {staus.index + 1} 。 我也尝试了 id = image\\\\\\ \ \ \ \ \ \ \ 但它也行不通。 我该怎么做呢?

谢谢 谢谢

最佳回答

您不能在 id 属性内使用 El 表达式。 它必须是静态的。 ui: repeat 本身会生成id的前缀。 您不需要关注独一性 。

例如,如果您有一个 id=“ 图像” ,那么生成的ids就是

some Prefix:0:image , someprefix:1:image ,...

问题回答

您可以在 id 属性中使用EL, 但必须在视图构建时间里使用 。 lt; ui: repeat> 无论在视图转换时间期间运行, 它会重新使用相同的 lt; p:graphicImaage> 来生成多个 HTML ; lt; img> 元素。 它不会在视图构建时间里运行, 因此 id 仍然保留 image

如果您将 替换为 , 那么它就会按照您想要的那样工作。 在视图构建时间运行, 它会生成多个 组件, 然后每个组件只能完成一次 。

<div id="imageGallery">
    <c:forEach items="#{countryPages_Setup.images}" var="image" varStatus="status">
        <tr>
            <td>
                <a href="javascript:void()" class="launchLink">
                    <p:graphicImage id="image#{status.index}"
                                    name="images/#{image}"
                                    width="100"
                                    height="100"
                                    rendered="#{countryPages_Setup.renderImages}"/>
                </a>
            </td>
        </tr>
    </c:forEach>
</div>

但您可以在 Javascript 中使用变量,例如:

<h:graphicImage ... onclick="top.location.href= http://blabla.com/imageclicked?id=#{status.index} "/>

您也可以使用事件处理器的变量(自 JSF 2. 0):

<h:commandLink ... action="#{myBean.imageClicked(status.index)"/>

但您使用 < code> c: forEach 的环路可能会造成问题。 请注意, JSTL 标签( 以 < code> c: 开头的所有标签) 与 JSF 不完全兼容。 如果您运气好, 那么它们就会按预期工作 。 但是无论如何, 翻版引擎会放慢速度, 因为页面会多次被 JSF 和 JSP 打印引擎处理 。

更好地使用 ui:repeat

只需将 更改为





相关问题
JSF - create a generic confirmation page (dynamic navigation)

I ll try to explain my problem. I use the JSF 1.2 implementation of IBM in a very very rigid environment (company layer, and strict constraints -don t add library etc.-). I want to create a simple ...

How to put "new line" in JSP s Expression Language?

What would be right EL expression in JSP to have a new line or HTML s <br/>? Here s my code that doesn t work and render with in text. <af:outputText value="#{msg....

JSPX namespaces not visible for EL functions?

I m attempting to use JSPX (pure XML syntax for JSP) and running into what seems like it should work, but doesn t. I m importing taglibs using namespace declarations in the jsp:root element, then ...

Expression language for IP address

what is the corressponding expression for getting ip adress of server, as ${pageContext.request.serverName} is for getting the server name

JSP EL (Expression Language) causing problems in Eclipse

My system: Ubuntu 9.10. Eclipse 3.5.1 with Java EE 1.2.1 (manual install - NOT from synaptic). Web Developer Tools 3.1.1 I ve recently adopted someone else s code (a Dynamic Web Project), and run ...

Is it possible to use EL inside page directives?

I have some static html content (included on a dynamically generated page) that I want to localize, i.e., help-en.html, help-fr.html and so on. In the JSP file where it is to be included I have a bean ...

热门标签