raid3n
raid3n

Reputation: 115

Iterate <f:verbatim> within <ui:repeat>

I have a @ViewScoped bean with a List<String> containing plain HTML. I want to iterate over this list and output plain html:

<c:forEach items="#{bean.list}" var="html">
  <f:verbatim>#{html}</f:verbatim>
</c:forEach>

That snippet above works well but when the page is refreshed the bean costrunctor is recalled. This issue/bug is known: JSTL c:forEach causes @ViewScoped bean to invoke @PostConstruct on every request

So the suggestion is to replace <c:forEach> with <ui:repeat>.

<ui:repeat value="#{bean.list}" var="html">
  <f:verbatim>#{html}</f:verbatim>
</ui:repeat>

But that doesn't work. I have a blank page. I tried <h:dataTable>, <a4j:repeat> and <rich:dataTable> but nothing to do.

Any solutions?

Upvotes: 2

Views: 2884

Answers (2)

BalusC
BalusC

Reputation: 1108632

Use <h:outputText escape="false"> instead.

<ui:repeat value="#{bean.list}" var="html">
    <h:outputText value="#{html}" escape="false" />
</ui:repeat>

The <f:verbatim> is an old JSP-oriented tag which was intented to be able to embed plain HTML among JSF components in JSF 1.0/1.1. Without it, all the plain HTML would otherwise be rendered before the JSF component tree. This unintuitive behaviour was fixed in JSF 1.2 which made the tag superfluous. In Facelets it's also superfluous and in Facelets 2.0 (for JSF 2.0) it's even deprecated. See also the introductory text of the tag documentation. Don't use it. If you want to render unescaped HTML, rather use the <h:outputText escape="false"> as suggested in the above example. If you want to render pieces of inline HTML conditionally, rather use <h:panelGroup> or <ui:fragment> instead.

Upvotes: 4

Ilya Dyoshin
Ilya Dyoshin

Reputation: 4624

Why not to use ViewScoped datamodel?

in JEE5 (JSF 1.2) - could be seam-managed @Factory("bean.list") List<> produceList()... It will be view-scoped (actually page-scoped).

and in JEE6 (JSF 2.0) you can use the same pattern with CDI.

Or you can implement the same lifecycle for your list and create own solution.

Upvotes: 0

Related Questions