c:out 嵌套在元素属性内

发布于 2024-12-10 02:54:57 字数 447 浏览 0 评论 0原文

在元素属性内嵌套 ac:out JSTL 标记是一种好的做法还是通常首选使用 c:out 的 var 属性?它似乎可以以任何一种方式工作,但我怀疑嵌套它可能无法在某些应用程序服务器或 JSP 版本中工作(而且它看起来错误)。

例如,一个输入元素在验证失败时恢复其值,并使用特殊字符转义:

<input type="text" name="firstname" value="<c:out value="${param.firstname}"/>"/>

与:

<c:out value="${param.firstname}" var="firstname"/>
<input type="text" name="firstname" value="${firstname}"/>

Is nesting a c:out JSTL tag inside an element attribute a good practice or is using the var attribute of c:out generally preferred? It seems to work either way, but I suspect nesting it might not work in some application servers or versions of JSP (and it just looks wrong).

For example, an input element which has its value restored on validation failure, and with special character escaping:

<input type="text" name="firstname" value="<c:out value="${param.firstname}"/>"/>

versus:

<c:out value="${param.firstname}" var="firstname"/>
<input type="text" name="firstname" value="${firstname}"/>

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

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

发布评论

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

评论(2

携余温的黄昏 2024-12-17 02:54:57

防止 HTML 元素属性中的 XSS 攻击而不通过嵌套 标记干扰格式良好的 XML 语法的常见做法是使用 fn:escapeXml() 函数改为:

<input type="text" name="firstname" value="${fn:escapeXml(param.firstname)}"/>

The common practice to prevent XSS attacks in HTML element attributes without disturbing the well formed XML syntax by a nested <c:out> tag is using fn:escapeXml() function instead:

<input type="text" name="firstname" value="${fn:escapeXml(param.firstname)}"/>
唱一曲作罢 2024-12-17 02:54:57

我通常尽可能地使用 ${} 。它简单且更具可读性。当我需要额外的功能(例如 escapeXml 函数)时,我会使用

在您的示例中,您实际上可以摆脱 no :

<input type="text" name="firstname" value="${param.firstname}"/>

编辑:XSS 问题

我的答案没有解决 BalusC 和 StuartWakefield 提到的 XSS 漏洞。尽管我的答案简单地说是正确的,但您确实应该始终减少 XSS 漏洞。我更喜欢使用 OWASP taglib

<span>${esc:forHtml(sketchyText)}</span>
<span><esc:forHtml(sketchyText)/></span>
<input value="${esc:forHtmlAttribute(sketchyText)}"/>

I usually use the ${} everywhere that I can. It's simple and more readable. I use <c:out> when I need the extra functionality, such as the escapeXml function.

In your example, you could actually get away with no <c:out>:

<input type="text" name="firstname" value="${param.firstname}"/>

Edit: XSS issues

My answer does not address the XSS holes that BalusC and StuartWakefield mention. Although my answer is simplistically correct, you really should always mitigate XSS holes. I prefer to use the OWASP taglib.

<span>${esc:forHtml(sketchyText)}</span>
<span><esc:forHtml(sketchyText)/></span>
<input value="${esc:forHtmlAttribute(sketchyText)}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文