springmvc post方式提交form时乱码问题——filter方式解决
最近遇到了一个奇怪的问题,就是编码问题。采用springmvc的时候,正常的get方式和ajax方式提交的时候,后台接收的数据都没有问题,但是采用form的post方式提交表单的时候出现了问题。发现后台接收不到数据。
在网上查了好久,各种方式试了个遍,最后还是添加filter的方式最奏效。下面是代码配置:
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<filter>
<filter-name>encodingFilter</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>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.xueyoucto.xueyou.controller"/>
<context:component-scan base-package="com.xueyoucto.xueyou.utils"/>
<mvc:annotation-driven/>
<mvc:resources location="/Component/" mapping="/Component/**"/>
<mvc:resources location="/img/" mapping="/img/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/css/" mapping="/css/**"/>
</beans>
hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" pageEncoding="utf-8" %>
<html>
<head>
<script type="text/javascript" src="${pageContext.request.contextPath}/Component/jquery-2.2.2.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/hello.js"></script>
<title>ccc</title>
<meta charset="UTF-8"/>
</head>
<body>
<h1>hello</h1>
<input id="testDownload" type="button" value="测试服务器响应">
<input id="excelDownload" type="button" value="下载excel">
<form action="${pageContext.request.contextPath}/TestJson/getJson" method="post">
<input name="param" value="就是我"/>
<input type="submit" value="提交">
</form>
</body>
</html>
TestJson.class
package com.xueyoucto.xueyou.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Administrator on 2016-09-06.
*/
@RestController
@RequestMapping(value = "/TestJson",method = {RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")
public class TestJson {
@RequestMapping(value = "/getJson")
public Map<String,Object> getJson(String param){
System.out.println(param);
Map<String,Object> resMap = new HashMap<String, Object>();
resMap.put("resCode","1");
resMap.put("resMessage","成功");
return resMap;
}
@RequestMapping(value = "/getJson2")
public Map<String,Object> inputCustomer(){
Map<String,Object> resMap = new HashMap<String, Object>();
resMap.put("ccc", 123123);
return resMap;
}
}
运行截图:
点击提交后: