为什么转义字符在HTML中显示?(why are the escape characters being displayed in HTML?)
我需要在显示网页上的内容之前转义一些文本,这实际上是正确完成的。 但是,当我在html中显示String时,仍将显示转义字符。 以下是这样一个例子:
hello there my ni&%ame is + - && !
以及带有转义的相应字符串如下:
hello there my ni&%ame is + - && !
我已经读过某个地方,taglib中的核心只会逃避基本的,例如>,
以下是用于将特定字符转换为其转义字符的代码的一部分:
while (character != CharacterIterator.DONE ){
if (character == '
result.append("<");
}
else if (character == '>') {
result.append(">");
}
else if (character == '&') {
result.append("&");
} .....
return result;
}
转义部分完成并完美运行..当我尝试将带有转义字符的字符串显示到html页面时出现问题
I need to escape some text before displaying the contents on the webpage, and this in fact is being done correctly. However when I display the String in html, the escape characters will still be displayed. The following is such an example:
hello there my ni&%ame is + - && !
and the respective string with escaping is the following:
hello there my ni&%ame is + - && !
I've read somewhere that the core in taglib will only escape the basic ones such as >, < , ", \t and space. however none of these escape sequences are removed from the html code. Does any of you know how to be able to solve this problem please? thanks
the following is part of the code used to convert a specific character to its escape character:
while (character != CharacterIterator.DONE ){
if (character == '
result.append("<");
}
else if (character == '>') {
result.append(">");
}
else if (character == '&') {
result.append("&");
} .....
return result;
}
the escaping part is done and works perfectly.. the problem occurs when i try to display the string with escaped characters onto an html page
原文:https://stackoverflow.com/questions/8827896
更新时间:2020-01-26 13:17
最满意答案
if (character == '
result.append("<");
}
else if (character == '>') {
result.append(">");
// ...
删除它。 你不需要它。 JSTL 已经完成了这项工作。
否则,您的HTML字符串将被转义两次。 每个&成为& 再一次等等。 如果你真的需要自己动手逃跑(为什么?)那么就根本不要使用 :
${someBean.someProperty}
或者通过escapeXml="false"关闭它的转义:
if (character == '
result.append("<");
}
else if (character == '>') {
result.append(">");
// ...
Remove this. You don't need it. The JSTL already does this job.
Your HTML string is otherwise escaped twice. Each & becomes an & again and so on. If you really need to take the escaping in own hands (why?) then just don't use at all:
${someBean.someProperty}
or turn off its escaping by escapeXml="false":
相关问答
简短的回答:
还有另一种选择:
${fn:escapeXml(myString)}
Short answer:
there is another option:
...
StringUtils.replaceEach(str, new String[]{"&", "\"", ""}, new String[]{"&", """, "<", ">"})
StringUtils.replaceEach(str, new String[]{"&", "\"", ""}, new String[]{"&", """, "<", ">"})
您可以使用html_safe。 html_safe实际上“将字符串”设置为HTML安全(它比这更复杂,但它基本上就是这样)。 这样,您可以随意从助手或模型返回HTML安全字符串。 h只能在控制器或视图中使用,因为它来自帮助程序。 它将强制输出转义。 它并没有真正被弃用,但你很可能不再使用它了:唯一的用法是“恢复”html_safe声明,非常不寻常。 使用raw前置表达式实际上等同于在其上调用html_safe,但是,就像h一样,在辅助函数上声明,因此它只能在控制器和视图上使用。 以下是关于Safe
...
尝试在标准模板中定义内容。 使其跨浏览器兼容。 String content ="
Test Me
Test Me 2.
";
Try to define the content in standard template. To make it cross browser compatible. String content ="
...
您的服务器启用了“魔术引号” 。 这会自动转义通过$_GET和$_POST进入的所有'和' 。 建议您禁用此“功能”。 PHP 5.3已弃用它,PHP 5.4已将其删除。 文件: http : //www.php.net/manual/en/security.magicquotes.php Your server has "magic quotes" enabled. This automatically escapes all ' and " that come in via $_GET and
...
为什么不让框架为你做好工作? 你可以试试HtmlEncode : string encodedHtml = Server.HtmlEncode(html);
Why not let the Framework do the work for you? You could try HtmlEncode: string encodedHtml = Server.HtmlEncode(html);
如果您使用的是Web API,则无需调用JsonConvert.SerializeObject() 。 将方法的返回类型更改为Model类(而不是string ),然后只返回模型。 Web API将为您序列化它。 If you are using Web API, then you don't need to call JsonConvert.SerializeObject(). Change the return type of your method to your Model class (
...
if (character == '
result.append("<");
}
else if (character == '>') {
result.append(">");
// ...
删除它。 你不需要它。 JSTL 已经完成了这项工作。
否则,您的HTML字符串将被转义两次。 每个&成为& 再一次等等。 如果你真的需要自己动手逃跑(为什么
...
我使用SO使用的东西。 它是opensource并具有多种语言的解析器。 名字是WMD ,问题是“WMD编辑器开源项目在哪里?” 有一些QA材料概述了这个编辑器。 “运行showdown.js服务器以将Markdown转换为HTML(在PHP中)”的问题有一些QA材料概述了PHP中的一些Markdown库。 I use what SO uses. it is opensource and has parsers for many languages. The name is WMD and the
...
不太确定真正了解整个问题。 无论如何,这是我指出的导致一些问题的因素。 当你像这样创建一个 : '' + this.typeName +''
然后this.typeName用于填充value属性和节点内容。 在后一种情况下,一切都很好,正如您所报告的那样,因为当前的typeName内容在此处没有什么特
...