【问题标题】:character encoding in JSF 1.1JSF 1.1 中的字符编码
【发布时间】:2011-05-15 07:28:22
【问题描述】:

我正在使用 JSF 1.1 开发一个工具,但我遇到了这个问题: 我的支持 bean 中有一个字符串,打印为:

./src.cpp: In function ‘int main()’:
./src.cpp:4: error: ‘dsdada’ was not declared in this scope

在一个 txt 文件上。

但是当我把它放在 h:inputTextArea 上时,它是这样的:

./src.cpp: In function ‘int main()’:
./src.cpp:4: error: ‘dsdada’ was not declared in this scope

-

<%@ page contentType="text/html;charset=UTF-8" %>
and this
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>

但它也没有工作。 有人可以告诉我如何解决这个问题。谢谢

/* String[0] as stdout, String[1] as stderr */
String[] results = sshBO.execCommand(cmd, timeout);

/* Done with SSH things */
sshBO.closeSession();

/* Bring the output and err to the presentation */
msg = results[1]+results[0];
FileServices.saveStringToFile("F:/myoutput.txt", msg);
msg = new String(msg.getBytes("UTF8"), "UTF8"); /* makes no difference */

在 JSP 页面上:

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://sakaiproject.org/jsf/sakai" prefix="sakai" %>

<f:view >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
...

<h:inputTextarea disabled="true" value="#{SSH.msg}" styleClass="myTextArea" />

【问题讨论】:

    标签: jsf utf-8 character-encoding sakai


    【解决方案1】:

    字符 存在于字节0xE2 0x80 0x98 的Unicode 中。当您使用CP1252(Windows 默认)编码对这些字节进行编码时,您会得到‘

    您需要将pageEncoding 显式设置为UTF-8。

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

    这样它将使用 UTF-8 打印字符隐式设置 Content-Type 标题。

    【讨论】:

    • 感谢 BalusC,我尝试将 添加到我的页面,但它似乎不起作用。出现同样的问题。还有什么我应该尝试的吗?顺便说一句,你有一个很棒的博客。我确实学会了从你的博客上传文件:)。
    【解决方案2】:

    为了确定转码错误的来源,请在每次转码操作后检查数据。使用工具like this one 来确定应该是什么值。

    例如,您的 JSP 编写器正在将 Java 字符串从 UTF-16(Java 字符串的编码)转码为 UTF-8。另一个转码操作看起来像是从本机系统读取程序输出。

    /* String[0] as stdout, String[1] as stderr */
    String[] results = sshBO.execCommand(cmd, timeout);
    

    例如,您可以使用如下代码打印字符串的十六进制值:

    for (char ch : msg.toCharArray())
      System.out.format("%04x ", (int) ch);
    

    代码点 应打印为2018。您的文件编写代码可能与读取输入的代码有类似的错误,从而误导您了解问题的根源。


    msg = new String(msg.getBytes("UTF8"), "UTF8"); /* makes no difference */
    

    这没什么区别,因为它需要 UTF-16 数据,将其转码为 UTF-8,然后将其转码回 UTF-16。但除此之外,如果您的字符串已损坏,则为时已晚。你在错误的地方修复错误。

    【讨论】:

      猜你喜欢
      • 2011-11-17
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      相关资源
      最近更新 更多