How to not render whole block in JSF? - 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

Related

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

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.

Embed JSF code in a xhtml

I need to complete a xhtml page with some JSF code (with p:panel and p:datatables, etc.) from a managed bean, but I'm not sure that is possible.
My attemps:
1º
<h:outputText escape="true" value="#{controller.jsfString}"/>
It's not be able to understand "p:" components, only simple html.
2º
<ui:include src="#{controller.jsfString}">
It expects a xhtml path, not a String.
I don't know what else try... Is it even possible?
It's not be able to understand "p:" components, only simple html.
Of course it is not!
The h:outputText value is evaluated at view render time, so if you render JSF tags, they won't be evaluated again since rendering is done.
In principle, it could have been possible to add JSF tags this way using the JSTL <c:out>, but it is not available in JSF facelets.
Anyway, just tell yourself that it prevents you from making bad design.
We'll need more information regarding what the controller is supposed to output in order to help you.
Here p means prime-faces u need to include prime-faces dependency in pom and enable tag lib for prime-faces in XHTML then u can use the all the prime-faces components.

Wrapping JSF components with DIV

I have DIV element with a header style enclosing a h:outputText but when the page renders i see the HTML is formed in such a way that my div doesnot contain the label generated by the h:outputText instead i see the label above my DIV . Am i doing it wrong? i tried f:verbatim but didn't help either
Based on your question history you're using the ancient JSF 1.1. The <f:verbatim> way should work. Perhaps you've enclosed JSF components inside <f:verbatim>. You should not do that. It should only contain raw HTML.
<f:verbatim><div></f:verbatim>
<h:outputText value="Some text." />
<f:verbatim></div></f:verbatim>
Since JSF 1.2 the <f:verbatim> is not necessary anymore. JSF 1.2 should work on all environments where JSF 1.1 is been used. I strongly recommend to upgrade.
See also:
Mix HTML and JSF in a JSF subview
What are the main disadvantages of Java Server Faces 2.0? (a bit of history)

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?

Is it a best practices to wrap a h:form tag in the jsf template page

<p><h:commandLink value="Apply Now" action="register" /></p>
when i writing code above, exception below throws:
[2/12/10 11:24:29:535 CST] 0000005f renderkit W Component j_id26 must be enclosed inside a form.
Is it a best practices to wrap a h:form tag in the jsf template page, and then every page using the template can just directly use the h:commandLink tag without hesitate on the h:form tag?
In my opinion <h:form> and <f:view> shouldn't be in the template page, because:
you can't customize their attributes, like (notably: enctype or beforePhase)
you may want multiple forms in a page.
you may at some point, in some places need to replace <h:form> with <a4j:form> (for example)
If you have many, very simple pages, then you can make an alternative template, which includes the main template, and which adds <f:view> and <h:form>.

Resources