如何在 JSTL 函数/EL 中转义双引号?

发布于 2024-11-30 07:15:29 字数 1664 浏览 0 评论 0 原文

我需要使用 更改为 \" fn/replace.fn.html" rel="noreferrer">JSTL 替换 函数在输入标记中使用字符串,例如:

<input type="hidden" name="text" size="40" value="${text}">

如果 ${text} 具有 "< /code>,HTML 将会被破坏

。尝试

<input type="hidden" name="text" size="40" value="${fn:replace(text, "\"", "\\\""}">

<input type="hidden" name="text" size="40" value="${fn:replace(text, '"', '\"'}">

但没有成功,页面出现类似错误。

org.apache.el.parser.ParseException:在行遇到“”}“”}“” 1,第 32 栏。期待以下之一: “。” ... “)”... “[”... “、”…… “>” ... “gt”... “<” ... “它”... “>=”... “格”... “<=”... “勒”... “==”... “当量”... “!=”... “讷”... “&&” ... “和” ... “||” ... “或者” ... “*”... “+”... “——”…… “/”... “div”... “%”... “模组”...

我该怎么做?

更新

我错过了替换函数的右括号。正确的一个是这个带有闭括号的:

<input type="hidden" name="text" size="40" value="${fn:replace(text, '"', '\"')}">

Update2

我发现在发布文本时,使用 \ 并不是一个好主意,因为这个原因 为什么不能在 HTML 输入标记中使用 \" ? 代码应该是这样的。 :

<input type="hidden" name="text" size="40" value="${fn:replace(text, '"', '&quot;')}">

I need to change " to \" with JSTL replace function to use the string in input tag like:

<input type="hidden" name="text" size="40" value="${text}">

If the ${text} has the ", the HTML will be broken.

So I tried

<input type="hidden" name="text" size="40" value="${fn:replace(text, "\"", "\\\""}">

and

<input type="hidden" name="text" size="40" value="${fn:replace(text, '"', '\"'}">

but didn't worked. The page makes errors like

org.apache.el.parser.ParseException: Encountered " "}" "} "" at line
1, column 32. Was expecting one of:
"." ...
")" ...
"[" ...
"," ...
">" ...
"gt" ...
"<" ...
"lt" ...
">=" ...
"ge" ...
"<=" ...
"le" ...
"==" ...
"eq" ...
"!=" ...
"ne" ...
"&&" ...
"and" ...
"||" ...
"or" ...
"*" ...
"+" ...
"-" ...
"/" ...
"div" ...
"%" ...
"mod" ...

How can I do this?

Update

I missed a close paren of replace function. The right one was this one with a close paren:

<input type="hidden" name="text" size="40" value="${fn:replace(text, '"', '\"')}">

Update2

I found out that when posting texts, using \ is not a good idea because of this reason why can't use \" in HTML input tag?. The code should be like this:

<input type="hidden" name="text" size="40" value="${fn:replace(text, '"', '"')}">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青丝拂面 2024-12-07 07:15:29

它不起作用,因为 \ 是 Java 字符串中的转义字符。要按字面意思表示它,您需要再次使用另一个 \ 对其进行转义。另外 " 是 EL 中的特殊字符,您还需要对其进行转义以按字面意思表示。因此,正确的语法是:

<input type="hidden" name="text" size="40" value="${fn:replace(text, '\"', '\\\"')}">

但是,您实际上应该是 () 来防止 XSS。它不仅转义引号,还转义其他字符

<input type="hidden" name="text" size="40" value="${fn:escapeXml(text)}">

使用 fn: escapeXml

  • 。 href="https://stackoverflow.com/questions/2658922/xss-prevention-in-java">JSP/Servlet Web 应用程序中的 XSS 预防

It doesn't work because the \ is an escape character in Java string. To represent it literally, you need to escape it with another \ again. Also the " is a special character in EL, you also need to escape it to represent it literally. So, the proper syntax would have been:

<input type="hidden" name="text" size="40" value="${fn:replace(text, '\"', '\\\"')}">

But, you should actually be using fn:escapeXml() to prevent XSS. It not only escapes quotes, but also other characters.

<input type="hidden" name="text" size="40" value="${fn:escapeXml(text)}">

###See also:

醉生梦死 2024-12-07 07:15:29

你做错了(使用fn:replace)。

正确的方法是:

<input type="hidden" name="text" size="40" value="<c:out value='${text}'/>">
(actually tested code - works 100%)

编辑: 经过更多思考:

  • 使用 fn:escapeXml (由 BalusC 编写)的方式也可以工作,并且
  • 使用 < 看起来更好(没有嵌套标签) em>fn:replace 模仿 fn:escapeXml 是在自找麻烦。您将会忘记包含一些应该转义的字符。只需使用现有的、经过尝试和测试的fn:escapeXml(或c:out

You are doing it wrong (with fn:replace).

The correct way is:

<input type="hidden" name="text" size="40" value="<c:out value='${text}'/>">
(actually tested code - works 100%)

Edit: Upon more thinking:

  • the way by using fn:escapeXml (as written by BalusC) works too and looks nicer (no nested tags)
  • using fn:replace to mimick fn:escapeXml is asking for trouble. You will forget to include some character that should be escaped. Just use the existing, tried and tested fn:escapeXml (or c:out)
烂人 2024-12-07 07:15:29

您可能有一个错字:我在那里没有看到结束括号。试试这个:

${fn:replace(news.title, "\"", "\\\"")}

另外,您是否正在尝试输出结果,或者是否正在尝试更新 news.title 以便下次访问 news.title 时替换就位?这将输出结果,但不会替换实际值:此调用不会更改 news.title

You may have a typo: I don't see a closing paren in there. Try this:

${fn:replace(news.title, "\"", "\\\"")}

Also, are you trying to OUTPUT the results or are you trying to update news.title so the next time you access news.title the replacement is in place? This will work to output the result, but not to replace the actual value: news.title will not be changed by this call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文