3

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?

user2924127
  • 6,034
  • 16
  • 78
  • 136

1 Answers1

2

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555