Conditional variable definition in JSF
Asked Answered
B

2

7

i want to change the variable value based on a condition so i tried the following:

<h:head>

    <ui:param name="userCase" value="Insert" />

    <ui:fragment rendered="#{employee.employeesBulkInsert==false}">
       <ui:param name="userCase" value="Update" />
    </ui:fragment>

       <title>#{userCase} Employee </title>

</h:head>

but it doesn't work in the update case, it shows an empty string, any ideas why ?

i know that there are other solutions like defining the variable in the backing bean, or make the conditional ui fragment on the title tag directly, but i want to know why the above is not working, please advise, thanks.

Berkly answered 6/12, 2011 at 12:45 Comment(2)
It's very good practice to say what behaviour you are experiencing (an error? What's the message? Not the output you expect? What you were expecting?), not just "doesn't work".Tuxedo
sure, i will update the question :)Berkly
C
13

The <ui:fragment> is a render-time tag while the <ui:param> is a tag handler (tag handlers are easily recognizeable by the absence of the rendered attribute). So when the view get built, the <ui:param> is set, regardless of the outcome of <ui:fragment>. The rendered attribute of <ui:fragment> is only evaluated when the already-built view is to be rendered.

You want to make the condition using a tag handler instead, such as JSTL <c:if>.

<ui:param name="userCase" value="Insert" />

<c:if test="#{not employee.employeesBulkInsert}">
    <ui:param name="userCase" value="Update" />
</c:if>

(note that I removed the unnecessary boolean ==false comparison)

Cubit answered 6/12, 2011 at 12:56 Comment(0)
S
2

Try using JSTL instead:

<c:set var="userCase" value="Insert" />
<c:if test="#{employee.employeesBulkInsert}">
    <c:set var="userCase" value="Update" />
</c:if>

Tutorial here: http://www.ibm.com/developerworks/java/library/j-jstl0211/index.html

Sup answered 6/12, 2011 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.