【问题标题】:Problems with accented characters in jspjsp中重音字符的问题
【发布时间】:2013-07-12 18:34:11
【问题描述】:

我正在尝试在表单上收集重音字符 [áéíóúÁÉÍÓÚ],但未正确发送到操作:

JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
[. . .]
<s:form action="resultRot" method="post" theme="simple">
<s:textfield name="param" theme="simple" size="20" maxlength="20" style="text-transform: uppercase; text-align:center"/>
<s:submit name="submit" key="ejercicios.roturaPalabras.corregir" align="center"/>

当我在动作类中选择参数 param 时,它不包含正确的值。 我使用 Eclipse,我检查了项目编码是 ISO-8859-1

我也尝试过使用 UTF-8 编码(在我的 jsp 中):

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

我也尝试过使用 URLDecoder/Encoder:

String prueba = java.net.URLDecoder.decode(solucionIntroducida, "ISO-8859-1"); 

提前致谢。

【问题讨论】:

  • ISO-8859-1 在字符集中不包含这些字符。

标签: java jsp encoding struts2


【解决方案1】:

最佳做法是在任何地方使用UTF-8

Here you can find how to do it by altering the application server's connectors,而对于其他部分,您可以简单地在每个 JSP 中指定它(就像您正在做的那样),或者在 web.xml 中指定一次:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>

要在 Eclipse 中调试(和打印)Request,您需要确保使用UTF-8 作为文本文件编码

进入 Preferences -> General -> Workspace -> Text file encoding ,设置@987654328 @

在代码中,务必在将 String 转换为 Byte[] 时始终指定字符编码,反之亦然:

String str = new String(myByteArray,"UTF-8");

byte[] ba = myString.toByteArray("UTF-8");

这是在各处保留正确字符所需的步骤。

【讨论】:

  • 谢谢,您的解决方案适用于带有重音符号的参数。现在,当我使用属性文件时会出现问题,该文件必须使用 ISO-8859-1 编码而不是 UTF8 才能正确显示文本。这不是一个大问题。主要问题是从文本文件中读取带有重音符号的单词并将它们显示在 jsp 中,它使用 ISO-8859-1 显示正确的字符,但使用 UTF8 则不显示。
  • 我使用 FileInputStream 逐行读取文件: FileInputStream fstream = new FileInputStream(nameFile); DataInputStream 条目 = new DataInputStream(fstream); BufferedReader buffer = new BufferedReader(new InputStreamReader(entry));字符串strLine; while ((strLine = buffer.readLine()) != null) { result.add(strLine); }
  • 有效!使用编码 ISO-8859-1 的方法(使用 UTF8 不起作用)File fileDir = new File(nombreFichero); BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileDir), "ISO-8859-1")); String strLine; while ((strLine = in.readLine()) != null) { resultado.add(strLine); } in.close(); 谢谢!
猜你喜欢
  • 1970-01-01
  • 2011-06-14
  • 2011-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-09
  • 1970-01-01
  • 2021-07-07
相关资源
最近更新 更多