开发者

Looking for something similar to ui:repeat to use in a JSP

I just want to iterate over a list, I don't want any html spitted out, so datalist and c:ForEach are not an option. The reason is, the mockup is already made and as rule we have to use <ul> and <li>, so I can't use anything creating a table.

I have investigated and ui:repeat would do the job, but it does not work开发者_JAVA技巧 in a JSP.

I wish there was something like in STRUTS logic:iterate because I only need to iterate over a list.

Thanks for your help.

John


<c:forEach> doesn't generate any HTML. It only iterates through a collection or array. It does exactly the same thing as <struts:iterate>, but in a standard way, and with the JSP EL.

<ul>
    <c:forEach var="item" items="${myListOfItems}">
        <li><c:out value="${item.label}"/></li>
    </c:forEach>
</ul>


It's not entirely clear what exactly you mean with "datalist", but Tomahawk's <t:dataList> does not emit any HTML by default if you omit the layout attribute, so it ought to work out for you.

<ul>
    <t:dataList value="#{memberHandler.subTypes}" var="subType">
        <li><h:outputText value="#{subType.fullSubtypeDisplayName}"/></li>
    </t:dataList>
</ul>

By the way, setting layout="unorderedList" should render exactly the same <ul><li> as in the above example:

<t:dataList value="#{memberHandler.subTypes}" var="subType" layout="unorderedList">
    <h:outputText value="#{subType.fullSubtypeDisplayName}"/>
</t:dataList>

When using JSTL <c:forEach> on a JSP template referring a managed bean value, you are dependent on the JSP version used and whether JSF has already autocreated the managed bean beforehand. When using Servlet 2.5/JSP 2.1, you should be able to use #{} in JSTL tags:

<ul>
    <c:forEach value="#{memberHandler.subTypes}" var="subType">
        <li><h:outputText value="#{subType.fullSubtypeDisplayName}"/></li>
    </c:forEach>
</ul>

When using Servlet 2.4/JSP 2.0, you should stick using ${} and use <c:out> instead of <h:outputText> and ensure that JSF has already autocreated the managed bean beforehand in the view template by #{} which triggers autocreating beans whereas ${} doesn't.

<h:someComponent value="#{memberHandler.someThing}" />
...
<ul>
    <c:forEach value="${memberHandler.subTypes}" var="subType">
        <li><c:out value="${subType.fullSubtypeDisplayName}"/></li>
    </c:forEach>
</ul>

Tomahawk's <t:dataList> would be a much better alternative here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜