项目中需要判断传来的数值是否在某个区间内,例如:判断1是否在(0,99)内,判断表达式通过字符串的截取与拼接为:“1>0&&1<99”。判断该式子是否成立。
import org.springframework.expression.spel.standard.SpelExpressionParser; //代码示例 String expression = "1>0&&1<99"; String expression2 = "1>0&&1>99"; SpelExpressionParser spelExpressionParser = new SpelExpressionParser(); Boolean value = spelExpressionParser.parseExpression(expression).getValue(Boolean.class); Boolean value1 = spelExpressionParser.parseExpression(expression2).getValue(Boolean.class); System.out.println(value); System.out.println(value1);
输出结果:
true false



