2009-06-15 30 views
5

我有這樣的JSP代碼片段:如何使用JSTL標籤逃避EL中的字符?

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 

<c:choose> 
    <c:when test="${var1.properties[\"Item Type\"] eq \"Animal's Part\"}"> 
    <c:set var="cssClassName" value="animalpart" /> 
    </c:when> 
    <c:otherwise> 
    <c:set var="cssClassName" value="" /> 
    </c:otherwise> 
</c:choose> 

的JSP不能由服務器進行編譯。但是,如果我從「動物的部分」中刪除了字符「'」,它是可編輯的。我試圖通過使用「\」字符來逃避它,但它仍然給我錯誤。

任何建議/幫助表示讚賞。如果可能,我儘量避免使用scriptlet。

謝謝。

編輯:我設法得到它的工作(發佈到的StackOverflow)後,張貼在這個問題上的解決方案之一。我嘗試過之前發佈的其他解決方案(文森特和埃迪),但不幸的是,沒有人在我的環境中工作,但我認爲他們可能在答案環境中工作。謝謝。

+0

在哪個環境中Eddie's/Vincent的解決方案無效? – hop 2012-01-30 02:53:28

回答

3

這是在工程解決方案我的使用案例:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 

<c:set var="itemType"  value="${var1.properties[\"Item Type\"]}" /> 
<c:set var="item_animalpart" value="Animal's Part" /> 
<c:set var="item_treepart" value="Tree's Part" /> 

<c:choose> 
    <c:when test="${itemType eq name_item_animalpart}"> 
    <c:set var="cssClassName" value="animalpart" /> 
    </c:when> 
    <c:when test="${itemType eq name_item_treepart}"> 
    <c:set var="cssClassName" value="treepart" /> 
    </c:when> 
    <c:otherwise> 
    <c:set var="cssClassName" value="" /> 
    </c:otherwise> 
</c:choose> 
+0

** item_animalpart **和** item_treepart **是冗餘定義 – gavenkoa 2012-11-26 12:48:05

6

試試這個

<c:when test='${var1.properties["Item Type"] eq "Animal\'s Part"}'> 
0

使用將escapeXml = 「假」 例如:

<c:out value="${formulario}" escapeXml="false" /> 
3

你有兩個簡單的選擇:

<c:when test="${var1.properties['Item Type'] eq 'Animal\'s Part'}"> 

<c:when test='${var1.properties["Item Type"] eq "Animal\'s Part"}'>