转义Unicode有时会正确显示,但有时会呈现为HTML实体

3
我遇到一个问题,我的.properties文件中的某些值在UI中无法正确显示,但大多数都可以。我看到的是带有HTML实体名称的字母而不是带有音符的字母。我将解释我已经做过的事情:
一开始,我无法使任何带有音符的字母正确显示。幸运的是,我找到了this post,通过在我的.properties文件中使用转义Unicode,我能够取得进展。
(顺便说一句,虽然转义的Unicode基本上解决了问题,但它使.properties文件难以阅读。幸运的是,IDEA提供了一个选项,可以使用转义的Unicode并仍然使用可读的字符来读取文件。在此处阅读更多信息。)
现在我的问题是:在我的应用程序的某些部分,带有变音符号的字母会显示为Latin-1 HTML实体名称。例如,我看到的是"& amp;ccedil;"(我在&和amp之间添加了一个额外的空格,否则它将呈现为和号),而不是'ç'。起初,我甚至不知道那是什么意思,但在查看this table后,我知道这是ISO-8859-1实体名称。
以下是我尝试过的方法,但没有成功地给我带来带有变音符号的字符。
  1. Although I'm still using Glassfish 2, I found this post, and tried adding the following to my web.xml

    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>UTF-8</page-encoding>
        </jsp-property-group>
    </jsp-config>
    

    Now, when I check the response headers in Chrome dev tools, I can see the the following:

     Content-Type:text/html;charset=UTF-8
    

    However, I still see the aforementioned HTML entity names in my UI.

  2. I tried explicitly setting the charset within the JSP itself by adding the following to the JSP where the values are pulled from the .properties file:

    <%@page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
    
  3. While trying to fix this, I've read that ISO 8895-1 is the default encoding for properties files, so I tried changing the encoding within IDEA (I'm using 11, btw). You can do so by going to Settings > File Encodings. At the bottom is an option entitled "Default encoding for properties file", and I changed it to UTF-8. However, I still see the HTML entity names.

我已经尝试了一段时间,现在我终于到了无可奈何的地步。有什么建议吗?

1个回答

0

我不太明白属性文件如何适用于此,但我的建议是编写一个函数,逐个字符地遍历字符串,并将所有charcode大于128的字符更改为&#charcode;,其中charcode是十进制代码,在显示之前将所有文本都通过该函数运行。然后,即使您实际上没有正确设置页面编码,浏览器仍应能够正确处理这些字符。(例如,'ç'将变为&#231;)

例如,而不是'ç',我看到"& amp;ccedil;"(我在&和amp之间添加了一个额外的空格,否则它会呈现为一个和符号)

相反,您应该将&amp;ccedil;更改为&ccedil;,然后浏览器将显示实际字符。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接