Spring 他 - "There is still more data in the expression"

Spring EL - "There is still more data in the expression"

是否可以解析如下所示的表达式,我在其中调用一个方法并希望在该方法调用的结果之后有文本?

String expression = "obj.someMethod()'test'";
ExpressionParser parser = new SpelExpressionParser(); 
Expression expression = parser.parseExpression(expression);

当我 运行 编写如下代码时,出现以下错误:

org.springframework.expression.spel.SpelParseException: EL1041E:(pos 23): After parsing a valid expression, there is still more data in the expression: 'test'

如果我删除 'test' 字符串,那么它会正确解析和计算。

即使是表达式语言,它也是基于核心编程语言并遵循其规则的。

因此,如果您使用 + 运算符将方法结果与 Java 中的字符串连接起来,您应该在 SpEL 中执行相同的操作:

"obj.someMethod() + 'test'"

是您的正确答案。

以及您可以使用:

"obj.someMethod().concat('test')"

if someMethod() returns String 当然可以。