user2924127
user2924127

Reputation: 6252

conditional statement with expression language that is not ternary

I need to make an expression language conditional statement with a few conditions to check. Googling I can only find examples using ternary

#{SomeBean.someProperty ? 'bob' : 'John'}

I need to have more conditions though. I need something like:

If (SomeBean.someProperty == 'a'){
   //Ant
}
Else if (SomeBean.someProperty == 'b'){
   //Bob
}
Else if (SomeBean.someProperty == 'c'){
   //C++
}
Else{
   //Back to the drawing board, something went wrong.
}

How can I write this in expression language?

Upvotes: 4

Views: 2099

Answers (1)

BalusC
BalusC

Reputation: 1109232

Just the same syntax as in plain Java.

#{bean.property eq 'a' ? 'Ant' : bean.property eq 'b' ? 'Bob' : bean.property eq 'c' ? 'C++' : null}

Do note that property is assumed to be String or enum and not char because a char is interpreted the same way as numbers in EL. See also How to compare a char property in EL.

Upvotes: 3

Related Questions