Friday, October 5, 2012

Equivalence between JSTL and JSF tag libraries

As you know some developers tend to combine JSTL tags into a JSF page. Although in most cases this works, it's also possible to meet some side effects related to the JSF lifecycle.

The purpose of this post is not to describe how to convert a JSP page into a JSF one but just to show you that there are some equivalences between these two technologies to switch more easily from one to the other!

Conditional Tags

From the JSTL Core Tag Library, the basic <c:if /> tag allows the conditional execution of its body according to the value of the test attribute.

    content to process if test condition is satisfied

JSF provide the rendered attribute indicating whether or not this component should be rendered (during Render Response Phase). This is not intended to be a replacement of <c:if /> but in fact it could be even more flexible.

    content to process if rendered condition is satisfied

The rendered attribute can be applied to each JSF component.
<h:outputText value="booleanValue is true" rendered="#{booleanValue}" />
<h:outputText value="booleanValue is false" rendered="#{!booleanValue}" />

Iterator Tags

The basic iteration tag <c:forEach /> also from the Core library, accepts many different collection types and supports subsetting and other functionality.

    ${item.name}

JSF provide the UI Repeat tag as replacement. It is also used to iterate over a collection of objects exposed to the JSF page as a value-binding EL expression.

    <h:outputText value="#{item.name}" />

You can also make classical loop with UI repeat since JSF 2.0.

   <h:outputText value="value: #{i}" />
   <h:outputText value="index: #{status.index}" />

Variable Tags

The tag <c:set /> has no equivalence in JSF because you should put your variables directly into a Bean.
<c:set var="foo" value="${myBean.foo}" />
NB : Don't be confused with the UI Param tag in JSF that is really not the same thing as <c:set />.

See also :

1 comment: