1. When using ajax to transfer data from the foreground page to the background controller, Chinese garbled code (question mark???) appears.
I looked for various solutions online before, but they didn't work. Finally, I found that it was a problem with the tomcat server receiving data.
Solution:
Method 1: Transcode the parameters when the controller receives the parameters
@ResponseBody@RequestMapping(value="/getJsonDataByCityName",produces="application/json")public String getJsonByName(HttpServletRequest request,HttpServletResponse response,@RequestParam String city_name)throws ServletException,IOException{ //response.setContentType("text/html;charset=UTF-8"); //request.setCharacterEncoding("UTF-8");//Solve the post garbled problem System.out.println(request.getCharacterEncoding()); city_name = new String(city_name.getBytes("ISO-8859-1"), "UTF-8"); System.out.println("city_name:"+city_name);}Method 2:
Configure the service.xml file in the tomcat directory
tomcat7/conf/server.xml
Add the encoding attribute of URIEncoding="UTF-8" to this line of code
<Connector port="8080" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" URIEncoding="UTF-8" />
Cause analysis:
In fact, the reason for the garbled problem is that the default tomcat configuration, the request received is transcoded with ISO-8859-1, which leads to the garbled problem in Chinese. As long as the garbled code can be correctly transcoded with utf-8, the garbled problem can be solved.
2. Ordinary data transmission, from the jsp page to the background controller, solution to the problem of garbled in Chinese
(1) First check whether the jsp page encoding format is utf-8
(2) Set Chinese filtering
<!-- Chinese encoding--> <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> <init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
(3) Set JSON data conversion in springMvc configuration file
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <!-- Start Spring MVC annotation function, complete the mapping of requests and annotations POJO mapping annotation request mapping is ISO-88859-1 by default, avoiding garbled codes. This is set to UTF-8 --> <bean> <property name="supportedMediaTypes" value="text/html;charset=UTF-8" /> </bean> <!-- Start JSON format configuration, automatically convert the format to JSON format, no other classes are required --> <bean id="jacksonMessageConverter"> <property name="supportedMediaTypes" value="application/json;charset=UTF-8" /> </bean> </mvc:message-converters></mvc:annotation-driven>
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.