Should I use ui:fragment with the render attribute to conditionally render HTML tags in Facelets with JSF 2.2? - jsf

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?

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.

Related

how to use <input/> in xhml using JSF of java?

i want to know how to use
of HTML in xhtml page using jsf framework. or please suggest me how to use bootstrap in jsf framework. i tried to use jsf tags instead of HTML tags
but the bootstrap theme and css are not loading properly. can anybody suggest me how to use bootstrap in jsf or any alternate solution?
You can't.
As per w3schools :
XHTML Elements
XHTML elements must be properly nested
XHTML elements must always be closed
XHTML elements must be in lowercase
XHTML documents must have one root element
Related to your problem, Bootstrap can be used with JSF with no problem. Just simply include .css and .js provided by Bootstrap and use them normally in XHTML files as you would do in HTML, but taking care of the above restrictions.

How to not render whole block in JSF?

Is there a JSF 2.1 component which lets me conditionally render (or not render) all its content? Something like
<h:component rendered="#{user.loggedIn}">
...a bunch of jsf components and HTML code...
...even more HTML code...
</h:component>
I am using PrimeFaces 3M4 as this may influence your answer!
<h:panelGroup>
If you set attribute layout="block", you will have a <div> tag
Otherwise, you have a <span> tag.
In general most of jsf components support the render attribute (never bumped in some that does not),
container components like h:panelGrid or h:panelGroup supports the rendered attribute and if its set to false all its child will be hidden too
Same goes for the primefaces components ,and if not it probably a bug (i think there was an issue with tabview of primefaces)
Here's a link for primefaces user guide, you can find supported attributes of all primefaces components there User’s Guide for 3.0.M4

myfaces -- duplicate rendering

I am using myfaces 1.2 with trinidad and tomahawk. I have 1 view xhtml that re-displays on a claim search (the div section with the results uses EL to only display if there are results).
On a 2nd submission, it appears that some HTML components display a 2nd time. For example, i'm using a html table to render a grid. I am not using any JSF component tags to render the table, it's straightup table, tr, td.
Is there a reason why this occurs, can I not mix HTML tags in xhtml or JSF (without f:verbatim)? If so, why not? If not, any hints on what isn't properly configured? Should I use something other than xhtml?

inserting a faces (JSF) EL element on an iframe

I have an iframe like this
<iframe src="path/of/destiny"></iframe>
and I want to write something like this
<iframe src="path/of/destiny?#{myParameter}"></iframe>
That should be possible if you're using Facelets as view technology. But since you're asking this question, I assume that you're still using the legacy JSP as view technology. JSP doesn't allow deferred EL (#{}) in template text as Facelets does. You can however use standard EL (${}) in template text.
<iframe src="path/of/destiny?${myParameter}"></iframe>
You only need to take into account that whenever this has to be obtained as a JSF managed bean property like ${bean.myParameter}, that this bean is already been created and available in the scope. You can ensure this by calling #{bean.something} in a fullworthy JSF component somewhere before the line with the <iframe>. The #{} will create the bean if not created yet.

tool or plugin available to convert HTML to JSF tags

Is there any tool or plugin available to convert HTML code to JSF tags?
If you are using Facelets, you can add for any HTML tag the jsfc attribute that will indicate which JSF component is related to the HTML tag. For example:
<input id="bar" type="text" jsfc="h:inputText" value="#{foo.bar}"/>
I am not sure that automatically generate JSF code from HTML code is a good idea. JSF uses JSP or XHTML pages as a XML description of a page structure. The HTML code is automatically generated by the JSF framework. If you generate the JSF code from the HTML code, you will have a lot of garbage code in your JSP/XHTML files, and I am not sure that the generated code will work correctly.
It is quite dependent of the quality of the HTML you have in fact...
However, you may have a look at the phoenix solution and then clean the generated JSF code.
Maybe you could try this tools: http://docs.oracle.com/cd/E13226_01/workshop/docs92/studio33/JSF/ConvertingHTMLtoJSF.html

Resources