Adverts

The c:if tag is used where a simple decision needs to be made in the page. If the result of processing the test is true (either boolean true or the string true) the body of the tag is processed if it is anything else the body is skipped. This capability makes the c:if tag useful for optionally including pieces of content based on the current state of the system. A simple example of c:if usage is shown below:

<c:if test="${bean.value}">
  <p>Optional content</p>
</c:
if>

Here the tag is testing a simple boolean property in a bean but it could also use JSP Expression Language to perform a more complex check.

The big limitation of c:if is that it doesn't have the ability to make complex choices either the body is processed or it isn't. For if/else style functionality it is necessary to use the c:choose tag with c:when and c:otherwise. Which c:choose replicates all the functionality of c:if it is not a direct replacement because of readability. Using c:choose when you only need the binary choice provided by c:if would clutter the code with unnecessary lines making it harder to read.

Adverts

Donate and Help

Please support this site and
Bandwidth doesn't grow on trees y' know :o)

Adverts

Get Adsense