Conditionally Generating Output Using JSTL in a JSP Page

Any non-JSP code in a JSP file is called template text and is automatically written to the output stream. The JSTL tags <c:if> and <c:choose> make it possible to dynamically generate template text depending on a condition. The <c:if> tag generates its body if the expression in the test attribute evaluates to the boolean value true or the string value "true":
<%-- Declare the core library --%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>

<%-- Simple if conditions --%>
<c:if test='${param.p == "someValue"}'>
    Generate this template text if p equals someValue
</c:if>

<c:if test='${param.p}'>
    Generate this template text if p equals "true"
</c:if>
An if/else action requires the use of the <c:choose> tag:
<%-- A simple if/else condition --%>
<c:choose>
    <c:when test='${param.p == "someValue"}'>
        Generate this template text if p equals someValue
    </c:when>
    <c:otherwise>
        Otherwise generate this template text
    </c:otherwise>
</c:choose>
Multiple conditions can be evaluated using the <c:choose> tag:
<c:choose>
    <c:when test='${param.p == "0"}'>
        Generate this template text if p equals 0
    </c:when>
    <c:when test='${param.p == "1"}'>
        Generate this template text if p equals 1
    </c:when>
    <c:otherwise>
       Generate this template text if p equals anything else
    </c:otherwise>
</c:choose>
The <c:out> can also be used to conditionally generate template text. If the value attribute is null, a default value (if specified) is generated. The default value can be specified using the default attribute or can be specified in the body content. This example demonstrates both methods:
<%-- Default value in an attribute --%>
<c:out value='${param.p}' default="Generate this if p is null" />

<%-- Default value in the body content --%>
<c:out value='${param.p}'>
    Generate this if p is null
</c:out>

Comments

24 Feb 2010 - 12:03am by Anonymous (not verified)

Thank you.

24 Jun 2010 - 10:57am by Anonymous (not verified)

very good!!

29 Sep 2010 - 2:52am by Anonymous (not verified)

Thanks a lot, very helpfull!

27 Oct 2010 - 3:27am by Anonymous (not verified)

very helpful ..thank u very much

18 Apr 2011 - 8:50am by Anonymous (not verified)

This is really a very booby example.. thanks a lot

26 Jul 2011 - 11:38am by Anonymous (not verified)

Excellent guidance

5 Oct 2011 - 11:34pm by The North Face Store (not verified)

These boots are made from very flexible materials and the sole is generally made of rubber. At the beginning, the style of these boots were basic but as time went by, the style changed according to the style of the sports. They can be used for running.

2 Nov 2011 - 1:58am by Magrulla (not verified)

someone may want this..
checking multiple condition using if..

16 Nov 2011 - 8:27pm by Destrie (not verified)

You coldun't pay me to ignore these posts!

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.