Java jstl core tag if
The JSTL <c:if>
tag is used to conditionally execute a block of JSP code based on the evaluation of a boolean expression. This tag is part of the JSTL core tag library, and is typically used to generate dynamic HTML content based on the state of some variable or condition.
The basic syntax for the <c:if>
tag is as follows:
<c:if test="${condition}"> <!-- JSP code to execute if the condition is true --> </c:if>
The test
attribute of the <c:if>
tag is used to specify the boolean expression that should be evaluated. This attribute is required.
Here's an example that shows how to use the <c:if>
tag to conditionally display a message based on the value of a variable:
<c:set var="score" value="90" /> <c:if test="${score >= 80}"> <p>Congratulations, you passed the test!</p> </c:if>
In this example, the <c:set>
tag is used to create a variable called "score" with a value of 90. The <c:if>
tag is then used to conditionally display a message if the value of "score" is greater than or equal to 80.
The resulting output of this code will be:
<p>Congratulations, you passed the test!</p>
Note that the <c:if>
tag can also be used in conjunction with the <c:choose>
and <c:when>
tags to create more complex conditional statements. The <c:if>
tag also provides a number of other attributes for controlling the behavior of the tag, such as var
and scope
, which allow you to store the result of the boolean expression in a variable.