Spring教程 - Spring表達(dá)式語(yǔ)言運(yùn)算符

2018-01-09 19:06 更新

Spring教程 - Spring表達(dá)式語(yǔ)言運(yùn)算符


Spring Expression Language支持標(biāo)準(zhǔn)的數(shù)學(xué),邏輯或關(guān)系運(yùn)算符。

Spring Expression Language支持標(biāo)準(zhǔn)的數(shù)學(xué),邏輯或關(guān)系運(yùn)算符。...

  • equal: ==, eq
  • not equal: !=, ne
  • less than: <, lt
  • less than or equal: <= , le
  • greater than: >, gt
  • greater than or equal: >=, ge

支持以下邏輯運(yùn)算符。

  • and
  • or
  • not (!)

支持以下邏輯運(yùn)算符。...

  • addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)
  • Exponential power (^)


例子

以下代碼顯示了如何使用Spring表達(dá)式語(yǔ)言中的運(yùn)算符。

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表達(dá)式語(yǔ)言中的運(yùn)算符。...

<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三元運(yùn)算符(if-then-else)

Spring表達(dá)式語(yǔ)言三元運(yùn)算符具有以下語(yǔ)法。并執(zhí)行if then else條件邏輯。

condition ? trueAction : falseAction

如果 condition 為true,它將執(zhí)行 trueAction ,如果條件為假,它將運(yùn)行 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 注釋的三元運(yùn)算符。如果“itemBean.qtyOnHand"小于100,則將“customerBean.warning"設(shè)置為true,否則將其設(shè)置為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標(biāo)記中使用三元運(yùn)算符。

<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 &lt; 100 ? true : false}" />
  </bean>
</beans>

Spring EL三元運(yùn)算符(if-then-else)...

以下Java bean具有將通過(guò)使用驗(yàn)證的電子郵件字段正則表達(dá)式。

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;
}

以下代碼使用正則表達(dá)式驗(yàn)證數(shù)字并存儲(chǔ)導(dǎo)致布爾值。

  // if this is a digit?
  @Value("#{"1" matches "\\d+" }")
  private boolean validDigit;

下面的代碼使用三元運(yùn)算符中的正則表達(dá)式。

  @Value("#{ ("abc" matches "\\d+") == true ? " +
      ""yes this is digit" : "No this is not a digit"  }")
  private String msg;

以下代碼使用正則表達(dá)式驗(yàn)證電子郵件地址從另一個(gè)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中使用相同的正則表達(dá)式

...
  <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>
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)