Solve SPRINGMVC Chinese garbled problem

Source: Internet
Author: User

to tell the truth, Itoo the initial development of the most troublesome except that there is no way to heat deployment is garbled problem, not database Chaos is the page chaos, really is the heart plug ah, good data is always garbled so that human favor.

Here to summarize a few can solve the problem of Chinese garbled, I hope to help you:

usually we set the encoding format to UTF-8 .

First case:

JSP page Chinese input, to Controller garbled, this time you need to set the Web. XML A coded filter is added to the file ( Filter ) to unify the encoding UTF-8 , the code is as follows :

Web. XML configuration file:

<filter>    <filter-name>CharacterEncodingFilter</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 ></filter><filter-mapping>    <filter-name>CharacterEncodingFilter</filter-name>    <url-pattern>/*</url-pattern></filter-mapping>


It is important to note that this code is best placed in the Web. XML in the beginning of the position, because the interception in order, if placed in the back of the words are easy to intercept.

Second case:

database Chinese data, JSP the page is garbled (not in the strict sense of garbled characters, but in the form of question marks)

since our data interaction with the front and back tables is JSON data, the cause of this situation I am not very clear, has not met before, can only blame oneself did the project too little, solve it is not difficult, just to turn JSON when you set the encoding format, the code is as follows:

Response.setcontenttype ("Application/json;charset=utf-8");//Prevent data transmission garbled

Write this sentence will not appear garbled.

The third case:

page Chinese, passed to Controller is also correct, but after saving to the database is garbled (also not the strict meaning of garbled, as the above is the question mark)

This problem bothered me for some time, began to feel that the database encoding format is incorrect, re-created the encoding format for UTF -8 of the database is still not able, and finally feel that it is JBoss the problem that our server uses is JBoss , on the Internet to check the data in the connection to the source of the encoding format on it, the code is as follows:

     <datasource jta= "true" Jndi-name= "Java:jboss/datasources/jcmysqlds" pool-name= "Jcmysqlds" enabled= "true" Use-java-context= "true" >                    <connection-url>jdbc:mysql://192.168.24.46/itoo_basic_basic?useunicode= true&characterencoding=utf-8</connection-url>                    <driver>mysql</driver>                    <pool >                        <prefill>false</prefill>                        <use-strict-min>false</use-strict-min>                        < flush-strategy>failingconnectiononly</flush-strategy>                    </pool>                    <security>                        < user-name>root</user-name>                        <password>123456</password>                    </security>                </datasource>

Chinese garbled really is a headache problem, but fortunately all can be solved, I hope you have anything on the Chinese garbled solution can be shared.

Solve SPRINGMVC Chinese garbled problem

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.