3

I am in the process of upgrading an old project from JSF 1.1 to JSF 2.2. Specifically, I am upgrading the JSF implementation from MyFaces 1.1 to MyFaces 2.2.12, replacing JSPs with Facelets and upgrading/replacing outdated tag libraries. I am mainly using Migrating from JSF 1.2 to JSF 2.0 as a guide.

The project used some tag library called htmLib with the namespace http://jsftutorials.net/htmLib in it's JSP pages. I can't find any documentation about this tag library anymore, neither on the jsftutorials webpage nor elsewhere, but apparently it was used to include plain HTML tags like <div> or <table> in JSP pages.

Since plain HTML tags can now be used in XML Facelets with JSF2, I am right now removing all occurences of tags from the htmLib taglib like <htm:div>...</htm:div> and replace them with plain HTML tags like <div>...</div>.

However, some of the tags used from htmLib contain the render attribute for conditional rendering, like this:

<htmLib:h4 render="someCondition">
   ...
</htmLib:h4>

Because plain HTML tags don't have a render attribute for this purpose, I was searching for an alternative way to conditionally render plain HTML tags and stumbled upon this answer on SO: How to conditionally render plain HTML elements like <div>s?

So, my idea is to replace a construct like the one above with something like

<ui:fragment render="someCondition">
   <h4>
      ...
   </h4>
</ui:fragment>

My questions:

  • Is wrapping HTML tags inside a <ui:fragment> tag with the render attribute the recommended way to conditionally render HTML tags, or is this method only valid and recommended for the case in the linked question?
  • Are there other ways to conditionally render plain HTML tags in Facelets that should be preferred?
  • Does the <ui:fragment> wrapping method work, no matter what kind of plain HTML is contained within it?
  • Can conditionally rendered <ui:fragment> blocks be nested?
Community
  • 1
  • 1
scholt
  • 180
  • 4
  • 19

1 Answers1

5

There's no limitation in that. Not even for wrapping ui:fragment.

Basically:

  • In order just to control the inner content, with no extra HTML generation use ui:fragment.
  • To generate an extra HTML span element, use h:panelGroup.
  • To generate an extra HTML div element, use h:panelGroup layout="block".

The HTML you have inside isn't a problem. JSF, being a server side framework, performs all the HTML building/rendering job in the server, so the JSF/facelet tags get translated to HTML before the response being sent. All the plain HTML you use inside will remain HTML.

However, beware of using tag handlers inside UI Components when migrating from 1.x. Tag handlers (ui:include, c:if, c:forEach) evaluate before the UI Components (which tipically contain rendered clauses). This has been a source of conflict in JSF 2.

Aritz
  • 30,971
  • 16
  • 136
  • 217
  • Thanks a lot for your answer and explanation! So, do I understand correctly that if I have no tag handlers (`ui:include`, `c:if` etc.) inside the `ui:fragment` and don't need additional `
    ` or `` elements, wrapping a `` tag around my pure HTML content is indeed the way to go?
    – scholt May 03 '17 at 18:30
  • @scholt there's no problem with that! – Aritz May 03 '17 at 18:32