20

Consider the following piece of JSP:

<param name="FlashVars" value="${flashVars}" />

The value of ${flashVars} contains ampersands and needs to be encoded before it is output. Instead, JSP expects the value of ${flashVars} to be a piece of HTML and outputs the ampersands verbatim, resulting in bad HTML.

I found out that I can get the value to be encoded if I write it like this:

<param name="FlashVars" value="<c:out value="${flashVars}"/>" />

But this looks really ugly and confuses my IDE to boot. Is there a better way to get the same result?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Martijn
  • 6,713
  • 3
  • 31
  • 38
  • 2
    If IDE is the only problem with the above, then get another IDE - Intellij IDEA is great. – Chii Dec 24 '10 at 13:19
  • My main problem is it looks ugly. :-) I don't want to nest a tag in an attribute of another tag. – Martijn Dec 24 '10 at 13:29
  • I don't blame Eclipse; it's a very complicated, non-compositional templating language which makes it very difficult to do proper highlighting/error reporting without writing a full compiler for it (which is not Eclipse's job here IMHO). – Martijn Dec 24 '10 at 15:06

1 Answers1

33

Use fn:escapeXml().

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<param name="FlashVars" value="${fn:escapeXml(flashVars)}" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555