spring expression使用总结

Sample sample = new Sample();

ExpressionParser parser = new SpelExpressionParser();

EvaluationContext context = new StandardEvaluationContext(sample);
Expression exp;

//判断对象或字符串是否为NULL
exp = parser.parseExpression("id!=null");
boolean result = exp.getValue(sample, Boolean.class);
boolean result = exp.getValue(context, Boolean.class);
//判断字符串不为空(去除空格) ?的作用是为了防止空指针异常
exp = parser.parseExpression("name?.trim()?.length() > 0");
boolean result = exp.getValue(sample, Boolean.class);
boolean result = exp.getValue(context, Boolean.class);
//使用正则表达式
exp = parser.parseExpression("mail != null && (mail matches '^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$')");
boolean result = exp.getValue(sample, Boolean.class);
boolean result = exp.getValue(context, Boolean.class);
//判断集合不为空
exp = parser.parseExpression("list?.size()>0");
boolean result = exp.getValue(sample, Boolean.class);
boolean result = exp.getValue(context, Boolean.class);
//查找集合中ID大于1的数据,无数据赋值null ?:三目运算符简写
exp = parser.parseExpression("list?.?[id>1]?:null");
List<Sample> result = exp.getValue(sample,List.class);
List<Sample> result = exp.getValue(context,List.class);

#root代表根节点通常是可以省略。#root.name==>name


posted @ 2020-08-26 11:21  五虎啸天  阅读(2557)  评论(0编辑  收藏  举报