使用SpringEL表达式进行三目运算
程序员文章站
2022-11-22 11:38:10
原文链接:http://www.yiidian.com/spring/spring el three mesh operator.html SpEL支持三目运算符,以此来实现条件语句。 一、编写Bean类 Item类: Customer类: 二、配置applicationContext.xml 三、 ......
原文链接:http://www.yiidian.com/spring/spring-el-three-mesh-operator.html
spel支持三目运算符,以此来实现条件语句。
一、编写bean类
item类:
package com.yiidian.domain; /** * * @author http://www.yiidian.com * */ public class item { private int qtyonhand; public int getqtyonhand() { return qtyonhand; } public void setqtyonhand(int qtyonhand) { this.qtyonhand = qtyonhand; } }
customer类:
package com.yiidian.domain; import java.io.serializable; /** * * @author http://www.yiidian.com * */ public class customer implements serializable{ private boolean warning; public boolean iswarning() { return warning; } public void setwarning(boolean warning) { this.warning = warning; } @override public string tostring() { return "customer [warning=" + warning + "]"; } }
二、配置applicationcontext.xml
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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.xsd"> <bean id="customer" class="com.yiidian.domain.customer"> <property name="warning" value="#{itembean.qtyonhand < 100 ? true : false}" /> </bean> <bean id="itembean" class="com.yiidian.domain.item"> <property name="qtyonhand" value="99" /> </bean> </beans>
三、编写测试类
package com.yiidian.test; import org.junit.test; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import com.yiidian.domain.customer; /** * @author http://www.yiidian.com * */ public class demo1 { @test public void test1() { applicationcontext context = new classpathxmlapplicationcontext("applicationcontext.xml"); customer customer = (customer)context.getbean("customer"); system.out.println(customer); } }
四、运行结果
源码下载:http://pan.baidu.com/s/1qymlkfe
欢迎关注我的公众号::一点教程。获得独家整理的学习资源和日常干货推送。
如果您对我的系列教程感兴趣,也可以关注我的网站: