Not rendering Chinese characters on in spring MVC

2019-07-20 04:08发布

问题:

I am trying to display chines characters but its showing ??????????. In my spring-servlet I have

 <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages"/>
    <property name="defaultEncoding" value="UTF-8"/>
</bean>

and in my massages.property I have added some chines characters like this 這對中國的考驗

First line of my .jsp file I have

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

If I encode the chines in ascii format then its working fine. but I don't want to do that. Is there anything I am missing.

Please help.

回答1:

As per their documentation, the properties files are by default read using ISO-8859-1 encoding. You'd need to use unicode escapes like as in \uXXXX for each character beyond the supported range of ISO-8859-1. JDK offers the native2ascii tool for this in the /bin folder. You should then use the converted properties file instead.

E.g. (in command console)

native2ascii -encoding UTF-8 some.properties.utf8 some.properties

Where some.properties.utf8 is the properties file which you saved in UTF-8 and some.properties is the converted properties file which you should instead use in your web application.

The

some.key = 這對中國的考驗

would then become

some.key = \u9019\u5c0d\u4e2d\u570b\u7684\u8003\u9a57

Most IDEs like Eclipse, IntelliJ and maybe also Netbeans (not sure as I've never used it) will automatically do this when you use the builtin properties file editor.



回答2:

Can you try changing the encoding to UTF-16 {In property file and in spring bean file}

Some extra words : Chinese characters occupy two bytes so actually it should be strange how they can be represented in UTF-8 format (which ideally should read 8 bits at a time-- my assumption) Anyways the fact is we can represent chinese character can be represented in UTF-8 format some how.