Spring EL ternary operator (if-then-else) example

Spring EL supports ternary operator , perform “if then else” conditional checking. For example,


condition ? true : false

Spring EL in Annotation

Spring EL ternary operator with @Value annotation. In this example, if “itemBean.qtyOnHand” is less than 100, then set “customerBean.warning” to true, else set it to false.


package com.mkyong.core;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("customerBean")
public class Customer {

	@Value("#{itemBean.qtyOnHand < 100 ? true : false}")
	private boolean warning;

	public boolean isWarning() {
		return warning;
	}

	public void setWarning(boolean warning) {
		this.warning = warning;
	}

	@Override
	public String toString() {
		return "Customer [warning=" + warning + "]";
	}

}

package com.mkyong.core;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("itemBean")
public class Item {

	@Value("99")
	private int qtyOnHand;
	
	public int getQtyOnHand() {
		return qtyOnHand;
	}

	public void setQtyOnHand(int qtyOnHand) {
		this.qtyOnHand = qtyOnHand;
	}

}

Output


Customer [warning=true]

Spring EL in XML

See equivalent version in bean definition XML file.


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
	<bean id="customerBean" class="com.mkyong.core.Customer">
		<property name="warning" 
                          value="#{itemBean.qtyOnHand < 100 ? true : false}" />
	</bean>
 
	<bean id="itemBean" class="com.mkyong.core.Item">
		<property name="qtyOnHand" value="99" />
	</bean>
	
</beans>

Output


Customer [warning=true]

In XML, you need to replace less than operator "<" with "&lt;".

Download Source Code

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
5 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
4 Comment authors
NainasrinivasVivek PandeyRichardSpring Tutorial Recent comment authors
newest oldest most voted
Naina
Guest
Naina

Thanks your website is very helpful.
My code wasn’t working bcos I had no space around the ‘:’. Your example helped identify the problem.

Vivek Pandey
Guest
Vivek Pandey

FYI-

  <property name="warning"  value="#{itemBean.qtyOnHand < 100 ? true : false}" /> 

is not working .

@Mkyong can you explain me how we can use < operator here instead of < .??

Richard
Guest
Richard

In XML, you need to replace less than operator ‘<' with '<'.

Did you mean 'lt'?

srinivas
Guest
srinivas

xml doesnt suport the < operator so we use &lt instead of that.. it is explained here
http://www.mkyong.com/spring3/spring-el-operators-example/

trackback
Spring Tutorial

[…] example Spring EL suppots most of the standard relational, logical and mathematical operators.Spring EL ternary operator (if-then-else) example Conditional check , if else then.Spring EL Arrays, Lists, Maps example Works with Map and […]

trackback
Spring EL regular expression example

[…] – Spring3-EL-Regular-Expression-Example.zip (6 KB)ReferencesEmail regular expression exampleSpring EL ternary operator (if-then-else) example […]