The representation of if-elseif-else in EL using JSF
Asked Answered
B

4

42

The following JSF code contains two separate <c:if></c:if>. Let's look at it.

<?xml version='1.0' encoding='UTF-8' ?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:c="http://java.sun.com/jsp/jstl/core">
    <h:head>
        <title>JSF EL</title>
    </h:head>
    <h:body>
        <h:form>

            <c:set scope="request" var="row" property="x" value="10"/>

            <c:if test="#{row==10}">
                <h:outputLabel value="value = 10"/>
            </c:if>

            <c:if test="#{row==15}">
                <h:outputLabel value="value = 15"/>
            </c:if>

        </h:form>
    </h:body>
</html>

It simply displays value=10 on the JSF page at run time. I need to represent the above same <c:if></c:if> with the following if-elseif-else (Java context).

if(row.equals(10))
{
    //Do something...(JSF stuff)
}
else if(row.equals(15))
{
    //Do something...(JSF stuff)
}
else
{
    //Do something...(JSF stuff)
}

How can it be represented with Expression Language (EL) using JSF?

Baseler answered 14/11, 2011 at 16:57 Comment(1)
possible duplicate of Conditionally displaying HTML elementsDidymium
V
49

The following code the easiest way:

 <h:outputLabel value="value = 10" rendered="#{row == 10}" /> 
 <h:outputLabel value="value = 15" rendered="#{row == 15}" /> 
 <h:outputLabel value="value xyz" rendered="#{row != 15 and row != 10}" /> 

Link for EL expression syntax. http://developers.sun.com/docs/jscreator/help/jsp-jsfel/jsf_expression_language_intro.html#syntax

Virtuosic answered 14/11, 2011 at 17:46 Comment(1)
It should be noted that the rendered attribute applies at the render phase which means that everything inside will be fully evaluated. If a large component is not rendered (like this) it could mean that a lot of code could be executed and the results is tossed away.Hypochondrium
H
59

You can use EL if you want to work as IF:

<h:outputLabel value="#{row==10? '10' : '15'}"/>

Changing styles or classes:

style="#{test eq testMB.test? 'font-weight:bold' : 'font-weight:normal'}"

class="#{test eq testMB.test? 'divRred' : 'divGreen'}"
Hypervitaminosis answered 14/8, 2013 at 14:42 Comment(1)
is there a similar way to use an if without else?Mohammed
V
49

The following code the easiest way:

 <h:outputLabel value="value = 10" rendered="#{row == 10}" /> 
 <h:outputLabel value="value = 15" rendered="#{row == 15}" /> 
 <h:outputLabel value="value xyz" rendered="#{row != 15 and row != 10}" /> 

Link for EL expression syntax. http://developers.sun.com/docs/jscreator/help/jsp-jsfel/jsf_expression_language_intro.html#syntax

Virtuosic answered 14/11, 2011 at 17:46 Comment(1)
It should be noted that the rendered attribute applies at the render phase which means that everything inside will be fully evaluated. If a large component is not rendered (like this) it could mean that a lot of code could be executed and the results is tossed away.Hypochondrium
A
15

You can use "ELSE IF" using conditional operator in expression language as below:

 <p:outputLabel value="#{transaction.status.equals('PNDNG')?'Pending':
                                     transaction.status.equals('RJCTD')?'Rejected':
                                     transaction.status.equals('CNFRMD')?'Confirmed':
                                     transaction.status.equals('PSTD')?'Posted':''}"/>
Alanis answered 7/9, 2015 at 5:6 Comment(0)
L
2

One possible solution is:

<h:panelGroup rendered="#{bean.row == 10}">
    <div class="text-success">
        <h:outputText value="#{bean.row}"/>
    </div>
</h:panelGroup>
Lacustrine answered 18/9, 2015 at 7:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.