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

时间:2023-02-05 07:28:38

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表达式应用在&period;net平台

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

  3. Spring3系列6 - Spring 表达式语言&lpar;Spring EL&rpar;

    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&comma; 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. &lpar;ternary operator&rpar;三元运算符&period;

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

随机推荐

  1. Java-集合(没做出来)第四题 (List)写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列。 例如: List list &equals; new ArrayList&lpar;&rpar;&semi; list&period;add&lpar;&OpenCurlyDoubleQuote;Hello”&rpar;&semi; list&period;add&lpar;&OpenCurlyDoubleQuote;World”&rpar;&semi; list&period;add&lpar;&OpenCurlyDoubleQuote;Learn”&rpar;&semi; &sol;&sol;此时list 为Hello World Learn reverseL

    没做出来 第四题 (List)写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列. 例如: List list = new ArrayList(); list.a ...

  2. &lbrack;官方教程&rsqb; &lbrack;ES4封装教程&rsqb;2&period;使用 Easy Sysprep v4 封装 Windows XP

    (一)备份当前操作系统封装的第一步,其实是备份当前安装好的操作系统.避免我们在之后的步骤中出现问题,以至于还要重新安装操作系统,浪费时间精力.系统备份想必大家都会.对于WinXP而言,建议使用Ghos ...

  3. mina学习(一)

    Mina:是一个帮助用户开发高性能和高伸缩性网络应用程序的框架 学习地址链接:http://www.cnblogs.com/xuekyo/archive/2013/03/06/2945826.html ...

  4. 127&period; 126&period; Word Ladder &ast;HARD&ast; -- 单词每次变一个字母转换成另一个单词

    127. Given two words (beginWord and endWord), and a dictionary's word list, find the length of short ...

  5. ENTBOOST 2014&period;180L 发布,开源企业IM免费企业即时通讯

    ENTBOOST,VERSION 2014.180 Linux版本发布,主要增加企业IM应用集成功能,完善安卓SDK功能及部分BUG修正: 7/1(明天)发布Windows版本,敬请关注! ENTBO ...

  6. Feel Good

    poj2796:http://poj.org/problem?id=2796 题意:给出一个长度为n(n<100000)的序列,求出一个子序列,使得这个序列中的最小值乘以这个序列的和的值最大. ...

  7. 【转】Vim学习资料

    初学资料:1:一个介绍VIM操作的游戏,十分适合初学者.只是:不要怕英文.vim-adventures.com2:http://blog.csdn.net/niushuai666/article/de ...

  8. js 继承的简单易懂小例子

    js 继承 今天主要说原型链继承.构造继承.组合继承三种常用继承方式,分享一下我的理解. 原型链继承例子1 //原型继承function A(name){ this.name = name;}func ...

  9. x多进程

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' from multiprocessing import Process import os #子进 ...

  10. zabbix的日常监控-自动化监控(十一)

    自动化监控: 1.自动注册 1.1.zabbix agent自动添加 2.主动发现 2.1.自动发现Discover 2.2.zabbix api 自动发现与自动注册,哪一个更好? 共同的特点均可以添 ...