我需要您的帮助来显示一些存储在 xhtml 页面变量中的阿拉伯文本。我已在 jdeveloper 中配置我的项目以在属性中包含 UTF-8,并且阿拉伯文本可以正确显示。我有一个名为bankName 的变量,它的unicode 值是:
String bankName = "\u0627\u0644\u0628\u0646\u0643 \u0627\u0644\u0645\u062a\u062d\u062f";
在 xhtml 中,当我打印变量输出 <h:outputText value="#{hrd.bankName}" style="font-weight:bold" />
时,阿拉伯文本正确显示 "البنك المتحد"
,但是相同的 unicode 值存储在数据库字段中,我从下面的代码中检索它的值:
String bankName=result.getString("bank_name_arabic").trim();
xhtml 会将阿拉伯文本显示为文本:
\u0627\u0644\u0628\u0646\u0643 \u0627\u0644\u0645\u062a\u062d\u062f
在 xhtml 页面中,不会给出阿拉伯语的值。
那么我怎样才能实现这一目标呢?
最佳答案
这一行:
String bankName = "\u0627\u0644\u0628\u0646\u0643 \u0627\u0644\u0645\u062a\u062d\u062f";
完全等同于:
String bankName = "البنك المتحد";
转义(例如,\n
)不是 Java 字符串中内置的机制。 Java 编译器会为您执行这些替换。想象一下,有一个包含这两个字符的文本文件:\ 和 n。如果您像这样阅读它们:
Path path = Paths.get("yourFile.txt");
String text = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
现在您可以检查文本内容:它恰好是两个字符:\和n然后:
System.out.format("Content is '%s'", text);
将打印:
Content is '\n'
After this introduction you should understand why it doesn't work what you're doing: you're storing an escaped string in your database column but, as we said, unescaping is performed only by compiler and you get exactly what you stored.
If you can I'd suggest to simply store "البنك المتحد" in your database column.
If you can't change that and you ALREADY USE Apache Common Lang you can use an utility function to perform unescaping:
String bankName = org.apache.commons.lang.StringEscapeUtils.unescapeJava(
getString("bank_name_arabic").trim());
当然,我不建议仅将其用于此目的(它很小),如果您需要它,请检查 tchrist的回答How to unescape a Java string literal in Java .
关于java - 如何在从数据库检索的页面中显示阿拉伯语 unicode 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30868974/