JSLT: 5 minutes with core JSLT tags

JSLT is a tag library provided by Sun, which has support for common tasks such as iteration and conditional execution, database access, tags for manipulating XML documents, internationalization. It is also possible to integrate custom tags with the default JSTL tags. There are in total 6 JSLT tag library descriptors, a few from the core library are presented in this blog entry.

<c:if> tag

<code><c:if></code> identifies an if statement, as per the below example. It also includes <code><cl:out></code> displays the result of an expression:

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>IF conditional example</title>
  </head>
  <body>
     <c:set var="num" scope="session" value="${100}"/>    
        <c:if test="${num> 0}"/>
            <c:out value="${num}"/>
        </c:if>
  </body>
</html>

<c:choose> tag

The <code><c:choose></code> establishes the context for non mutually exclusive, meaning that there is room for multiple if else clauses via <code><c:when></code> and <code></c:otherwise></code> tags:

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>CHOOSE example</title>
  </head>
  <body>
    <c:set var="name" scope="session" value="aName"/> 
    <cl:choose>
      <cl:when test="${name=='aName'}">
         <cl:out value="${name}"></cl:out>
      </cl:when>
   <cl:otherwise>
      <cl:out value="not maching"></cl:out>
   </cl:otherwise>
</cl:choose>
</body>
</html>

<c:forEach> tag

The tag <code><c:forEach></code> is for the basic iteration functionality.

<html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 <title>forEach iteration example</title>
 </head>
 <body>
    <c:forEach var="counter" begin="1" end="10">
       <c:out value="${counter}"/>
    </c:forEach>
 </body>
</html>

Posted on September 17, 2017, in Uncategorized. Bookmark the permalink. Leave a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.