Spring教程 - Spring表达式语言运算符
Spring教程 - Spring表达式语言运算符
Spring Expression Language支持标准的数学,逻辑或关系运算符。
Spring Expression Language支持标准的数学,逻辑或关系运算符。...
- equal: ==, eq
- not equal: !=, ne
- less than: <, lt
- less than or equal: <= , le
- greater than: >, gt
- greater than or equal: >=, ge
支持以下逻辑运算符。
- and
- or
- not (!)
支持以下逻辑运算符。...
- addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
- Exponential power (^)
例子
以下代码显示了如何使用Spring表达式语言中的运算符。
package com.java2s.core; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("customerBean") public class Server { //Relational operators @Value("#{1 == 1}") //true private boolean testEqual; @Value("#{1 != 1}") //false private boolean testNotEqual; @Value("#{1 < 1}") //false private boolean testLessThan; @Value("#{1 <= 1}") //true private boolean testLessThanOrEqual; @Value("#{1 > 1}") //false private boolean testGreaterThan; @Value("#{1 >= 1}") //true private boolean testGreaterThanOrEqual; //Logical operators , numberBean.no == 999 @Value("#{numberBean.no == 999 and numberBean.no < 900}") //false private boolean testAnd; @Value("#{numberBean.no == 999 or numberBean.no < 900}") //true private boolean testOr; @Value("#{!(numberBean.no == 999)}") //false private boolean testNot; //Mathematical operators @Value("#{1 + 1}") //2.0 private double testAdd; @Value("#{"1" + "@" + "1"}") //1@1 private String testAddString; @Value("#{1 - 1}") //0.0 private double testSubtraction; @Value("#{1 * 1}") //1.0 private double testMultiplication; @Value("#{10 / 2}") //5.0 private double testDivision; @Value("#{10 % 10}") //0.0 private double testModulus ; @Value("#{2 ^ 2}") //4.0 private double testExponentialPower; }
例子...
以下代码显示了如何使用Spring表达式语言中的运算符。...
<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.java2s.core.Customer"> <property name="testEqual" value="#{1 == 1}" /> <property name="testNotEqual" value="#{1 != 1}" /> <property name="testLessThan" value="#{1 lt 1}" /> <property name="testLessThanOrEqual" value="#{1 le 1}" /> <property name="testGreaterThan" value="#{1 > 1}" /> <property name="testGreaterThanOrEqual" value="#{1 >= 1}" /> <property name="testAnd" value="#{numberBean.no == 999 and numberBean.no lt 900}" /> <property name="testOr" value="#{numberBean.no == 999 or numberBean.no lt 900}" /> <property name="testNot" value="#{!(numberBean.no == 999)}" /> <property name="testAdd" value="#{1 + 1}" /> <property name="testAddString" value="#{"1" + "@" + "1"}" /> <property name="testSubtraction" value="#{1 - 1}" /> <property name="testMultiplication" value="#{1 * 1}" /> <property name="testDivision" value="#{10 / 2}" /> <property name="testModulus" value="#{10 % 10}" /> <property name="testExponentialPower" value="#{2 ^ 2}" /> </bean> </beans>
Spring EL三元运算符(if-then-else)
Spring表达式语言三元运算符具有以下语法。并执行if then else条件逻辑。
condition ? trueAction : falseAction
如果 condition
为true,它将执行 trueAction
,如果条件为假,它将运行 falseAction
。
以下Java bean具有值为99的 qtyOnHand
的属性值。
package com.java2s.core; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("itemBean") public class Item { @Value("99") private int qtyOnHand; }
Customer bean使用具有 @Value
注释的三元运算符。如果“itemBean.qtyOnHand"小于100,则将“customerBean.warning"设置为true,否则将其设置为false。
package com.java2s.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; }
以下xml配置文件显示如何在xml标记中使用三元运算符。
<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="itemBean" class="com.java2s.core.Item"> <property name="qtyOnHand" value="99" /> </bean> <bean id="customerBean" class="com.java2s.core.Customer"> <property name="warning" value="#{itemBean.qtyOnHand < 100 ? true : false}" /> </bean> </beans>
Spring EL三元运算符(if-then-else)...
以下Java bean具有将通过使用验证的电子邮件字段正则表达式。
package com.java2s.core; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("emailBean") public class Email { @Value("your email here") String emailAddress; }
以下代码使用正则表达式验证数字并存储导致布尔值。
// if this is a digit? @Value("#{"1" matches "\\d+" }") private boolean validDigit;
下面的代码使用三元运算符中的正则表达式。
@Value("#{ ("abc" matches "\\d+") == true ? " + ""yes this is digit" : "No this is not a digit" }") private String msg;
以下代码使用正则表达式验证电子邮件地址从另一个Java bean。
// email regular expression String emailRegEx = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)" + "*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; @Value("#{emailBean.emailAddress matches customerBean.emailRegEx}") private boolean validEmail;
在xml中使用相同的正则表达式
... <bean id="customerBean" class="com.java2s.core.Customer"> <property name="validDigit" value="#{"1" matches "\d+" }" /> <property name="msg" value="#{ ("abc" matches "\d+") == true ? "yes this is digit" : "No this is not a digit" }" /> <property name="validEmail" value="#{emailBean.emailAddress matches "^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$" }" /> </bean> <bean id="emailBean" class="com.java2s.core.Email"> <property name="emailAddress" value="your email" /> </bean> </beans>
更多建议: