Tuesday, February 12, 2013

5 JSTL Core IF Tag Examples in JSP - Tutorial


<c:if>  or if tag of JSTL core tag library in JSP is one of the most versatile and useful tag. JSTL if tag allows you
to test for a condition, like checking for a particular parameter in requestScope, sessionScope or pageScope. You can also  check any parameter in request parameters and headers or can check for a variable in JSP page using <c:if> tag. JSTL if tag helps a lot to reduce amount of Java code from JSP  page and if used, along with expression language JSTL core tag library, can remove almost all Java code from JSP files. Earlier we have seen examples of JSTL foreach tag and JSTL core set tag and this JSP JSTL tutorial is based on if tag of JSTL core tag library. We will, see how to use <core:if> tag inside JSP files and different example of <core:if> tag to get ourselves familiar with functionality and power offered by JSTL <c:if> tag. After seeing these examples of <core:if> tag along with expression language, You will be amazed to see, how clean your JSP looks like.

What is <core:if> tag in JSTL and JSP

How to use JSTL core if tag in JSP with examples tutorial<core:if> or <c:if> tag, based on prefix you choose on taglib declaration, is part of standard JSTL (Java Standard Tag libary). JSTL <core:if> tag test one condition and display or evaluate body content only if condition is true. If test expression results in false than content of <core:if> body is not evaluated. Here is syntax of JSTL if tag:

<core:if test="boolean expression" var="variable" scope="scope">
body content only be evaluated if test condition will be true
</core:if>

test attribute contains a boolean expression which is used to test a condition while attribute var and scope can be used to store the result of test into a particular variable specified by var and in a particular scope specified by scope. We will see example of JSTL if tag for storing test result in example section.

How to use JSTL IF tag in JSP
Here is a step by step guide to use JSTL <core:if> tag inside JSP file:

1) Download Java standard tag library or if you are using Maven along with Eclipse or Netbeans IDE then just import
dependency. Netbeans itself maintains set of libraries including JSTL, Spring and Struts, if you download there web package.

2) Include jstl.jar in application’s classpath. In web application, you should put JAR files on WEB-INF/lib folder, that folder is available on web application’s classpath.

3) Import JSTL core tag library in your JSP file by using <taglib> tag

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

Now you are ready to use JSTL if tag in your JSP  file , just follow sytax of <c:if> tag and you will be fine. Here prefix core will be used along with IF tag e.g. <core:if>.

JSTL if tag Examples in JSP
Here are some frequently used examples of <c:if> tag for checking request parameters, header values in JSP pages. This examples can be best used to remember syntax of JSTL if tag.

1) If condition with request parameter in JSP

<core:if test="${param.loan != null}">
How to get: <%=request.getParameter("loan")%>
</core:if>

This block of code in JSP will only execute, if request contains a parameter called loan and will not display anything if request parameter doesn't contain any parameter named loan.

2) If condition with header parameter in JSP
This is similar to above example of JSTL if tag. Only difference is, instead of request parameters now <core:if> will check headers for a particular parameter as shown in below code:

<core:if test="${header.cookie == null}">
Hello: ${header.cookie}
</core:if>

This JSTL example checks if cookie is present in HTTP header and display value of cookie if present.

3) How to check a particular variable in pageScope, requestScope and sessionScope in JSTL if tag.
In this JSTL If tag example, we have put a variable loan in request scope using <c:set> tag, which is another JSTL tag. Later we check for same variable using <c:if> tag.

<c:set var="loan" value="Property Loan" scope="request" />
<c:if test="${requestScope.loan != null }">
Loan type is ${requestScope.loan}
</c:if>

4) How to check for a bean value in JSP if tag
This if tag will only be executed if type property of loan object is "Personal Loan". This way you can check for any property of bean for particular value or null.

<c:if test="${loan.type == 'Personal Loan'}">
${loan}<br>
</c:if>

5) How to show  result of <c:if> tag test in JSP
You can export result of <c:if> tag condition check using var and scope. var is used to define name of variable which holds result of if tag evaluation and scope define scope of that variable. In following example, variable cookieTestResult will hold result of if tag evaluation in request scope.

<c:if test="${header.cookie == null}" var="cookieTestResult" scope="request">
Hello: ${header.cookie}
</c:if>

Result of Cookie Test:  <c:out value="${requestScope.cookieTestResult}"/>


Important points on JSTL <core:if> tag
Now we know How to use JSTL <c:if> tag, let’ revise some important things about this tag in JSP.

1) While using if tag from JSTL core library along with expression language its good to recap implicit variables available to Expression Language or EL in JSP. They are pageContext, pageScope, requestScope, sessionScope and applcationScope. They are self explanatory and represent Map holding request, session or application level attributes key is the name of attribute and value form Map is value of that attribute. Apart from these 5 we have 6 more maps called param, paramValues, header, headerValues, cookie and initParam. As name suggest they hold request and header parameters, name and values of cookie and init parameters.

2)  Also remember about dot(.) and bracket([]) operator in JSP expression language; dot is used to get property from object and bracket is used to get elements from collections and arrays.

That’s all on How to use <c:if> or JSTL If tag in JSP pages. For Java programmers, who sometime has added responsibility to code JSP as well, it’s difficult to remember syntax and options of various JSTL tags. These <c:if> tag example will help to quickly remember how to use JSTL if tag with full of its potential.


1 comment :

Anonymous said...

Best examples and information on JSTL Core IF tag, I have seen in the web. I didn't know that var and scope variables are used for storing result and there scope. Thanks for that. I also like to share one example of JSTL IF tag, which I used to test whether a particular variable is empty or not e.g. if you have a employeeId in request scope, then you can write something like :

<c:if test="${! empty requestScope.empId}">
<a href="detailedInfo.htm?empId=${requestScope.empId}">Employee Details</a>
</c:if>

Post a Comment