Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conditional statement with expression language that is not ternary

Tags:

el

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?

like image 224
user2924127 Avatar asked Sep 01 '25 21:09

user2924127


1 Answers

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.

like image 123
BalusC Avatar answered Sep 10 '25 06:09

BalusC