java spring表达式,Spring教程 - Spring表达式语言运算符

Spring教程 - Spring表达式语言运算符

Spring Expression Language支持标准的数学,逻辑或关系运算符。

Spring Expression Language支持标准的数学,逻辑或关系运算符。...

equal: ==, eq

not equal: !=, ne

less than:

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表达式语言中的运算符。...

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">

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标记中使用三元运算符。

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">

value="#{itemBean.qtyOnHand < 100 ? true : false}" />

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中使用相同的正则表达式

...

value="#{ ("abc" matches "\d+") == true ? "yes this is digit" : "No this is not a digit" }" />

value="#{emailBean.emailAddress matches "^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$" }" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值