Encoding problem using Spring MVC

前端 未结 6 1093
北海茫月
北海茫月 2020-12-10 12:56

I have a demo web application that creates users. When I try to insert data in other languages (like french) the characters are not encoded correctly. The code on the contr

相关标签:
6条回答
  • 2020-12-10 13:30

    Try making CharacterEncodingFilter the first filter in web.xml.

    I realize this question is a little old, but I just ran into the same problem, and moving CharacterEncodingFilter fixed it for me.

    0 讨论(0)
  • 2020-12-10 13:34

    There are two things to help:

    1. In your setenv.sh file add JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=utf-8"
    2. In the script that starts your tomcat process, set the LANG environment: export LANG='utf-8'

    This can then be tested by examining the default character set: Charset.defaultCharset().

    0 讨论(0)
  • 2020-12-10 13:35

    You need to add accept-charset="UTF-8" to your form.

    0 讨论(0)
  • 2020-12-10 13:49

    Perhaps I'm missing something, but if the page-encoding in your JSP is "UTF-8", shouldn't the encoding in your CharacterEncodingFilter be UTF-8 rather than ISO-8859-7?

    0 讨论(0)
  • 2020-12-10 13:50

    Output of System.out.println() depends on console encoding, so it's not a good way to debug encoding problems.

    To check that your values are decoded properly, you should show it at another page. Actually, it's already done in the case of form validation failure, so your system works fine if values in the fields remains the same after validation error.

    0 讨论(0)
  • 2020-12-10 13:53

    If this still does not work and you're using Tomcat as your application server try to set the following option on every <Connector> element in the server.xml:

    <Connector URIEncoding="UTF-8" ...>
        ...
    </Connector>
    

    This did the trick for me. There might be similar options for other application servers, so you might want to check the server documentation.

    0 讨论(0)
提交回复
热议问题