<...">

How to avoid character in EL with JSTL tag?

I have this piece of JSP code:

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

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

JSP could not be compiled by the server. However, if I remove the β€œβ€ symbol from the β€œAnimal Part”, it will compile. I tried to escape from it with the "\" character, but it still gives me an error.

Any suggestion / help is appreciated. I tried to avoid using a script if possible.

Thanks.

EDIT : I managed to get it working (after posting on StackOverflow), posted as one of the solutions in this question. I tried another solution published before (Vincent and Eddie), however, unfortunately, it does not work in my environment, although I believe that they can work in the response environment. Thanks.

+5
4

, :

<%@ 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 Part" />
<c:set var="item_treepart"   value="Tree 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>
+3

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

You have two simple options:

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

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

Use escapeXml = "false" For example:

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

All Articles