2014-11-15 27 views
0

如何配置SpringMVC的注释消息编码? 例如,我注释的形式的字段进行验证这样的:Spring注解消息编码

@NotEmpty(message = "ъ") 

但是,当发生错误,则在错误的字符集进行编码。 已启用web.xml中的编码过滤器并且* .java文件是UTF-8。 server.xml中的T​​omcat连接器配置为处理UTF-8。

+0

Java文件使用UTF8,但是您是否配置了编译器使其将Java文件读取为UTF8? http://stackoverflow.com/questions/1726174/how-to-compile-a-java-source-file-which-is-encoded-as-utf-8 –

回答

0

如果您知道原始字符集,则可以尝试将原始字符串转换为UTF。例如:

public static String convertFromISO88591ToUTF8(String original) 
{ 
    String converted = original; 
    try 
    { 
     byte[] iso88591Bytes = original.getBytes("ISO-8859-1"); 
     converted = new String(iso88591Bytes, "UTF8"); 
    } 
    catch (UnsupportedEncodingException e) 
    { 
     e.printStackTrace(); 
    } 
    return converted; 
} 

其实,我一直在使用的后端同样的方法(没有变化),即使不考虑对前端的编码和已工作的罚款。但是,我没有对所有编码类型进行系统测试。

我希望它有帮助。