How do I escape values in SpEL?

断了今生、忘了曾经 提交于 2019-12-01 15:32:36
Damon

From the documentation:

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

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

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>

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

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