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



免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM