c:out 嵌套在元素属性内
在元素属性内嵌套 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
防止 HTML 元素属性中的 XSS 攻击而不通过嵌套
标记干扰格式良好的 XML 语法的常见做法是使用fn:escapeXml()
函数改为: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 usingfn:escapeXml()
function instead:我通常尽可能地使用
${}
。它简单且更具可读性。当我需要额外的功能(例如escapeXml
函数)时,我会使用
。在您的示例中,您实际上可以摆脱 no
:编辑:XSS 问题
我的答案没有解决 BalusC 和 StuartWakefield 提到的 XSS 漏洞。尽管我的答案简单地说是正确的,但您确实应该始终减少 XSS 漏洞。我更喜欢使用 OWASP taglib。
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 theescapeXml
function.In your example, you could actually get away with no
<c:out>
: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.