【问题标题】:How do I escape values in SpEL?如何在 SpEL 中转义值?
【发布时间】:2015-06-19 22:16:21
【问题描述】:

我正在用 XML 编写一些 SpEL 语句,但无法让解析器确定何时需要转义字符。

我尝试了以下方法:

<... property="someProperty" value="#{ someBean.aMethodOnTheBean('st\'ring') }" />

但是,在 \' 中添加似乎并没有转义该单引号,并且我一直收到解析器异常。

有没有办法逃避这些值?

【问题讨论】:

  • #{someBean.aMethodOnTheBean(T(org.apache.commons.lang.StringEscapeUtils).escapeJavaScript("ad'sf")) } 有效,但不是很干净
  • 不错的尝试!顺便说一句,这种方法记录在 expression templating section 的示例中。
  • 另外,这可能是谁在使用spring property placeholder 的一个选项(虽然我不知道在这种情况下如何正确使用它,所以我最终转义了@ 987654324@文件直接)。

标签: java spring spring-el


【解决方案1】:

来自documentation

"将单引号本身放在一个 字符串使用两个单引号 字符。”

expression = 'something = ''' + someMethod.getValue + ''''

【讨论】:

    【解决方案2】:

    我修改为:

    .... value="#{ someBean.aMethodOnTheBean("st'ring") }" 
    

    这行得通,我记错了,我之前在使用双引号将字符串值输入到 SpEL 函数时遇到了问题。

    以下是架构定义:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:util="http://www.springframework.org/schema/util"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
           default-lazy-init="true"
           default-init-method="initialize">
    

    XML 在 Eclipse 中正确验证。但是,我的简化示例无效-我很抱歉并且很好。我实际上是这样设置值的:

    <bean id="someBean" class="someClass">
        <property name="someList">
            <list>
                <value>"#{ anotherBean.methodOnBean("some'String") }"</value>
            </list>
        </property>
    </bean>
    

    【讨论】:

    • SO 上的内容是否以某种方式损坏了,还是我误会了您?我很确定:&lt;something value="#{"a"}" /&gt; 不是有效的 XML。
    【解决方案3】:

    FWIW,SpEL 从 3.2 版开始支持双引号。 SPR-9620

    【讨论】:

      猜你喜欢
      • 2018-08-29
      • 2018-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-27
      相关资源
      最近更新 更多