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

﹥>﹥吖頭↗ 提交于 2019-12-24 02:31:10

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/5613117/iterate-fverbatim-within-uirepeat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!