14

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

我尝试了以下方法:

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

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

有没有办法逃避这些价值观?

4

3 回答 3

21

文档中:

“要将单引号本身放在字符串中,请使用两个单引号字符。”

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

于 2011-06-01T19:38:39.127 回答
0

我修改为:

.... 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>
于 2010-11-29T21:53:27.397 回答
0

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

于 2015-06-19T14:16:21.490 回答