Java JSP : JSTL 的 <c:out> 标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/291031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:38:48  来源:igfitidea点击:

JSP : JSTL's <c:out> tag

javajspjstltags

提问by Steve Kuo

Writing a JSP page, what exactly does the <c:out>do? I've noticed that the following both has the same result:

写一个JSP页面,究竟是<c:out>做什么的?我注意到以下两者的结果相同:

<p>The person's name is <c:out value="${person.name}" /></p>
<p>The person's name is ${person.name}</p>

采纳答案by krosenvold

c:outescapes HTML characters so that you can avoid cross-site scripting.

c:out转义 HTML 字符,以便您可以避免跨站点脚本。

if person.name = <script>alert("Yo")</script>

如果 person.name = <script>alert("Yo")</script>

the script will be executed in the second case, but not when using c:out

脚本将在第二种情况下执行,但在使用时不会执行 c:out

回答by Will Wagner

Older versions of JSP did not support the second syntax.

旧版本的 JSP 不支持第二种语法。

回答by Chris Serra

c:outalso has an attribute for assigning a default value if the value of person.namehappens to be null.

c:out如果 的值person.name恰好为空,则还有一个用于分配默认值的属性。

Source: out (TLDDoc Generated Documentation)

来源:out(TLDDoc 生成的文档)

回答by alexmeia

As said Will Wagner, in old version of jsp you should always use c:outto output dynamic text.

正如 Will Wagner 所说,在旧版本的 jsp 中,您应该始终使用c:out输出动态文本。

Moreover, using this syntax:

此外,使用此语法:

<c:out value="${person.name}">No name</c:out>

you can display the text "No name" when name is null.

当名称为空时,您可以显示文本“无名称”。

回答by Greenhorn

You can explicitly enable escaping of Xml entities by using an attribute escapeXml value equals to true. FYI, it's by default "true".

您可以使用等于 true 的属性 escapeXml 值显式启用 Xml 实体的转义。仅供参考,默认情况下为“true”。