【发布时间】:2017-05-14 18:48:25
【问题描述】:
我想使用 SpEL 来评估一些谓词。由于值/属性是动态的,因此我没有特定的 bean 类。因此,我们有一个 hashmap,其中(根据应用程序状态)键映射到不同的 POJO/bean,例如:
Person person = new Person();// a person instance
person.setAge(25) // e.g. property age
Map<String, Object> model = new HashMap<>();
model.put("person", person);
// and others ...
我们希望通过设置变量来为此使用评估上下文,例如:
String predicate = "person.age>21";
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariables(model);
Expression expression = expressionParser.parseExpression(predicate);
boolean result = expression.getValue(context, Boolean.class);
但这会引发异常:
SpelEvaluationException: EL1007E:(pos 0): 在 null 上找不到属性或字段“person”
有人给点建议吗?
【问题讨论】: