jsp中使用带有重音符号的字符出现问题

4

我将尝试收集表单中的重音字符 [áéíóúÁÉÍÓÚ],但它们没有正确地发送到操作:

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字符集中不包含这些字符。 - Roman C
1个回答

7
最佳实践是在任何地方都使用 UTF-8这里可以找到如何通过修改应用服务器连接器来实现,而对于其他部分,您可以在每个 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 ,并设置 Other: UTF-8
在代码中,将 String 转换为 Byte[] 或反之时,请始终指定字符编码:
String str = new String(myByteArray,"UTF-8");

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

这是保持正确字符的步骤。


谢谢,你的解决方案对带有重音符号的参数起效果了。 现在问题出现在我使用属性文件时,该文件必须使用ISO-8859-1编码而不是UTF8才能正确显示文本。 这并不是一个大问题。 主要问题是从文本文件中读取带有重音符号的单词并在jsp中显示它们,它可以使用ISO-8859-1正确显示字符,但不能使用UTF8。 - user2213180
我使用FileInputStream逐行读取文件: FileInputStream fstream = new FileInputStream(nameFile); DataInputStream entry = new DataInputStream(fstream); BufferedReader buffer = new BufferedReader(new InputStreamReader(entry)); String strLine; while ((strLine = buffer.readLine()) != null) { result.add(strLine); } - user2213180
1
成功了!使用 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(); 谢谢! - user2213180

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接