Java Reference
In-Depth Information
Core JSTL Tags
NetBeans allows us to easily use three core JSTL tags, the
<c:if>
tag used to
conditionally render segments of a page; the
<c:choose>
tag, which allows us
to render part of a page differently based on a boolean condition; and the
<c:forEach>
tag, which allows us to iterate through an instance of a class
implementing
java.util.Collection
or through an array. These tags can be
dragged from the NetBeans palette into our page.
Before we can use core JSTL tags, we need to add a
taglib directive
to our page. The
taglib directive tells our JSP that we will be using custom tags in the page. For core
JSTL tags, the taglib directive looks like this:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
The value of the
prefix
attribute is the prefix to be used before any tags in the
tag library. By convention, the prefix of
c
is used for JSTL core tags. The value
of the
uri
attribute is a
Unique Resource Identifier
that will let our page know
where to find the custom tags. Each tag library defines its URI in a tag library
descriptor file. For JSTL core tags, the value of the
uri
attribute must always be
http://java.sun.com/jsp/jstl/core
.
Conditionally Displaying Part of a Page with
the <c:if> Tag
The JSTL
<c:if>
tag allows us to conditionally display or hide part of a page, based
on a boolean condition. With NetBeans, all we need to do to add a JSTL
<c:if>
tag
to one of our JSP pages is to drag the
JSTL If
item from the palette to the location in
our page where we wish to place the tag.