Spring 专题
专题目录
您的位置:java > Spring专题 > Spring EL三元运算(if-then-else)实例
Spring EL三元运算(if-then-else)实例
作者:--    发布时间:2019-11-20
spring el支持三元运算符,执行“if then else”条件检查。 例如,
condition ? true : false

spring el以注解形式

spring el三元运算符可使用@value注解。在这个例子中,如果“itembean.qtyonhand”小于100,则设置“customerbean.warning”为true,否则将其设置为false。
package com.h3.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.h3.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;
	}

}

输出

customer [warning=true]

spring el以xml形式

请参阅在xml文件定义bean的等效版本。
<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.h3.core.customer">
		<property name="warning" 
                          value="#{itembean.qtyonhand < 100 ? true : false}" />
	</bean>
 
	<bean id="itembean" class="com.h3.core.item">
		<property name="qtyonhand" value="99" />
	</bean>
	
</beans>

输出结果

customer [warning=true]
在xml中,需要小于运算符使用"&lt;"替换“<”。


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册