如何逃避&春天Spel Evaluator中的角色

时间:2018-03-20 10:22:34

标签: java spring spring-el spelevaluationexception

我的表达式包含单个&和spring spel求值程序不断抛出错误missing expected character '&'

我尝试使用反斜杠和双反斜杠来逃避但是它会导致非法的转义字符错误。

这是我需要解析的表达式

(#PaymentType!=null&&!(#PaymentType).toString().isEmpty()?'PaymentType:'+((#PaymentType)+' ')+'$':'')+(#ProductSearchState!=null&&!(#ProductSearchState).toString().isEmpty()?'ProductSearchState:'+((#ProductSearchState)+' ')+'$':'')+(#Manufacturer_s39s_s32Name_s32_s38_s32Address!=null&&!(#Manufacturer_s39s_s32Name_s32_s38_s32Address).toString().isEmpty()?'Manufacturer's Name & Address:'+((#Manufacturer_s39s_s32Name_s32_s38_s32Address)+' '):'')

以下是我用来解析表达式的代码。

 public static void main (String args[]) {
   String expression="(#PaymentType!=null&&!(#PaymentType).toString().isEmpty()?'PaymentType:'+((#PaymentType)+' ')+'$':'')+(#ProductSearchState!=null&&!(#ProductSearchState).toString().isEmpty()?'ProductSearchState:'+((#ProductSearchState)+' ')+'$':'')+(#Manufacturer_s39s_s32Name_s32_s38_s32Address!=null&&!(#Manufacturer_s39s_s32Name_s32_s38_s32Address).toString().isEmpty()?'Manufacturer's Name & Address:'+((#Manufacturer_s39s_s32Name_s32_s38_s32Address)+' '):'')";
   String g = expression;
   System.out.println(g);
        final Set<String> dependent = new HashSet<>();
        try {
            ExpressionParser parser = new SpelExpressionParser();

//        log.info("Extracting dependent attributes for {} ", expression);

            new SplExpressionVisitor<Void>() {
                public void visit(SpelExpression expression) {
                    visitNode(expression.getAST());
                }

                @Override
                protected Void visit(VariableReference node) {
                    final String variableReferenceName = getVariableReferenceName(node);
                    dependent.add(variableReferenceName);
                    return super.visit(node);
                }

            }.visit((SpelExpression) parser.parseExpression(expression));

        }catch (Exception e){
            System.out.println(e.toString());
        }
    }

1 个答案:

答案 0 :(得分:1)

您的字符串'Manufacturer's Name & Address:'看起来像字符串'Manufacturer'后跟一些废话,因为您不能使用单引号来分隔字符串以及字符串内部。

你需要在字符串中使用两个单引号字符来逃避它,如下所示:

'Manufacturer''s Name & Address:'

请参阅&#34; Literal Expressions&#34;有关在表达式中包含文字的更多信息,请参阅SpEL语言参考文档部分。