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?
Expression Language (EL), is a scripting language that's seen adoption within many Java frameworks, such as Spring with SpEL and JBoss with JBoss EL. In this article, we'll focus at the JSF's implementation of this scripting language – Unified EL.
An if statement is written with the if keyword, followed by a condition in parentheses, with the code to be executed in between curly brackets. In short, it can be written as if () {} .
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'}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With