Spring Mvc+mysql save Chinese into garbled

Source: Internet
Author: User
Tags form post

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&amp; 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.