How to use an OR condition in c:if in Facelets

facelets, jsf, jstl

Solution

Just follow the same logic as in normal Java:

if (privilege.equals("3") || privilege.equals("2") || privilege.equals("5"))

Thus, so:

<c:if test="#{privilege == '3' || privilege == '2' || privilege == '5'}">

You can use `eq` instead of `==` and `or` instead of `||` in EL.

<c:if test="#{privilege eq '3' or privilege eq '2' or privilege eq '5'}">

If you're on EL 3.0, then you can make use of new ability to declare inline collections via the new `#{[...]}` syntax for lists and `#{{...}}` syntax for sets. So, the below Java equivalent:

List<String> privileges = Arrays.asList("3", "2", "5");
if (privileges.contains(privilege))

Can in EL 3.0 be done like:

<c:if test="#{['3','2','5'].contains(privilege)}">

See also:

- Our EL wiki page

- Java EE 6 tutorial - Operators in EL

- Java EE 6 tutorial - Examples of EL expressions

- Java EE 7 tutorial - Operations on Collection objects

Problem

How can I use an OR condition in `<c:if>` in Facelets? I need to check for privlage if it is 3 or 2 or 5. ``` <c:if test="#{Privlage=='3'or '2' or '5'}" > ```

Original source