Question

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?

Was it helpful?

Solution

From the documentation:

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

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top