2010-11-29 26 views
7

我正在用XML寫一些SpEL語句,並且我無法讓解析器確定何時需要轉義字符。如何在SpEL中轉義值?

我已經嘗試以下操作:

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

在\但是加入」似乎並沒有逃脫單引號,我不斷收到一個解析器異常。

有什麼辦法可以逃避這些價值嗎?

+1

#{someBean.aMethodOnTheBean(T(org.apache.commons.lang.StringEscapeUtils).escapeJavaScript( 「ad'sf」))}工作,但不是很乾淨 – Scott 2010-11-29 21:29:26

+0

不錯的嘗試!順便說一句,該方法在[表達模板部分](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html#expressions-templating)上的例子中有記錄。 – falsarella 2015-05-13 20:33:20

回答

15

documentation

「放單引號本身在 字符串中使用兩個單引號字符 。」

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

0

我修改爲:

.... value="#{ someBean.aMethodOnTheBean("st'ring") }" 

這工作,我記得正確,我使用時,輸入雙引號的字符串值轉換成規劃環境地政司函數之前有問題。

下面是模式定義:

<?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正確地驗證月食。然而,我的簡單例子是無效的 - 我的道歉和良好的捕獲。我其實設置這樣的值:

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

FWIW,規劃環境地政司對雙引號作爲3.2版本的支持。 SPR-9620

相關問題