JSF 1.1中的字符编码

时间:2011-05-15 07:28:22

标签: jsf utf-8 character-encoding sakai

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

./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" />

2 个答案:

答案 0 :(得分:1)

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

您需要将pageEncoding明确设置为UTF-8。

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

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

答案 1 :(得分:0)

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

例如,Java编写器将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。但除此之外,如果你的字符串是腐败的,那已经太晚了。你正在把错误修复到错误的地方。