简体   繁体   English

为什么转义字符在HTML中显示?

[英]why are the escape characters being displayed in 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. 但是,当我在html中显示String时,仍将显示转义字符。 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 我已经读过某个地方,taglib中的核心只会逃避基本的,例如>,<,“,\\ t和空格。但是这些转义序列都没有从html代码中删除。你们中的任何人都知道如何成为能解决这个问题吗?谢谢

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("&lt;");
         }
         else if (character == '>') {
           result.append("&gt;");
         }
         else if (character == '&') {
           result.append("&amp;");


                } .....
       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 转义部分已完成且工作正常..当我尝试将带有转义字符的字符串显示到html页面时,会出现问题

if (character == '<') {
    result.append("&lt;");
}
else if (character == '>') {
    result.append("&gt;");
// ...

Remove this. 删除它。 You don't need it. 你不需要它。 The JSTL <c:out> already does this job. JSTL <c:out>已经完成了这项工作。

<c:out value="${someBean.someProperty}" />

Your HTML string is otherwise escaped twice. 否则,您的HTML字符串将被转义两次。 Each & becomes an &amp; 每个&成为&amp; again and so on. 再一次等等。 If you really need to take the escaping in own hands (why?) then just don't use <c:out> at all: 如果你真的需要自己动手逃跑(为什么?)那么就根本不要使用<c:out>

${someBean.someProperty}

or turn off its escaping by escapeXml="false" : 或者通过escapeXml="false"关闭它的转义:

<c:out value="${someBean.someProperty}" escapeXml="false" />

BalusC has nailed it. BalusC已经钉了它。

A couple of additional points: 还有几点要点:

  • If you get problems with web pages not looking right, one of the things you should do is to look at the raw HTML using your web browser's "view source" function. 如果您遇到的网页问题不正确,您应该做的一件事就是使用网络浏览器的“查看源”功能查看原始HTML。 In this case, it would have shown the double escaping, and a quicker realization of what the problem was. 在这种情况下,它会显示双重逃逸,并更快地实现问题所在。

  • In HTML, you should only need to escape < , > and & . 在HTML中,您只需要转义<>& Other characters should work just fine provided that your HTML is encoded in UTF-8 (and the content type says so too). 如果您的HTML以UTF-8编码(其内容类型也是如此),其他字符应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM