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;".

Spring EL ternary operator (if-then-else) example的更多相关文章

  1. Spring EL regular expression example

    Spring EL supports regular expression using a simple keyword "matches", which is really aw ...

  2. 把功能强大的Spring EL表达式应用在.net平台

    Spring EL 表达式是什么? Spring3中引入了Spring表达式语言—SpringEL,SpEL是一种强大,简洁的装配Bean的方式,他可以通过运行期间执行的表达式将值装配到我们的属性或构 ...

  3. Spring3系列6 - Spring 表达式语言(Spring EL)

    Spring3系列6-Spring 表达式语言(Spring EL) 本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.S ...

  4. Test Spring el with ExpressionParser

    Spring expression language (SpEL) supports many functionality, and you can test those expression fea ...

  5. Spring EL Lists, Maps example

    In this article, we show you how to use Spring EL to get value from Map and List. Actually, the way ...

  6. Spring EL Operators example

    Spring EL supports most of the standard mathematical, logical or relational operators. For example, ...

  7. Spring EL method invocation example

    In Spring EL, you can reference a bean, and nested properties using a 'dot (.)' symbol. For example, ...

  8. Spring EL hello world example

    The Spring EL is similar with OGNL and JSF EL, and evaluated or executed during the bean creation ti ...

  9. (ternary operator)三元运算符.

    ternary operator: advantage: make a terse simple conditional assignment statement of if-then-else. d ...

随机推荐

  1. POJ 3185 The Water Bowls(高斯消元-枚举变元个数)

    题目链接:http://poj.org/problem?id=3185 题意:20盏灯排成一排.操作第i盏灯的时候,i-1和i+1盏灯的状态均会改变.给定初始状态,问最少操作多少盏灯使得所有灯的状态最 ...

  2. 将SQLServer2005中的数据同步到Oracle中

    有时由于项目开发的需要,必须将SQLServer2005中的某些表同步到Oracle数据库中,由其他其他系统来读取这些数据.不同数据库类型之间的数据同步我们可以使用链接服务器和SQLAgent来实现. ...

  3. Android开发之assets文件夹中资源的获取

    assets中的文件都是保持原始的文件格式,需要使用AssetManager以字节流的形式读取出来 步骤: 1. 先在Activity里面调用getAssets() 来获取AssetManager引用 ...

  4. HDU 4869 Turn the pokers (2014 Multi-University Training Contest 1)

    Turn the pokers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. BZOJ2299: [HAOI2011]向量

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2299 题解:乱搞就可以了... 不妨认为有用的只有(a,b)(a,-b)(b,a)(b,-a) ...

  6. 注解框架ButterKnife

    将插件升级到1.3后支持Android Studio1.3 + ButterKnife7 如何使用 有所使用的布局 ID 上点击右键 (例如上图中的 R.layout.activity_setting ...

  7. listview默认选择第一项,点击换子项背景图

    (不是大神,没有几百子项目,去你丫的) private int last_item_position ; @Override public void onItemClick(AdapterView&l ...

  8. [转帖]Asp.NET 弹出页面

    原文链接:http://www.cnblogs.com/adi-liu/archive/2008/07/18/1246091.html ASP.NET 弹出对话框和页面之间传递值的经验总结 今天碰到一 ...

  9. ionic安装拍照选照片插件

    1.安装插件,也可以用ionic plugin add .... phonegap local plugin add https://git-wip-us.apache.org/repos/asf/c ...

  10. qt创建android项目后需要加入的参数

    默认用qtcreator5.2.0创建了一个quick项目,却报如下错误: error:cstdlib.h no such file or directory 解决方法: 打开项目文件untitled ...