Spring3开发实战 之 第八章:Spring3的表达式语言(五)

2014-11-24 08:41:37 · 作者: · 浏览: 4
eExpression(expression4).getValue(int[][][].class);
访问元素
SpEL目前支持所有集合类型和字典类型的元素访问,使用“集合[索引]”访问集合元素,使用“map[key]”访问字典元素 。示例如下;
1://SpEL内联List访问
int result1 = parser.parseExpression("{1,2,3}[0]").getValue(int.class);
//相当于result1.get(0)

2://SpEL目前支持所有集合类型的访问
Collection collection = new HashSet();
collection.add(1);
collection.add(2);
eva luationContext context2 = new Standardeva luationContext();
context2.setVariable("collection", collection);
int result2 = parser.parseExpression("#collection[1]").getValue(context2, int.class);
3://SpEL对Map字典元素访问的支持
Map map = new HashMap();
map.put("a", 1);
eva luationContext context3 = new Standardeva luationContext();
context3.setVariable("map", map);
int result3 = parser.parseExpression("#map['a']").getValue(context3, int.class);

元素修改
可以使用赋值表达式或Expression接口的setValue方法修改。示例如下:
1://修改数组元素值
int[] array = new int[] {1, 2};
eva luationContext context1 = new Standardeva luationContext();
context1.setVariable("array", array);
int result1 = parser.parseExpression("#array[1] = 3").getValue(context1, int.class);
2://修改集合值
Collection collection = new ArrayList();
collection.add(1);
collection.add(2);
eva luationContext context2 = new Standardeva luationContext();
context2.setVariable("collection", collection);
int result2 = parser.parseExpression("#collection[1] = 3").getValue(context2, int.class);
parser.parseExpression("#collection[1]").setValue(context2, 4);
result2 = parser.parseExpression("#collection[1]").getValue(context2, int.class);
3://修改map元素值
Map map = new HashMap();
map.put("a", 1);
eva luationContext context3 = new Standardeva luationContext();
context3.setVariable("map", map);
int result3 = parser.parseExpression("#map['a'] = 2").getValue(context3, int.class);
集合投影
在SQL中投影指从表中选择出列,而在SpEL指从集合中的元素,通过选择来构造新的集合,该集合和原集合具有相同数量的元素,但可能属性不一样;SpEL使用“(list|map).![投影表达式]”来进行投影运算。
1:示例集合的投影
//1.首先准备测试数据
Collection collection = new ArrayList();
UserModel um1 = new UserModel();
um1.setUuid("u1");
um1.setName("u1Name");
collection.add(um1);
UserModel um2 = new UserModel();
um2.setUuid("u2");
um2.setName("u2Name");
collection.add(um2);
//2.测试集合或数组
eva luationContext context1 = new Standardeva luationContext();
ExpressionParser parser = new SpelExpressionParser();
context1.setVariable("collection", collection);
Collection result1 =
parser.parseExpression(" #collection.![#this.name]").getValue(context1, Collection.class);
SpEL投影运算还支持Map投影,但Map投影最终只能得到List结果,对于投影表达式中的“#this”将是Map.Entry,所以可以使用“value”来获取值,使用“key”来获取键。 示例如下:
//1.首先准备测试数据

java代码:
查看复制到剪贴板打印
UserModel um1 = new UserModel();
um1.setUuid("u1");
um1.setName("u1Name");
UserModel um2 = new UserModel();
um2.setUuid("u2");
um2.setName("u2Name");
Map map = new HashMap();
map.put(um1.getUuid(),um1);
map.put(um2.getUuid(),um2);
//2.测试Map投影
eva luationContext context1 = new Standardeva luationContext();
ExpressionParser parser = new SpelExpressionParser();
context1.setVariable("map", map);
Collection result1 =
parser.parseExpression("#map.![#this.value.name]").getValue(context1, Collection.class);

集合筛选
在SpEL指根据原集合通过条件表达式选择出满足条件的元素并构造为新的集合,SpEL使用“(list|map). [选择表达式]”,其中选择表达式结果必须是boolean类型,如果true则选择的元素将添加到新集合中,false将不添加到新集合中。示例如下:
//1:准备测试数据的过程跟上一个示例一样,就不重复了
//2.测试集合或数组的筛选

java代码:
查看