简体   繁体   中英

What is the equivalent of JSTL <c:forTokens> in JSF or PrimeFaces?

Assume we have the following JSP code:

<c:forTokens items="${someBean.aStringOfIntNumbersSeparatedBySemicolons}" 
             delims=";" 
             var="item" 
             varStatus="stat">
  ${item}
  <c:if test="${!stat.last}">;</c:if>
  <c:if test="${stat.count %5 == 0}">
    <br/>
  </c:if>
</c:forTokens>

Of which the output is rendered in rows with 5 columns each, like so:

在此处输入图片说明

How can I possibly do this with the JSF or Primefaces tags?

There is no direct equivalent, you should transform your token into a list in the managed bean and consume the list in the framework components.

List<String> tokens = Arrays.asList("car1,car2,car3,car4".split(","));

For such simple scenario you might not need the primefaces components. When you go for a Object Oriented model, you can take advantage of PF components to iterate through the list and present its fields using data binding. For instance:

DataList - For each Car type in the cars1 list, a line will be added in the list.

<p:dataList value="#{dataListView.cars1}" var="car" type="ordered">
    <f:facet name="header">
        Basic
    </f:facet>
    #{car.brand}, #{car.year}
</p:dataList>

在此处输入图片说明
DataTable - For each car in cars list, a row is added to the table component.

<p:dataTable var="car" value="#{dtBasicView.cars}">
    <p:column headerText="Id">
        <h:outputText value="#{car.id}" />
    </p:column>

    <p:column headerText="Year">
        <h:outputText value="#{car.year}" />
    </p:column>

    <p:column headerText="Brand">
        <h:outputText value="#{car.brand}" />
    </p:column>

    <p:column headerText="Color">
        <h:outputText value="#{car.color}" />
    </p:column>
</p:dataTable>

在此处输入图片说明

Using such component framework you might speed your development focusing on the business logic instead of UI design.

The only working solution I've found

For now, the only working solution I've found lies partly in the JSF and partly in the back-end code:

  1. Edit the back-end code so that it adds a <br/> after every fifth element of that String field aStringOfIntNumbersSeparatedBySemicolons .

  2. Add escape="false" to the <h:outputText> tag that holds the data on the front end. (The default value is escape="true" , which renders <br/> as &lt;br/&gt; )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM