【问题标题】:why java code not converting text to utf8 characters?为什么java代码不将文本转换为utf8字符?
【发布时间】:2014-06-26 11:50:41
【问题描述】:

我的jsp页面编码如下:

<%@ page
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"
%>

<%

response.setHeader("Cache-control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
%>

javascript代码在firebug中很好地打印希腊字符以下是代码:

function save() {
console.log(document.editDistributionListForm.elements['name'].value); //this line prints greek characters
  document.editDistributionListForm.elements['subAction'].value='submit';
  document.editDistributionListForm.submit();
}

当上面的'name'值被发送到struts动作时,字符串不打印希腊字符而是打印????????????????

输入的希腊字符是:αβγδεζηθ

Struts Bean(示例代码):

public void setName(String name) {
                this.name = name;
        }

        public String getName() {
                return name;
        }

Struts Action(示例代码):

protected ActionForward updateList(ActionMapping mapping,
                                HttpServletRequest request,
                                EditListForm form)
                        throws CannotOverwriteExistingListException, EmptyListException,
                                ListUpdateException {
log.debug("form.getName :: "+form.getName()); //this line when printed prints (???) 

}

当数据从 html 发送到 java,即从浏览器发送到服务器时,字符串会从希腊语转换为 ????我做错了什么。我无法确定我错过了什么.. .

请帮忙。

【问题讨论】:

标签: java javascript jsp utf-8 struts-1


【解决方案1】:

UTF-8 中没有希腊字符。

我认为你应该重新编程你的程序,让它打印 UTF-16 字符(包括希腊字符)。 (将页面编码从 UTF-8 改为 UTF-16)

【讨论】:

  • 我认为它应该运作良好。让我让你明白这些事情。我们有管理应用程序和用户应用程序,它们都有不同的战争文件(应用程序部署到 tomcat 服务器)。但是它们共享 java 类文件。当我在 Admin 中进行更改(例如 name 属性)并为其赋予希腊字符并保存时。该应用程序还反映在用户应用程序中,因为它也为相同的属性显示希腊字符。但是当从 User Application 更改时,它们显示 ????字符。从这一点上,我可以说页面编码对两者都可以。但在 java 中出现问题。
  • 我仍然认为您应该更改 charset 和 pageEncoding 变量,只是为了安全起见。两行代码:response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8");只接受 UTF-8 字符......并且应该只打印出 UTF-8 字符。我的意思是你应该尝试一下,看看它是否有效。
  • 我点击了这个链接,它运行良好。 stackoverflow.com/a/138950/3493471 我想你也应该为此推荐其他的。
猜你喜欢
  • 1970-01-01
  • 2017-02-12
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-23
相关资源
最近更新 更多