The £ sign is shown as a diamond with a question mark in the middle

蹲街弑〆低调 提交于 2019-12-31 07:43:48

问题


I'm creating a webapp which involves displaying financial data to the user. Being from the UK and using GBP £ for currency, this character is used a lot.

However, every now and then, the £ is shown as a diamond with a question mark in the middle, and on the web page it throws an invalid charachter UTF-8 byte 1 of 1 byte string.

Is there a UTF safe way to display the £ sign? Here is an example of what I am doing at the moment:

 "Rent Per Annum: £" + '${tenant.currentRent}'

回答1:


The particular problem can have at least the following one or more causes:

  1. The JSP file is not by the editor (Eclipse, Netbeans, Notepad, etc) been saved using UTF-8 encoding.

  2. The server didn't use UTF-8 to decode the characters produced by the JSP to a byte stream before sending it over network.

  3. The browser didn't use UTF-8 to encode the byte stream from the network to characters.

Those problems can be solved as follows:

  1. Configure the editor to save JSP files using UTF-8. I'm not familiar with STS, but I know that it is Eclipse based, so it'll probably be the same as in standard Eclipse. Go to Window > Preferences > General > Workspace > Text File Encoding and then pick the right encoding in the dropdown.

    An alternative is to use the HTML entity £ (as suggested by the other answerer), this way it's not relevant anymore in which encoding the JSP file is saved. All characters involved in the string £ are supported by the basic ASCII encoding already (every decent character encoding used in the world basically "extends" ASCII, so it'll always work) and the HTML interpreter (the webbrower) will translate the HTML entity into the right character.

  2. The server has to be instructed to use UTF-8 to decode the JSP output. This can on a per-JSP basis be done by

    <%@page pageEncoding="UTF-8" %>
    

    or on an application-wide basis by

    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <page-encoding>UTF-8</page-encoding>
        </jsp-property-group>
    </jsp-config>
    
  3. The browser has to be instructed to use UTF-8 to encode the HTTP response. This is to be solved by setting the charset attribute of the HTTP response Content-Type header to UTF-8, which is already implicitly done by the solution to cause #2.

See also:

  • Unicode - How to get the characters right?



回答2:


A portable way of writing this in HTML as an entity is &pound; or in the general case by its character code &#163; or &#xA3; £. This way, your source is plain 7-bit ASCII, so basically independent of encoding (ignoring esoterics like EBCDIC etc). See also http://www.fileformat.info/info/unicode/char/a3/index.htm



来源:https://stackoverflow.com/questions/12338763/the-%c2%a3-sign-is-shown-as-a-diamond-with-a-question-mark-in-the-middle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!