Mixing JSF and JSTL

As you probably know, JSTL stands for JavaServer Pages Standard Tag Library and it:

Encapsulates as simple tags the core functionality common to many Web applications. JSTL has support for common, structural tasks such as iteration and conditionals, tags for manipulating XML documents, internationalization tags, and SQL tags. It also provides a framework for integrating existing custom tags with JSTL tags.

In this recipe, you will see how to mix JSF and JSTL to accomplish a common task, displaying an ArrayList.

Getting ready

JSTL libraries can be downloaded from http://java.sun.com/products/jsp/jstl/ and they should be placed in the /WEB-INF/lib folder of your application, next to the JSF libraries. Notice that NetBeans already comes with a library that contains a JSTL distribution.

How to do it...

We can integrate JSTL into JSF by following these steps:

  1. We develop a simple managed bean that contains our ArrayList (this ArrayList is named cars and it will be further rendered with pure JSF and with JSF and JSTL):
    package beans;
    import java.util.ArrayList;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    @ManagedBean(name="bean")
    @SessionScoped
    public class Bean {
    private ArrayList cars = new ArrayList();
    public Bean() {
    cars.add("Clio");
    cars.add("Sandero");
    cars.add("Fiat");
    cars.add("Citroen");
    }
    public ArrayList getCars() {
    return cars;
    }
    public void setCars(ArrayList cars) {
    this.cars = cars;
    }
    }
    
  2. The JSF code to render the cars ArrayList is:
    …
    <h:outputText value="Available cars:" />
    <h:dataTable value="#{bean.cars}" var="car">
    <h:column>
    <h:outputText value="#{car}" />
    </h:column>
    </h:dataTable>
    …
    
  3. Now is the interesting part, where we mix JSF and JSTL. We add JSTL tag library, as shown next:
    xmlns:c=http://java.sun.com/jsp/jstl/core
    

    Note

    We develop an XHTML page, but if you are more interested in a JSP page then use this form:

    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    
  4. Use JSTL c:forEach and c:set tags mixed with JSF h:outputText to render the same collection (which is a collection provided by a JSF managed bean), as shown next:
    …
    <table>
    <h:outputText value="Available cars:" />
    <c:forEach items="${bean.cars}" var="car">
    <tr>
    <td>
    <c:set var="jstlcar" value="${car}"/>
    <h:outputText value="#{jstlcar}" />
    </td>
    </tr>
    </c:forEach>
    </table>
    …
    

Notice how JSTL accesses the JSF managed bean, and JSF accesses the JSTL variables.

How it works...

It is obvious that JSF and JSTL code can appear on the same page, as long as they are tag-based technologies. In addition, they can interact (access, modify, and so on) over common resources, like bundles, session managed beans, POJOs, and so on and they can influence each other in the application flow.

Since both JSF and JSTL provide tags for rendering collections we can choose one of them independent of the other one (many situations are reduced to this—another example is the loading of a resource bundle, which can be accomplish using pure JSF, pure JSTL, or mixing them). But, there are cases where the coexistence of JSTL and JSF can help us solve different tasks, like conditional navigation (JSTL can decide JSF navigation).

As a final conclusion on which technology to use, we can say that this really depends on your situation. If you're developing a new application then a complete JSF solution will definitely be cleaner, but if you're refactoring an existing JSTL application, the JSTL-JSF mix will be faster. Anyway, if you are using JSF then it is best to use JSF for everything it can do unless there is a compelling reason to do otherwise.

See also

The code bundled with this book contains a complete example of this recipe. The project can be opened with NetBeans 6.8 and is named: Mixing_JSF_and_JSTL.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset