SpringEL-运算符

Spring EL 支持大多数的数学操作符、逻辑操作符、关系操作符。

1、关系操作符

  包括:等于 (==, eq),不等于 (!=, ne),小于 (<, lt),,小于等于(<= , le),大于(>, gt),大于等于 (>=, ge)

2、逻辑操作符

  包括:and,or,and not(!)

3、数学操作符

  包括:加 (+),减 (-),乘 (*),除 (/),取模 (%),幂指数 (^)。

一、编写Bean类

Number类:

package com.yiidian.domain;
/**
 * @author http://www.yiidian.com
 * 
 */
public class Number {
	private int no;
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
}

Customer类: 

package com.yiidian.domain;

import java.io.Serializable;
/**
 * 
 * @author http://www.yiidian.com
 *
 */
public class Customer implements Serializable{
	    private boolean testEqual;
	 
	    private boolean testNotEqual;
	 
	    private boolean testLessThan;
	 
	    private boolean testLessThanOrEqual;
	 
	    private boolean testGreaterThan;
	 
	    private boolean testGreaterThanOrEqual;
	 
	    private boolean testAnd;
	 
	    private boolean testOr;
	 
	    private boolean testNot;
	 
	    private double testAdd;
	 
	    private String testAddString;
	 
	    private double testSubtraction;
	 
	    private double testMultiplication;
	 
	    private double testDivision;
	 
	    private double testModulus ;
	 
	    private double testExponentialPower;
	 
	    @Override
	    public String toString() {
	        return "Customer [testEqual=" + testEqual + ", testNotEqual="
	                + testNotEqual + ", testLessThan=" + testLessThan
	                + ", testLessThanOrEqual=" + testLessThanOrEqual
	                + ", testGreaterThan=" + testGreaterThan
	                + ", testGreaterThanOrEqual=" + testGreaterThanOrEqual
	                + ", testAnd=" + testAnd + ", testOr=" + testOr + ", testNot="
	                + testNot + ", testAdd=" + testAdd + ", testAddString="
	                + testAddString + ", testSubtraction=" + testSubtraction
	                + ", testMultiplication=" + testMultiplication
	                + ", testDivision=" + testDivision + ", testModulus="
	                + testModulus + ", testExponentialPower="
	                + testExponentialPower + "]";
	    }

		public void setTestEqual(boolean testEqual) {
			this.testEqual = testEqual;
		}

		public void setTestNotEqual(boolean testNotEqual) {
			this.testNotEqual = testNotEqual;
		}

		public void setTestLessThan(boolean testLessThan) {
			this.testLessThan = testLessThan;
		}

		public void setTestLessThanOrEqual(boolean testLessThanOrEqual) {
			this.testLessThanOrEqual = testLessThanOrEqual;
		}

		public void setTestGreaterThan(boolean testGreaterThan) {
			this.testGreaterThan = testGreaterThan;
		}

		public void setTestGreaterThanOrEqual(boolean testGreaterThanOrEqual) {
			this.testGreaterThanOrEqual = testGreaterThanOrEqual;
		}

		public void setTestAnd(boolean testAnd) {
			this.testAnd = testAnd;
		}

		public void setTestOr(boolean testOr) {
			this.testOr = testOr;
		}

		public void setTestNot(boolean testNot) {
			this.testNot = testNot;
		}

		public void setTestAdd(double testAdd) {
			this.testAdd = testAdd;
		}

		public void setTestAddString(String testAddString) {
			this.testAddString = testAddString;
		}

		public void setTestSubtraction(double testSubtraction) {
			this.testSubtraction = testSubtraction;
		}

		public void setTestMultiplication(double testMultiplication) {
			this.testMultiplication = testMultiplication;
		}

		public void setTestDivision(double testDivision) {
			this.testDivision = testDivision;
		}

		public void setTestModulus(double testModulus) {
			this.testModulus = testModulus;
		}

		public void setTestExponentialPower(double testExponentialPower) {
			this.testExponentialPower = testExponentialPower;
		}
}
	

二、配置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="numberBean" class="com.yiidian.domain.Number"></bean>

	<bean id="customer" class="com.yiidian.domain.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>

三、编写测试类

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

}

四、运行结果

Customer [testEqual=true, testNotEqual=false, testLessThan=false, testLessThanOrEqual=true, testGreaterThan=false, testGreaterThanOrEqual=true, testAnd=false, testOr=true, testNot=true, testAdd=2.0, testAddString=1@1, testSubtraction=0.0, testMultiplication=1.0, testDivision=5.0, testModulus=0.0, testExponentialPower=4.0]

 

源码下载:http://pan.baidu.com/s/1pLE8a2R

热门文章

优秀文章