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. Android XML使用的学习记录

    1. 注释其中一段代码或是一行,可以采用<!-- -->,示例如下 <!--       <EditText         android:layout_width=&quo ...

  2. C#调用java程序

    前言: 最近跟项目组的人合作一个项目,由于之前我用的是java写的一个与android通信的程序,现在另一个同事来编写界面程序,由于C#编写起来比较方便,而我又不想重新写之前java的那段代码,于是需 ...

  3. 关于BigDecimal的四舍五入和截断 (2007-08-10 15:06:26)

    关于四舍五入:ROUND_HALF_UP: 遇到.5的情况时往上近似,例: 1.5 ->;2ROUND_HALF_DOWN : 遇到.5的情况时往下近似,例: 1.5 ->;1 BigDe ...

  4. Java Web编程的主要组件技术——Hibernate入门

    参考书籍:<J2EE开源编程精要15讲> Hibernate是对象/关系映射(ORM,Object/Relational Mapping)的解决方案,就是将Java对象与对象关系映射到关系 ...

  5. Data Binding(数据绑定)用户指南

    1)介绍 这篇文章介绍了如何使用Data Binding库来写声明的layouts文件,并且用最少的代码来绑定你的app逻辑和layouts文件. Data Binding库不仅灵活而且广泛兼容- 它 ...

  6. 移动对meta的定义

    以下是meta每个属性详解 尤其要注意的是content里多个属性的设置一定要用分号+空格来隔开,如果不规范将不会起作用. 一.<meta http-equiv="Content-Ty ...

  7. asp.net MVC 应用程序的生命周期(下)

    看看上面的UrlRoutingModule源码里面是怎么实现Init方法的,Init()方法里面我标注红色的地方: application.PostResolveRequestCache += new ...

  8. golang学习遭遇duang...duang...duang

    初学golang时,在windows上使用liteIDE进行,很多语法都能自己调整. 后来使用linux桌面,再次编写时,发现很多东西都忘掉了.这难道就是习惯gocode后的弊端吗?还是人到 前中年 ...

  9. IOS 疑问记录

    1. NSThread  中的  NSRunLoop 2. NSThread  中的 performSelector:onThread:withObject:waitUntilDone:

  10. 《Python基础教程(第二版)》学习笔记 -> 第十章 充电时刻 之 标准库

    SYS sys这个模块让你能够访问与Python解释器联系紧密的变量和函数,下面是一些sys模块中重要的函数和变量: 函数和变量 描述 argv 命令行参数,包括脚本和名称 exit([arg])   ...