java 如何在 SpEL 中转义值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4308247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I escape values in SpEL?
提问by Scott
I am writing some SpEL statements in XML, and I can't get the parser to determine when I need to escape a character.
我正在用 XML 编写一些 SpEL 语句,但我无法让解析器确定何时需要对字符进行转义。
I've attempted the following:
我尝试了以下方法:
<... property="someProperty" value="#{ someBean.aMethodOnTheBean('st\'ring') }" />
However adding in the \' does not seem to escape that single quote and I keep receiving a parser exception.
然而,添加 \' 似乎并没有逃脱那个单引号,我不断收到解析器异常。
Is there any way to escape these values?
有什么办法可以逃避这些值吗?
回答by Damon
From the documentation:
从文档:
"To put a single quote itself in a string use two single quote characters."
“要将单引号本身放在字符串中,请使用两个单引号字符。”
expression = 'something = ''' + someMethod.getValue + ''''
expression = 'something = ''' + someMethod.getValue + ''''
回答by Scott
I modified to:
我修改为:
.... value="#{ someBean.aMethodOnTheBean("st'ring") }"
This works, I remembered incorrectly that I had issues when using a double quote to input a string value into a SpEL function prior.
这是有效的,我错误地记得我在使用双引号将字符串值输入到 SpEL 函数之前遇到了问题。
Below is the schema definition:
以下是架构定义:
<?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">
The XML validates properly in eclipse. However my simplified example was not valid -- my apologies and good catch. I am actually setting the value like this:
XML 在 Eclipse 中正确验证。然而,我的简化示例无效——我很抱歉,并且收获颇丰。我实际上是这样设置值的:
<bean id="someBean" class="someClass">
<property name="someList">
<list>
<value>"#{ anotherBean.methodOnBean("some'String") }"</value>
</list>
</property>
</bean>