JSF EL nested ternary operator
Asked Answered
M

1

11

I have JSF 2.2, PrimeFaces 5.0 web application.

On my page I need to conditionally set <ui:param/>. The problem is that a standard ternary operator isn't enough, because I have more than 2 options to choose. This single page serves as an edit and create page for 2 types of users: regular user and a physician.

Here is what I tried:

<ui:param name="edit_title"
    value="#{empty dto.id  ? 
(bean.isPhysician ? msg.physicianNewTitle : msg.userNewTitle) :
(bean.isPhysician ? msg.physicianEditTitle : msg.userEditTitle)}" />

The result shows wrong title, when dto.id is empty and bean.isPhysician is false and I can't find a problem or a better solution. Tried to set it conditionally like it was said here, but it doesn't work for some reason.

Does anyone know how to solve this kind of trouble?

Every useful answer is highly appreciated and evaluated.

Thank you.

Mccary answered 8/8, 2014 at 13:55 Comment(4)
It might be better to move more complex logic into the controller. What is the expected and actual output in your example?Laclos
Output is ok except when I try to create a new user - it displays New Physician instead of New User.Mccary
Your logic looks correct. I would break it down into several statements for debugging, making sure the inputs are what you are expecting.Laclos
Try ? #{((empty dto.id) ? (bean.isPhysician ? msg.physicianNewTitle : msg.userNewTitle) : (bean.isPhysician ? msg.physicianEditTitle : msg.userEditTitle))} : sorry I cant helpDarcidarcia
M
24

Well, guys, I have found what was causing the wrong behavior and it has nothing to do with nested ternary operator. It was just wrong calculation of isPhysician variable.

So, if you need to choose from more than 2 conditions in a view - try making it like this:

<ui:param name="edit_title"
    value="#{empty dto.id  ? 
(bean.isPhysician ? msg.physicianNewTitle : msg.userNewTitle) :
(bean.isPhysician ? msg.physicianEditTitle : msg.userEditTitle)}" />

Or simply move your logic to a bean. Hope this helps somebody.

Cheers.

Mccary answered 11/8, 2014 at 6:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.