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;