Spring EL表達(dá)式詳細(xì)講解及使用實例
Spring EL表達(dá)式詳細(xì)講解及使用實例
轉(zhuǎn)自:
http://www.manongjc.com/article/8467.html
簡介
用法
1、文本表達(dá)式
Double.parseDouble()進(jìn)行表達(dá)式類型轉(zhuǎn)換。parser.parseExpression("'hello'").getValue(String.class); // hello , 注意單引號parser.parseExpression("1.024E+3").getValue(Long.class); // 1024 , 指數(shù)形式parser.parseExpression("0xFFFF").getValue(Integer.class); // 65535 , 十六進(jìn)制parser.parseExpression("true").getValue(Boolean.class); // trueparser.parseExpression("null").getValue();
2、變量
// 定義變量String name = "Tom";EvaluationContext context = new StandardEvaluationContext(); // 表達(dá)式的上下文,context.setVariable("myName", name); // 為了讓表達(dá)式可以訪問該對象, 先把對象放到上下文中ExpressionParser parser = new SpelExpressionParser();// 訪問變量parser.parseExpression("#myName").getValue(context, String.class); // Tom , 使用變量// 直接使用構(gòu)造方法創(chuàng)建對象parser.parseExpression("new String('aaa')").getValue(String.class); // aaa
3、屬性和方法調(diào)用
?屬性可直接使用屬性名,屬性名首字母大小寫均可(只有首字母可不區(qū)分大小寫);?數(shù)組、列表可直接通過下表形式( list[index] )訪問;?map 可以直接把 key 當(dāng)成索引來訪問( map[key] );?方法可以直接訪問;
// 準(zhǔn)備工作Person person = new Person("Tom", 18); // 一個普通的POJOList<String> list = Lists.newArrayList("a", "b");Map<String, String> map = Maps.newHashMap();map.put("A", "1");map.put("B", "2");EvaluationContext context = new StandardEvaluationContext(); // 表達(dá)式的上下文,context.setVariable("person", person); // 為了讓表達(dá)式可以訪問該對象, 先把對象放到上下文中context.setVariable("map", map);context.setVariable("list", list);ExpressionParser parser = new SpelExpressionParser();// 屬性parser.parseExpression("#person.name").getValue(context, String.class); // Tom , 屬性訪問parser.parseExpression("#person.Name").getValue(context, String.class); // Tom , 屬性訪問, 但是首字母大寫了// 列表parser.parseExpression("#list[0]").getValue(context, String.class) // a , 下標(biāo)// mapparser.parseExpression("#map[A]").getValue(context, String.class); // 1 , key// 方法parser.parseExpression("#person.getAge()").getValue(context, Integer.class); // 18 , 方法訪問
4、類型
T 操作符可以獲取類型, 可以調(diào)用對象的靜態(tài)方法.// 獲取類型parser.parseExpression("T(java.util.Date)").getValue(Class.class); // class java.util.Date// 訪問靜態(tài)成員(方法或?qū)傩?parser.parseExpression("T(Math).abs(-1)").getValue(Integer.class); // 1// 判斷類型parser.parseExpression("'asdf' instanceof T(String)").getValue(Boolean.class); // true;
5、操作符
?關(guān)系操作符, 包括: eq(==), ne(!=), lt()<, le(<=), gt(>), ge(>=)?邏輯運算符, 包括: and(&&), or(||), not(!)?數(shù)學(xué)操作符, 包括: 加(+), 減(-), 乘(*), 除(/), 取模(%), 冪指數(shù)(^)?其他操作符, 如: 三元操作符, instanceof, 賦值(=), 正則匹配
parseExpression("#name?:'defaultName'"), 如果變量name為空時設(shè)置默認(rèn)值.parser.parseExpression("1 > -1").getValue(Boolean.class); // trueparser.parseExpression("1 gt -1").getValue(Boolean.class); // trueparser.parseExpression("true or true").getValue(Boolean.class); // trueparser.parseExpression("true || true").getValue(Boolean.class); // trueparser.parseExpression("2 ^ 3").getValue(Integer.class); // 8parser.parseExpression("true ? true : false").getValue(Boolean.class); // trueparser.parseExpression("#name ?: 'default'").getValue(context, String.class); // defaultparser.parseExpression("1 instanceof T(Integer)").getValue(Boolean.class); // trueparser.parseExpression("'5.00' matches '^-?\d+(\.\d{2})?$'").getValue(Boolean.class); // trueparser.parseExpression("#person.name").getValue(context, String.class); // Tom , 原來的值parser.parseExpression("#person.name = 'Jim'").getValue(context, String.class); // Jim , 賦值之后parser.parseExpression("#person.name").getValue(context, String.class); // Jim, 賦值起了作用
6、避免空指針
null, 就會出現(xiàn)空指針異常. 安全導(dǎo)航會判斷對象是否為null, 如果是的話, 就返回null而不是拋出空指針異常. 使用方式就是在對象后面加個?. 如下:// 使用這種表達(dá)式可以避免拋出空指針異常parser.parseExpression("#name?.toUpperCase()").getValue(context, String.class); // null
7、#this變量
#this來表示當(dāng)前的對象. 常用于集合的過濾// this 使用示例parser.parseExpression("{1, 3, 5, 7}.?[#this > 3]").getValue(); // [5, 7]
8、集合選擇
??[expression]: 選擇符合條件的元素?^[expression]: 選擇符合條件的第一個元素?$[expression]: 選擇符合條件的最后一個元素?![expression]: 可對集合中的元素挨個進(jìn)行處理
#this變量進(jìn)行過濾, 對于map, 可分別對keySet及valueSet分別使用key和value關(guān)鍵字;// 集合parser.parseExpression("{1, 3, 5, 7}.?[#this > 3]").getValue(); // [5, 7] , 選擇元素parser.parseExpression("{1, 3, 5, 7}.^[#this > 3]").getValue(); // 5 , 第一個parser.parseExpression("{1, 3, 5, 7}.$[#this > 3]").getValue(); // 7 , 最后一個parser.parseExpression("{1, 3, 5, 7}.![#this + 1]").getValue(); // [2, 4, 6, 8] ,每個元素都加1// mapMap<Integer, String> map = Maps.newHashMap();map.put(1, "A");map.put(2, "B");map.put(3, "C");map.put(4, "D");EvaluationContext context = new StandardEvaluationContext();context.setVariable("map", map);parser.parseExpression("#map.?[key > 3]").getValue(context); // {4=D}parser.parseExpression("#map.?[value == 'A']").getValue(context); // {1=A}parser.parseExpression("#map.?[key > 2 and key < 4]").getValue(context); // {3=C}
9、模板表達(dá)式
#{}作為一個定界符:parser.parseExpression("他的名字為#{#person.name}", new TemplateParserContext()).getValue(context); // 他的名字為Tom10、綜合使用
//模板表達(dá)式,三元表達(dá)式結(jié)合使用parser.parseExpression("#{#person.name!=null?'他的名字叫'+#person.name+'先生':'不知道名字'}", new TemplateParserContext()).getValue(context);
歡迎關(guān)注我的公眾號“須彌零一”,更多技術(shù)文章第一時間推送。
評論
圖片
表情
