A summary of the problems and solutions of Chinese characters garbled in SPRINGMVC project (non-professional optimal solution)--The value of Ajax is garbled; Request.getparameter () garbled;

Source: Internet
Author: User

Situation one: Ajax in the value is garbled (background can get to Chinese characters, but returned with @responsebody the foreground is garbled)

Scenario Two: Controller in Request.getparameter () gets a garbled

@RequestMapping (params = "Method=submit")    publicthrows  exception{        = Request.getparameter ("uname");         return "Index_view";        }

The request default encoding is iso-8859-1, and the foreground is utf-8 encoded, resulting in a fetch value output found to be garbled. From the Internet to find a lot of ways, the following methods are not effective :

1. Add Filter

JSP page Chinese input, to controller garbled, this time needs to be set to add a coded filter in the Web. xml file (filter) to unify the encoding to 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 it is best to place this code at the beginning of the Web. XML because the interception is in order and is easily intercepted if placed behind.

2. Set the request character set

Often from the front desk to the corresponding controller or action after the garbled, tell me the general idea is to print the request itself by default character set

System.out.println(request.getCharacterEncoding());

Then, if you print a character set that is not required, set the appropriate character sets

request.setCharacterEncoding("UTF-8");

Effective workaround : Convert from iso-8859-1 to utf-8 format when acquired. (But this method is more cumbersome)

@RequestMapping (params = "Method=submit")    publicthrows  exception{         New String ((Request.getparameter ("uname")). GetBytes ("Iso-8859-1"), "Utf-8");         return "Index_view";        }

Situation Three:

A summary of the problems and solutions of Chinese characters garbled in SPRINGMVC project (non-professional optimal solution)--The value of Ajax is garbled; Request.getparameter () 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.