Java - JSTL

java

// JSTL and expression language:

// Set a value
<c:set var=“dummyName” value=“dummyValue”/>

// Output a value:
<c:out value=“${dummyName}”>No name</c:out>

// The if statement
<c:if test=“${dummyName == null}”>
</c:if>

// A switch statement:
<c:choose>
  <c:when test=“${dummyName == null}”>
    Empty
  </c:when>
  <c:when test=“${dummyName==‘dummyValue’}”>
    dummyValue.
  </c:when>
  <c:otherwise>
    Something else.
  </c:otherwise>
</c:choose>

// foreach loop:
<c:forEach var=headerValue” items=“${header}”>
  ${headerValue.key}: ${headerValue.value}</br>
</c:forEach>

${attributeName}
${requestScope.bestBatsmanList[0]}
${requestScope.bestBatsmanMap.Sachin}
${requestScope.bestBatsmanMap["Sachin"]}
${sessionScope.keyName}
${header.host}

// Implicit objects available to Expression Language:
requestScope
param: This object contains the URL parameters and post parameters as well.
sessionScope
paramValues
headerValues

How can we use JSTL?

<c:set var=“dummyName” value=“dummyValue”/>
<c:out value=“${dummyName}”>No name</c:out>

<c:if test=“${dummyName == null}”>
</c:if>

How can we implement a switch statement using JSTL?

<c:choose>
  <c:when test=“${dummyName == null}”>
    Empty
  </c:when>
  <c:when test=“${dummyName==‘dummyValue’}”>
    dummyValue.
  </c:when>
  <c:otherwise>
    Something else.
  </c:otherwise>
</c:choose>

How can we implement a foreach loop using JSTL?

<c:forEach var=headerValue” items=“${header}”>
  ${headerValue.key}: ${headerValue.value}</br>
</c:forEach>
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License