问题:
在前端显示的时候, 遇到了中文乱码问题.这导致了在UI上无法正常显示中文. 遇到的问题是在通过读取properties文件, 然后根据键值对的value值显示. 例子如下:
修改前的源码如下:
1 2 3 4 5 6 7
| InputStream inputStream = getClass().getResourceAsStream("/i18n/" + name + ".properties"); Properties properties = new Properties(); if (inputStream != null) { properties.load(inputStream); model.addAttribute("properties", properties); inputStream.close(); }
|
解决办法:
用BufferedReader去读取InputStream, 再load成properties对象.
1 2 3 4 5 6 7 8
| InputStream inputStream = getClass().getResourceAsStream("/i18n/" + name + ".properties"); Properties properties = new Properties(); if (inputStream != null) { BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream)); properties.load(bf); model.addAttribute("properties", properties); inputStream.close(); }
|