How do I escape values in SpEL?

会有一股神秘感。 提交于 2019-12-19 14:57:03

问题


I am writing some SpEL statements in XML, and I can't get the parser to determine when I need to escape a character.

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?


回答1:


From the documentation:

"To put a single quote itself in a string use two single quote characters."

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




回答2:


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.

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:

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



回答3:


FWIW, SpEL has support for double-quotes as of version 3.2. SPR-9620



来源:https://stackoverflow.com/questions/4308247/how-do-i-escape-values-in-spel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!