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
.
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.
We can integrate JSTL into JSF by following these steps:
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; } }
ArrayList
is:… <h:outputText value="Available cars:" /> <h:dataTable value="#{bean.cars}" var="car"> <h:column> <h:outputText value="#{car}" /> </h:column> </h:dataTable> …
xmlns:c=http://java.sun.com/jsp/jstl/core
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"%>
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.
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.