Showing posts with label JSTL. Show all posts
Showing posts with label JSTL. Show all posts

April 12, 2011

Setting up JSTL in JBoss 4.2.2GA

The JBoss4.2.2GA Application Server contains the dependent jar/s for JSTL support in JSPs. The dependent jar - jstl.jar - is present in the following folder of JBoss4.2.2GA:

%JBOSS_HOME%/server/default/deploy/jboss-web.deployer/jstl.jar

1) Open the jstl.jar with an archiving tool such as WinRar. Explore into the META-INF folder within the jstl.jar

2) The META-INF contains the tld files for all the JSTL tag libraries such as the core, fmt, xml, sql and functions tag libraries.

3) To use JSTL tags for a particular library, for example, the core library, open the c.tld file and look for the <uri> element in the tld file.

For the c.tld in JBoss4.2.2GA, the <uri> element is the following:
<uri>http://java.sun.com/jsp/jstl/core</uri>

The contents of the <uri> element is needed for the taglib directive in the JSP that will contain the core library JSTL tags.

4) In the JSP which contains the JSTL tags from the core library, add the following taglib directive. The contents of the <uri> element are the value of the uri attribute in the taglib directive.
Note: Create a new JSP in your web application and add this taglib directive at the top of that JSP.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


5) Test if JSTL has successfully been setup by put the following out tag in the JSP as follows:

<body>
   <c:out value="Testing JSTL" /> 
</body>

March 2, 2011

JSTL 1.1 - <c:choose>, <c:when> and <c:otherwise> example


<c:set var="userType" scope="request" value="none" />
<c:choose>
   <c:when test="${requestScope.userType == 'regular'}">
      Welcome! You are a regular user.
   </c:when>
   <c:when test="${requestScope.userType == 'premium'}">
      Welcome! You are a premium user.
   </c:when>
   <c:otherwise>
      Welcome! You are not a registered user.
   </c:otherwise>
</c:choose>

prints,

Welcome! You are not a registered user.

JSTL 1.1 - <c:forTokens> example


Example 1:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<table border="1">
   <c:forTokens var="element" items="t1;t2/t3;t4/t5;t6,t7,t8;t9" delims=";,/"  varStatus="c">
      <tr>
         <td>${c.count}</td>
         <td>${element}</td>
      </tr>
   </c:forTokens>
</table>

prints,

1t1
2t2
3t3
4t4
5t5
6t6
7t7
8t8
9t9

Example 2:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="testString" scope="application" value="t1;t2/t3;t4/t5/t6,t7,t8;t9" />
<table border="1">
   <c:forTokens var="element" items="t1;t2/t3;t4/t5;t6,t7,t8;t9" delims=";,/"  varStatus="c" begin="1" end="7" step="2">
      <tr>
         <td>${c.count}</td>
         <td>${element}</td>
      </tr>
   </c:forTokens>
</table>

prints,



1t2
2t4
3t6
4t8

JSTL 1.1 - <c:catch> example


<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:catch var="ex" >
   <% int j=3/0; %>
</c:catch>
<c:out value="${ex.message}" />

prints,

/ by zero

JSTL 1.1 - <c:remove> example


<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="name" value="Dinesh" scope="session" />
<c:out value="${sessionScope.name}" /><br/>
<c:remove var="person" scope="session" />
<c:out value="${sessionScope.name}" /><br/>


prints, (the second <c:out> does not display anything)


Dinesh



JSTL 1.1 - <c:set> example

Example 1: Setting an attribute in some scope

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="userType" scope="request" value="premium" />
<c:out value="${requestScope.userType}" default="normal" />

prints,

premium


Example 2: setting JavaBean property
Note: PersonBean.java is a simple JavaBean with properties firstName and age


<%@ page import="info.icontraining.beans.PersonBean"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
 PersonBean p = new PersonBean();
 session.setAttribute("person", p);
%>
<c:set target="${sessionScope.person}" property="firstName" value="Dinesh" />
<c:set target="${sessionScope.person}" property="age" value="32" />
<c:out value="${sessionScope.person.firstName}" /><br/>
<c:out value="${sessionScope.person.age}" /><br/>


prints,

Dinesh
32


Example 2: setting a key-value pair in a Map
Note: PersonBean.java is a simple JavaBean with properties firstName and age

<%@ page import="java.util.*"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<% 
   Map m = new HashMap();
   session.setAttribute("map", m);
%>
<c:set target="${sessionScope.map}" property="firstName" value="Dinesh" />
<c:set target="${sessionScope.map}" property="lastName" value="PC" />
<c:out value="${sessionScope.map.firstName}" /><br/>
<c:out value="${sessionScope.map.lastName}" /><br/>

prints,

Dinesh
PC

JSTL 1.1 - <c:if> example


<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<% request.setAttribute("userType","premium"); %>
<c:if test="${requestScope.userType == 'premium'}" var="isPremiumMember" scope="session" >
   Welcome! You are a privileged customer.
</c:if>
<br/>
<c:out value="${sessionScope.isPremiumMember}" />

prints,

Welcome! You are a privileged customer.
true

JSTL 1.1 - <c:forEach> example

test.jsp

Example 1:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
   String[] arr = new String[] {"dog", "cat", "pig", "cow"};
   application.setAttribute("arrayAttribute", arr);
%>

<table border="1">
<c:forEach var="element" items="${arrayAttribute}" varStatus="c">
 <tr><td>${c.count}</td>
 <td>${element}</td></tr>
</c:forEach>
</table>

prints,


1dog
2cat
3pig
4cow


Example 2:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
   String[] arr = new String[] {"dog", "cat", "pig", "cow", "horse", "rhino", "hippo"};
   application.setAttribute("arrayAttribute", arr);
%>

<table border="1">
<c:forEach var="element" items="${arrayAttribute}" varStatus="c" begin="1" end="5" step="2">
 <tr><td>${c.count}</td>
 <td>${element}</td></tr>
</c:forEach>
</table>

prints,


1cat
2cow
3rhino

JSTL 1.1 - <c:out> example

test.jsp


Example 1:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:out value="<h1>Hello</h1>" escapeXml="false" />

prints:

Hello


Example 2:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:out value="<h1>Hello</h1>" escapeXml="true" />

prints:

<h1>Hello</h1>

Example 3:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<% session.setAttribute("cart",null); %>
<c:out value="${sessionScope.cart}" default="Shopping Cart is Empty" />

prints:

Shopping Cart is Empty  (when ${sessionScope.cart} evaluates to a null value)