0

I'm working with spring mvc. I've set up a web form that has two simple text inputs. On controller, I use @ModelAttribute to let spring build the bean from the web form.

The problem comes when user puts on those text fields specials characters, like 酒店 and this kind of stuff, spring doesn't read it as utf-8, and they become the usual bad-encoded string.

I've checked web.xml and there's the utf-8 encoding filter, all pages are marqued as utf-8 and browser is sending right charset headers. Any idea on what's going on?

3 Answers 3

1

You may want to check this out: http://forum.springsource.org/showthread.php?81858-ResponseBody-and-UTF-8

The short of it is that if you are using annotated methods then the messageconverter being used has a default character set. You can change this setting in your web.xml by setting the supported media types.

However, if your service doesn't like that media type, you may get a 406 error. You can create your own string message converter and set the default encoding, but there is no easy way with the built in HttpStringMessageConverter.

Alternately you can re-encode a string to a different character set:

String newresponse = new String(responseString.getBytes("ISO-8859-1"), "UTF-8"); 

You may also want to check out the related question here: How to get UTF-8 working in Java webapps?

Sign up to request clarification or add additional context in comments.

Comments

0

the solution is simple by add produces = "text/plain;charset=UTF-8" to request mapping you can force spring mvc to encode the returned text.

Comments

0

I ran into a similar problem with the @ModelAttribute to fetch a model as parameter for my controller method. Then I hit upon a easy solution with help from chatgpt.

Just add this settings to your web.xml file.

If you want to pass unicode from jsp to java then:

<filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value> <!-- Set the desired character encoding -->
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern> <!-- Apply the filter to all URLs -->
    </filter-mapping>

If you want to pass unicode characters from java to jsp:

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

The encodingFilter forcefully encodes the characters in UTF-8 (you can change it to any other encoding). The jsp-config applies the page-encoding to all jsp pages opposed to specifying page-encoding in all pages individually. It helps jsp to render unicode charcters correctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.