Docsity

JSTL (JavaServer Pages Standard Tag Library) in Advanced Java Programming, Slides of Java Programming

Partial preview of the text

Download JSTL (JavaServer Pages Standard Tag Library) in Advanced Java Programming and more Slides Java Programming in PDF only on Docsity!

Advanced Java Programming

Lecture 12 - JSTL (JSP Standard Tag Library) Reference: JAVA EE 5 TUTORIAL

Agenda

 What is and Why JSTL?  JSTL Functional areas  Core tags  Database Access tags  XML tags  Quick review on XPath  Internationalization and Text Formatting tags  EL (Expression Language) functions tags

What is JSTL?

 Standard set of tag libraries  Encapsulates core functionality common to many JSP applications  iteration and conditionals  XML  database access  internationalized formatting  Likely to evolve to add more commonly used tags in future versions

Why JSTL?

 You don't have to write them yourself  You learn and use a single standard set of tag libraries that are already provided by compliant Java EE platforms  Vendors are likely to provide more optimized implementation  Portability of your applications are enabled

Declaration of JSTL Tag Libraries  Core  <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  XML  <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>  Internationalization (i 18 n)  <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>  Database (SQL)  <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>  Functions  <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

Core Tags

(Core Actions)

Core Tags Types (page 2 )

 URL management  <c:import>  <c:param>  <c:redirect>  <c:param>  <c:url>  <c:param>  General purpose  <c:out>  <c:catch>

Variable Support, <c:set>

 Sets the value of an EL variable or the property of an EL variable via “var” attribute  in any of the JSP scopes via “scope” attribute  page, request, session, application  If the variable does not already exist, it gets created and saved (in the scope object)  Variable can be set in 2 different ways  <c:set var="foo" scope="session" value="..."/>  example: <c:set var="bookId" value="${param.BookID}"/>  <c:set var="foo">value to be set</c:set>

Example: How a predefined Variable

“customerTable” is used: ./elsupport/Set2.jsp

<h 4 >Using "customerTable" application scope attribute defined in Set.jsp a first time</h 4 > <c:out value="${customerTable}" escapeXml="false"/> <h 4 >Using "customerTable" application scope attribute defined in Set.jsp a second time</h 4 > <c:out value="${customerTable}" escapeXml="false" />

Example: How a predefined Variable

“customerTable” is used: ./elsupport/Set 2 .jsp

Conditional Tags

 Flow control tags eliminate the need for scriptlets  Without conditional tags, a page author must generally resort to using scriptlets in JSP page  <c:if test=”..”>  Conditional execution of its body according to value of a test attribute  <c:choose>  Performs conditional block execution by the embedded <c:when> and <c:otherwise> sub tags  Works like if-then-else

Example: <c:if test=”...”>, ./conditionals/If.jsp <c:forEach var="customer" items="${customers}"> <c:if test="${customer.address.country == 'USA'}"> ${customer}
</c:if> </c:forEach> Only the customers whose “address.country” property value is “USA” are displayed through <c:forEach> loop.