springmvc post方式提交form时乱码问题——filter方式解决

springmvc post方式提交form时乱码问题——filter方式解决

最近遇到了一个奇怪的问题,就是编码问题。采用springmvc的时候,正常的get方式和ajax方式提交的时候,后台接收的数据都没有问题,但是采用form的post方式提交表单的时候出现了问题。发现后台接收不到数据。
在网上查了好久,各种方式试了个遍,最后还是添加filter的方式最奏效。下面是代码配置:
web.xml
[html]  view plain  copy
  1. <!DOCTYPE web-app PUBLIC  
  2.         "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
  3.         "http://java.sun.com/dtd/web-app_2_3.dtd" >  
  4.   
  5. <web-app>  
  6.     <display-name>Archetype Created Web Application</display-name>  
  7.     <filter>  
  8.         <filter-name>encodingFilter</filter-name>  
  9.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  10.         <init-param>  
  11.             <param-name>encoding</param-name>  
  12.             <param-value>UTF-8</param-value>  
  13.         </init-param>  
  14.         <init-param>  
  15.             <param-name>forceEncoding</param-name>  
  16.             <param-value>true</param-value>  
  17.         </init-param>  
  18.     </filter>  
  19.     <filter-mapping>  
  20.         <filter-name>encodingFilter</filter-name>  
  21.         <url-pattern>/*</url-pattern>  
  22.     </filter-mapping>  
  23.     <servlet>  
  24.         <servlet-name>springmvc</servlet-name>  
  25.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  26.         <init-param>  
  27.             <param-name>contextConfigLocation</param-name>  
  28.             <param-value>/WEB-INF/springmvc-servlet.xml</param-value>  
  29.         </init-param>  
  30.         <load-on-startup>1</load-on-startup>  
  31.     </servlet>  
  32.     <servlet-mapping>  
  33.         <servlet-name>springmvc</servlet-name>  
  34.         <url-pattern>/</url-pattern>  
  35.     </servlet-mapping>  
  36. </web-app>  

springmvc-servlet.xml
[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:context="http://www.springframework.org/schema/context"  
  4.        xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.        xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/beans  
  8.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  9.         http://www.springframework.org/schema/context  
  10.         http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  11.         http://www.springframework.org/schema/mvc  
  12.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
  13.   
  14.     <context:component-scan base-package="com.xueyoucto.xueyou.controller"/>  
  15.     <context:component-scan base-package="com.xueyoucto.xueyou.utils"/>  
  16.   
  17.     <mvc:annotation-driven/>  
  18.     <mvc:resources location="/Component/" mapping="/Component/**"/>  
  19.     <mvc:resources location="/img/" mapping="/img/**"/>  
  20.     <mvc:resources location="/js/" mapping="/js/**"/>  
  21.     <mvc:resources location="/css/" mapping="/css/**"/>  
  22. </beans>  

hello.jsp
[plain]  view plain  copy
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" pageEncoding="utf-8" %>  
  2. <html>  
  3. <head>  
  4.   <script type="text/javascript" src="${pageContext.request.contextPath}/Component/jquery-2.2.2.js"></script>  
  5.   <script type="text/javascript" src="${pageContext.request.contextPath}/js/hello.js"></script>  
  6.     <title>ccc</title>  
  7.     <meta charset="UTF-8"/>  
  8. </head>  
  9. <body>  
  10. <h1>hello</h1>  
  11. <input id="testDownload" type="button" value="测试服务器响应">  
  12. <input id="excelDownload" type="button" value="下载excel">  
  13. <form action="${pageContext.request.contextPath}/TestJson/getJson" method="post">  
  14.   <input name="param" value="就是我"/>  
  15.   <input type="submit" value="提交">  
  16. </form>  
  17. </body>  
  18. </html>  

TestJson.class
[java]  view plain  copy
  1. package com.xueyoucto.xueyou.controller;  
  2.   
  3. import org.springframework.web.bind.annotation.RequestMapping;  
  4. import org.springframework.web.bind.annotation.RequestMethod;  
  5. import org.springframework.web.bind.annotation.RestController;  
  6.   
  7. import javax.servlet.http.HttpServletRequest;  
  8. import java.io.UnsupportedEncodingException;  
  9. import java.util.HashMap;  
  10. import java.util.Map;  
  11.   
  12. /** 
  13.  * Created by Administrator on 2016-09-06. 
  14.  */  
  15. @RestController  
  16. @RequestMapping(value = "/TestJson",method = {RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")  
  17. public class TestJson {  
  18.     @RequestMapping(value = "/getJson")  
  19.     public Map<String,Object> getJson(String param){  
  20.         System.out.println(param);  
  21.         Map<String,Object> resMap = new HashMap<String, Object>();  
  22.         resMap.put("resCode","1");  
  23.         resMap.put("resMessage","成功");  
  24.         return resMap;  
  25.     }  
  26.   
  27.     @RequestMapping(value = "/getJson2")  
  28.     public Map<String,Object> inputCustomer(){  
  29.         Map<String,Object> resMap = new HashMap<String, Object>();  
  30.         resMap.put("ccc"123123);  
  31.         return resMap;  
  32.     }  
  33. }  

运行截图:

点击提交后:

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wild46cat/article/details/52468062

猜你喜欢

转载自blog.csdn.net/zzy7075/article/details/80521403