How do I check two or more conditions in one <c:if>?
Asked Answered
H

4

107

How do I check two conditions in one <c:if>? I tried this, but it raises an error:

<c:if test="${ISAJAX == 0} && ${ISDATE == 0}"> 
Hafler answered 2/12, 2011 at 11:40 Comment(0)
E
166

This look like a duplicate of JSTL conditional check.

The error is having the && outside the expression. Instead use

<c:if test="${ISAJAX == 0 && ISDATE == 0}">
Entrenchment answered 2/12, 2011 at 11:44 Comment(0)
G
36

If you are using JSP 2.0 and above It will come with the EL support: so that you can write in plain english and use and with empty operators to write your test:

<c:if test="${(empty object_1.attribute_A) and (empty object_2.attribute_B)}">
German answered 24/4, 2014 at 8:46 Comment(0)
N
21

Recommendation:

when you have more than one condition with and and or is better separate with () to avoid verification problems

<c:if test="${(not validID) and (addressIso == 'US' or addressIso == 'BR')}">
Naif answered 16/9, 2015 at 13:13 Comment(0)
L
2

Just in case somebody needs to check the condition from session.Usage of or

<c:if test="${sessionScope['roleid'] == 1 || sessionScope['roleid'] == 4}">
Lardon answered 24/10, 2018 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.