简体   繁体   中英

Spring annotations messages encoding

How do I configure SpringMVC's annotation messages encoding? For example, I annotated a field of the form to be validated like this:

@NotEmpty(message = "ъ")

But when the error occurs, it is encoded in a wrong charset. Encoding filter in web.xml is enabled and *.java files are UTF-8. Tomcat's Connector in server.xml is configured to deal with UTF-8.

If you know the original charset, you can try to convert the original string to UTF. For example:

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;
}

Actually, I've been using this same method (without changes) on the back-end even regardless the encoding on the front-end and it has been working fine. However, I didn't make a systematic test for all encoding types.

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM