Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluate empty or null JSTL c tags

Tags:

jsp

jstl

el

How can I validate if a String is null or empty using the c tags of JSTL?

I have a variable of name var1 and I can display it, but I want to add a comparator to validate it.

<c:out value="${var1}" /> 

I want to validate when it is null or empty (my values are strings).

like image 759
user338381 Avatar asked May 11 '10 14:05

user338381


People also ask

Which tag is used in JSTL to show the output?

The <c:out> tag is similar to JSP expression tag, but it can only be used with expression. It will display the result of an expression, similar to the way < %=... % > work.

What is C set in JSTL?

JSTL Core Tags c:set allows to set the result of an expression in a variable within a given scope. Using this tag helps to set the property of 'JavaBean' and the values of the 'java.

How do remove variable using C Set tag from JSTL?

JSTL - Core <c:remove> TagThe <c:remove> tag removes a variable from either a specified scope or the first scope where the variable is found (if no scope is specified). This action is not particularly helpful, but it can aid in ensuring that a JSP cleans up any scoped resources it is responsible for.

How check list is empty or not in JSTL?

The first way is to use the JSTL tag and empty operator to check if an ArrayList is empty and the second way is to use the JSTL function, fn: length() instead of the empty operator as shown in our example.


1 Answers

How can I validate if a String is null or empty using the c tags of JSTL?

You can use the empty keyword in a <c:if> for this:

<c:if test="${empty var1}">     var1 is empty or null. </c:if> <c:if test="${not empty var1}">     var1 is NOT empty or null. </c:if> 

Or the <c:choose>:

<c:choose>     <c:when test="${empty var1}">         var1 is empty or null.     </c:when>     <c:otherwise>         var1 is NOT empty or null.     </c:otherwise> </c:choose> 

Or if you don't need to conditionally render a bunch of tags and thus you could only check it inside a tag attribute, then you can use the EL conditional operator ${condition? valueIfTrue : valueIfFalse}:

<c:out value="${empty var1 ? 'var1 is empty or null' : 'var1 is NOT empty or null'}" /> 

To learn more about those ${} things (the Expression Language, which is a separate subject from JSTL), check here.

See also:

  • How does EL empty operator work in JSF?
like image 156
BalusC Avatar answered Sep 19 '22 17:09

BalusC