jsp 页面显示非英语的垃圾字符
我有一个主 JSP 页面,例如 jsp1,其中包含两个 JSP 页面(jsp2、jsp3)。这些页面中的所有字符串都来自属性文件。
非英文属性文件使用native2ascii进行转换
native2ascii –encoding="8859-1" lang.properties lang1.properties
所有JSP页面都有 <%@ 页面 contentType="text/html;charset=UTF-8" language="java" %>
现在,当显示主jsp页面(jsp1)时,我们会看到jsp2和jsp3中的一些字符串出现乱码。到目前为止,我已经看到这种情况发生在俄语、韩语、日语字符串上。它发生在随机字符串上。
有谁知道可能出了什么问题
更新更多详细信息
rus_utf8.proeperties 中的字符串是
Щелкните УСТАНОВИТЬ СЕЙЧАС и сохраните файл в некотором расположении
使用native2Ascii转换后,rus.properties中的字符串为
\u0429\u0435\u043b\u043a\u043d\u0438\u0442\u0435 \u0423\u0421\u0422 \u0410\u041d\u041e\u0412\u0418\u0422\u042c\u0421\u0415\u0419\u0427\u0410\u0421 \u0438\u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u0435\u0444\u0430\u0439\u043b\u0432\u043d\u0435\u043a\u043e\u0442\u0 43e\u0440\u043e\u043c\u0440 \u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0438.
在 JSP 中,我们使用 struts
来加载字符串属性文件
在 Firefox 中,字符串显示为
��елкните УСТАНОВИТЬ СЕЙЧАС и сохраните файл в некотором расположении.
字符 Щ
出现乱码。页面中其他位置的相同字符串已正确显示。
I have a Main JSP page say jsp1 which includes two JSP pages (jsp2, jsp3). All the strings in these pages come from property files.
The non-english property files are converted using native2ascii
native2ascii –encoding="8859-1" lang.properties lang1.properties
All the JSP pages have
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Now when main jsp page(jsp1) gets displayed, we see garbled characters in a few strings of jsp2 and jsp3. Till now I have seen this happening to Russian, Korean, Japanese language strings. And it happens on a random string.
Does any one have an idea what could be wrong
Updating with more details
The string in rus_utf8.proeperties is
Щелкните <strong>УСТАНОВИТЬ СЕЙЧАС</strong> и сохраните файл в некотором расположении
After Conversion using native2Ascii, String in rus.properties is
\u0429\u0435\u043b\u043a\u043d\u0438\u0442\u0435 <strong>\u0423\u0421\u0422\u0410\u041d\u041e\u0412\u0418\u0422\u042c \u0421\u0415\u0419\u0427\u0410\u0421</strong> \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u0435 \u0444\u0430\u0439\u043b \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0438.
In JSP we use struts <s:text>
to load the string from property file
In firefox the string got displayed as
��елкните УСТАНОВИТЬ СЕЙЧАС и сохраните файл в некотором расположении.
The char Щ
got garbled. Same String in some other place in the page got displayed properly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是无效的。除了
您遇到的语法错误(应该立即中止native2ascii)之外,ISO-8859-1 编码对于俄语、韩语和日语字符串来说不可能是正确的。 ISO-8859-1 编码根本不涵盖这些字符。假设您将其保存为 UTF-8,那么您应该使用
这种方式,native2ascii 将从 UTF-8
lang.properties
转换为 ISO-8859-1 兼容的lang1.properties
。 native2ascii 将始终转换为 ASCII。-encoding
属性涉及源文件的编码,而不是目标文件的编码。至于 JSP 页面,
根据 http://wiki,只需一个就足够了。 apache.org/tomcat/FAQ/CharacterEncoding#Q8。
另请参阅:
更新按照您对示例的更新。一切实际上都运转正常。它看起来很像 UTF-8 BOM (字节顺序标记) 是罪魁祸首。记事本默认添加它。尝试在其他编辑器(例如 Eclipse)中创建属性文件。
This is invalid. It should have been
Apart from the syntax error which you have there (which should immediately have aborted native2ascii), the ISO-8859-1 encoding can impossibly be correct for Russian, Korean and Japanese strings. The ISO-8859-1 encoding does not cover those characters at all. Assuming that you saved it as UTF-8, then you should be using
This way the native2ascii will convert from an UTF-8
lang.properties
to an ISO-8859-1 compatiblelang1.properties
. The native2ascii will always convert to ASCII. The-encoding
attribute concerns the encoding of the source file, not the target file.As to the JSP pages, just a
ought to be sufficient, per http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q8.
See also:
Update as per your update with the examples. Everything is actually working right. It only look much like that the UTF-8 BOM (Byte Order Mark) is the culprit. Notepad adds it by default. Try creating the properties file in another editor instead like Eclipse.
我和你有同样的问题,我也尝试过类似的解决方案,但没有用。因此我怀疑这可能不是JSP配置的问题,而是我的tomcat的配置问题。
我在一个中文网站上找到了这个,https://openhome.cc/Gossip/Encoding/Servlet。 html:
request.setCharacterEncoding("UTF-8");
。这对我有用。我在request.getParameter();
之前添加了它。I have the same problem as you and I also tried the similar solutions but they didn't work. Hence I suspected that it may not be an issue with JSP config but rather it is a config issue with my tomcat.
I found this on a Chinese site, https://openhome.cc/Gossip/Encoding/Servlet.html:
request.setCharacterEncoding("UTF-8");
. It worked for me. I added this before myrequest.getParameter();
.