如何在JSP表达式语言中进行HTML编码? [英] How to HTML-encode in the JSP expression language?

查看:125
本文介绍了如何在JSP表达式语言中进行HTML编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下JSP:

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

$ {flashVars} 的值包含&符号需要在输出之前进行编码。相反,JSP期望 $ {flashVars} 的值是一段HTML并逐字输出&符号,导致HTML格式不正确。

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}"/>" />

但这看起来很丑陋并且让我的IDE混乱。有没有更好的方法来获得相同的结果?

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

推荐答案

使用 fn:escapeXml()

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<param name="FlashVars" value="${fn:escapeXml(flashVars)}" />

这篇关于如何在JSP表达式语言中进行HTML编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆