繁体   English   中英

如何在春季Spel Evaluator中逃避&字符

[英]How to escape a & character in spring Spel Evaluator

我有一个包含单个&的表达式,并且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());
        }
    }

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

您需要在字符串中使用两个单引号将其转义,如下所示:

'Manufacturer''s Name & Address:'

有关在表达式中包含文字的更多信息,请参考SpEL语言参考文档的“ 文字表达式 ”部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM