jsp中重音字符的问题

时间:2013-07-15 00:08:02

标签: java jsp encoding struts2

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

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

当我在action类中选择参数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"); 

提前致谢。

1 个答案:

答案 0 :(得分:7)

最佳做法是在任何地方使用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作为文本文件编码

转到偏好设置 - &gt; 一般 - &gt; 工作区 - &gt; 文本文件编码,并设置Other: UTF-8

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

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

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

这是为了在任何地方保留正确的字符所需的步骤。

相关问题