JSTL : Java Standard Tag Library
Use this taglib for to use basic tags of JSTL in jsp
<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
<c:out>
<c:out value=’${user}’ default=’guest’ />
or
<c:out value=’${user}’>guest</c:out>
<c:forEach>
<c:forEach var=”movie” items=”${movieList}” varStatus=”movieLoopCount” >
${movieLoopCount.count} : ${movie}
</c:forEach>
varStatus attribute makes a new variable that holds an instance of
javax.servlet.jsp.jstl.core.LoopTagStatus
LoopTagStatus class has a count property that gives you the current value
of the iteration counter. (Like the “i” in a for loop.)
<c:if>
<c:if test=”${userType eq ‘member’ }” >
inside if
</c:if>
<c:choose> and <c:when> and <c:otherwise>
<c:choose>
<c:when test=”${userPref == ‘performance’}”>
PERFORMANCE
</c:when>
<c:when test=”${userPref == ‘safety’}”>
SAFETY
</c:when>
<c:otherwise>
ELSE
</c:otherwise>
</c:choose>
<c:set>
No Body
<c:set var=”userLevel” scope=”session” value=”Cowboy” />
If there is no session level variable named userLevel then this tag will create a new one with value CorBoy
var is required attribute
scope is optional
value doesnt have to be a string only.
<c:set var=”Fido” value=”${person.dog}” />
With Body
<c:set var=”userLevel” scope=”session” >
Sheriff, Bartender, Cowgirl
</c:set>
Here the body is evaluated and used as a value of the userLevel variable.
Note : If the value evaluates to null then the variable will be removed. Yes removed !!
<c:set target="">
NO BODY
<c:set target=”${PetMap}” property=”dogName” value=”Clover” />
target must not be null
If target is a Map, set value of a key named "dogName”.
If target is bean then set value for the property name "dogName"
WITH BODY
<c:set target=”${PetMap}” property=”dogName” >
Clover
</c:set>
Points to Remember
You can never have BOTH the “var” and “target”
attributes in a <c:set>.
“Scope” is optional, but if you don’t use it the default
is page scope.
If the “value” is null, the attribute named by “var”
will be removed!
If the attribute named by “var” does not exist, it’ll be
created, but only if “value” is not null.
If the “target” expression is null, the Container
throws an exception.
The “target” is for putting in an expression that
resolves to the Real Object. If you put in a String
literal that represents the “id” name of the bean or
Map, it won’t work. In other words, “target” is not for
the attribute name of the bean or Map—it’s for the
actual attribute object.
If the “target” expression is not a Map or a bean, the
Container throws an exception.
If the “target” expression is a bean, but the bean
does not have a property that matches “property”,
the Container throws an exception. Remember that
the EL expression ${bean.notAProperty} will also
throw an exception.
<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
<c:out>
<c:out value=’${user}’ default=’guest’ />
or
<c:out value=’${user}’>guest</c:out>
<c:forEach>
<c:forEach var=”movie” items=”${movieList}” varStatus=”movieLoopCount” >
${movieLoopCount.count} : ${movie}
</c:forEach>
varStatus attribute makes a new variable that holds an instance of
javax.servlet.jsp.jstl.core.LoopTagStatus
LoopTagStatus class has a count property that gives you the current value
of the iteration counter. (Like the “i” in a for loop.)
<c:if>
<c:if test=”${userType eq ‘member’ }” >
inside if
</c:if>
<c:choose> and <c:when> and <c:otherwise>
<c:choose>
<c:when test=”${userPref == ‘performance’}”>
PERFORMANCE
</c:when>
<c:when test=”${userPref == ‘safety’}”>
SAFETY
</c:when>
<c:otherwise>
ELSE
</c:otherwise>
</c:choose>
<c:set>
No Body
<c:set var=”userLevel” scope=”session” value=”Cowboy” />
If there is no session level variable named userLevel then this tag will create a new one with value CorBoy
var is required attribute
scope is optional
value doesnt have to be a string only.
<c:set var=”Fido” value=”${person.dog}” />
With Body
<c:set var=”userLevel” scope=”session” >
Sheriff, Bartender, Cowgirl
</c:set>
Here the body is evaluated and used as a value of the userLevel variable.
Note : If the value evaluates to null then the variable will be removed. Yes removed !!
<c:set target="">
NO BODY
<c:set target=”${PetMap}” property=”dogName” value=”Clover” />
target must not be null
If target is a Map, set value of a key named "dogName”.
If target is bean then set value for the property name "dogName"
WITH BODY
<c:set target=”${PetMap}” property=”dogName” >
Clover
</c:set>
Points to Remember
You can never have BOTH the “var” and “target”
attributes in a <c:set>.
“Scope” is optional, but if you don’t use it the default
is page scope.
If the “value” is null, the attribute named by “var”
will be removed!
If the attribute named by “var” does not exist, it’ll be
created, but only if “value” is not null.
If the “target” expression is null, the Container
throws an exception.
The “target” is for putting in an expression that
resolves to the Real Object. If you put in a String
literal that represents the “id” name of the bean or
Map, it won’t work. In other words, “target” is not for
the attribute name of the bean or Map—it’s for the
actual attribute object.
If the “target” expression is not a Map or a bean, the
Container throws an exception.
If the “target” expression is a bean, but the bean
does not have a property that matches “property”,
the Container throws an exception. Remember that
the EL expression ${bean.notAProperty} will also
throw an exception.