Environment: Mysql,spring mvc3.2.0,jquery v2.0.3, using JdbcTemplate to access the database, is equivalent to a full Spring solution.
Phenomenon
Using the form post directly, or using the jquery post data to the server, the Chinese strings obtained in the controller are garbled. When you arrive jdbctemplate insert or update the database, it is also garbled.
Resolution measures
Modify Web. XML to add the encoding filter as follows (note that the forceencoding parameter value is set to True)
<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> </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></filter-mapping>
Effect
The Chinese string obtained in the controller is normal. However, after using JdbcTemplate to insert or update the database, it is still garbled. You can determine that there is a coding problem when writing to the database.
Resolution measures
Modify the Applicationcontext.xml file to set the URL for MySQL access to: url= "jdbc:mysql://localhost:3306/xxx?useunicode=true& Characterencoding=utf-8 "
Effect
Writing to the database is also normal, reading and displaying normal on the Web page.
Reference
http://blog.csdn.net/xuechongyang/article/details/8283924
http://hi.baidu.com/fuzk2008/item/956351c17a47d653bcef6956
Spring Mvc+mysql save Chinese into garbled