Spring mvc get和post传值乱码问题
1.url拼值 传单值 对象 list map都是用json的格式传入后台
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<link href="../css/bootstrap.css" rel='stylesheet' type='text/css' />
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="../js/jquery.min.js"></script>
<!-- Custom Theme files -->
<link href="../css/style.css" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<input type="button" onclick="clink1()" value="通过Url?传值">
<input type="button" onclick="clink2()" value="单值传参">
<input type="button" onclick="clink3()" value="对象传参">
<input type="button" onclick="clink4()" value="List传值">
<input type="button" onclick="clink5()" value="map传值">
<script type="text/javascript">
function clink1(){
var url="/ajax/testUrl?dataParam=test";
$.ajax({
type:"post",
url:url,
dataType:'json',
success:function(data){
alert(JSON.stringify(data));
}
});
}
function clink2(){
var url="/ajax/testData";
$.ajax({
type:"post",
data:{dataParam:"test",str:"测试"},
url:url,
dataType:'json',
success:function(data){
alert(JSON.stringify(data));
}
});
}
function clink3(){
var url="/ajax/testObject";
var org={id:1,age:1,name:"zhu",happy:"睡觉"};
$.ajax({
type:"post",
data:org,
url:url,
data:org,
dataType:'json',
success:function(data){
alert(JSON.stringify(data));
}
});
}
function clink4(){
var url="/ajax/testList";
var userList = JSON.stringify([
{id:1,age:1,name:"zhu",hobby:"睡觉"},
{id:1,age:22,name:"zhu2",hobby:"睡觉2"}
]);
$.ajax({
type:"post",
data:userList,
url:url,
contentType:'application/json; charset=UTF-8',
dataType:'json',
success:function(data){
alert(JSON.stringify(data));
}
});
}
function clink5(){
var url="/ajax/testMap";
var userList = JSON.stringify({"姓名":"wanglin","age":"18"});
$.ajax({
type:"post",
data:userList,
url:url,
contentType:'application/json; charset=UTF-8',
dataType:'json',
success:function(data){
alert(JSON.stringify(data));
}
});
}
</script>
</body>
</html>
2.后台代码
package cn.springmvc.controller;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.springmvc.model.User;
import cn.springmvc.util.ActionResult;
import cn.springmvc.util.DataResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
@Controller
@RequestMapping("ajax")
public class AjaxDom {
/**
*
* @Title: test1
* @Description: url传值
* @param @param dataParam
* @param @return
* @return String
* @throws
*/
@RequestMapping(value="/testUrl",method=RequestMethod.POST)
public @ResponseBody String test1(String dataParam){
ActionResult ActionResult=new ActionResult(true,dataParam);
//对象转json
String json = JSON.toJSONString(ActionResult);
return json;
}
/**
* get方式
* @Title: testget
* @Description: TODO
* @param @param dataParam
* @param @return
* @return String
* @throws
*/
@RequestMapping(value="/testUrl")
public @ResponseBody String testget(String dataParam ){
ActionResult ActionResult=new ActionResult(true,dataParam);
//对象转json
String json = JSON.toJSONString(ActionResult);
return json;
}
/**
* 传单值
* @Title: test2
* @Description: TODO
* @param @param dataParam
* @param @return
* @return String
* @throws
*/
@RequestMapping(value="/testData",method=RequestMethod.POST)
public @ResponseBody String test2(String dataParam,String str){
ActionResult ActionResult=new ActionResult(true,str);
//对象转json
String json = JSON.toJSONString(ActionResult);
return json;
}
/**
* 对象传值
* @Title: test3
* @Description: TODO
* @param @param dataParam
* @param @return
* @return String
* @throws
*/
@RequestMapping(value="/testObject",method=RequestMethod.POST)
public @ResponseBody String test3(User user){
List<User> userList=new ArrayList<User>();
userList.add(user);
DataResult dataResult=new DataResult(userList);
//对象转json
String json = JSON.toJSONString(dataResult);
return json;
}
/**
* List传值
* @Title: test4
* @Description: TODO
* @param @param dataParam
* @param @return
* @return String
* @throws
*/
@RequestMapping(value="/testList",method=RequestMethod.POST)
public @ResponseBody String test4(@RequestBody String userList){
List<User> list = JSON.parseArray(userList, User.class);
DataResult dataResult=new DataResult(list);
//对象转json
String json = JSON.toJSONString(dataResult);
return json;
}
/**
* map传值
* @Title: test5
* @Description: TODO
* @param @param dataParam
* @param @return
* @return String
* @throws
*/
@RequestMapping(value="/testMap",method=RequestMethod.POST)
public @ResponseBody String test5(@RequestBody String userMap){
@SuppressWarnings("unchecked")
Map<String,String> map= (Map<String,String>)JSON.parse(userMap);
DataResult dataResult=new DataResult(map);
//对象转json
String json = JSON.toJSONString(dataResult);
return json;
}
}
3.传入后台需要在web.xml配置编码格式
<filter>
<filter-name>SpringEncodingFilter</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>SpringEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4.返回值乱码问题 需要在spring mvc的xml里配置
<!-- 解决json @Respone返回值乱码问题 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" >
<property name="messageConverters">
<list>
<bean class = "org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="java.nio.charset.Charset" />
<property name="targetMethod" value="forName"/>
<property name="arguments" value="UTF-8"/>
</bean>
</constructor-arg>
</bean>
</list>
</property>
5.最后是spring mvc redirect 参数乱码问题
解决在tomcat目录下conf-->server.xml
加上URIEncoding="UTF-8"就行
<Connector connectionTimeout="20000" port="80" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
Spring mvc get和post传值乱码问题的更多相关文章
- 解决Spring MVC @ResponseBody返回中文字符串乱码问题
spring mvc使用的默认处理字符串编码为ISO-8859-1 解决方法: 第一种方法: 对于需要返回字符串的方法添加注解,如下: @RequestMapping(value="/use ...
- spring mvc form表单提交乱码
spring mvc form表单submit直接提交出现乱码.导致乱码一般是服务器端和页面之间编码不一致造成的.根据这一思路可以依次可以有以下方案. 1.jsp页面设置编码 <%@ page ...
- 【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码
概述 spring MVC框架controller间跳转,需重定向,主要有如下三种: 不带参数跳转:形如:http://localhost:8080/SpringMVCTest/test/myRedi ...
- Spring MVC整合fastjson、EasyUI乱码问题
一.框架版本 Spring MVC:spring-webmvc-4.0.0.RELEASE fastjson:fastjson-1.2.45 EasyUI:1.5 二.乱码现象 Controller调 ...
- Spring MVC @ResponseBody返回中文字符串乱码问题
朋友做小项目练手的时候遇到的,着实让他郁闷够呛..这个问题的确很恶心.. 项目中引用的json包,直接用@ResponseBody注解返回json字符串..有关这个的乱码问题网上很多,各种花样各种转码 ...
- Spring MVC+MySQL保存中文变成乱码
环境:MySQL,Spring MVC3.2.0,jQuery v2.0.3,使用JdbcTemplate访问数据库,相当于全套Spring解决方案. 现象 直接使用表单POST,或者使用jQuery ...
- spring mvc 解决后台传递值乱码问题
在Web-xml 配置添加过滤器 <!-- 配置过滤器 解决乱码问题 --> <filter> <filter-name>CharacterEncodingFilt ...
- spring mvc ModelAndView向前台传值
今天在做项目的时候遇到一个问题,把第一个页面保存的id传到第三个页面中去用,原来是在controller层加了一个全局变量控制的,但是后来发现这个变量实现不了我要的功能,于是查了一下,原来ModelA ...
- 解决Spring MVC前台传参中文乱码问题
在web.xml文件中配置字符编码过滤器: <filter> <filter-name>CharacterEncoding</filter-name> <fi ...
随机推荐
- Spring 官方下载地址(非Maven)
现在spring的官网停止了使用zip包下载,只能使用maven,非常的不方便,分享如下网址可以使用zip包下载,是不是方便多了!~ 下载列表如下: spring-framework-3.2.8.RE ...
- laravel的多态关联--morphTo和morphMany
首先,文档里面讲述的不是特别详细,详细寻找查询流程没有过多介绍,只是介绍如何去定义,直接使用,导致很多该明白的东西,没有说明,下面详细看看这个多态关联 是怎么定义,使用,详细查询的. 先看文档介绍 多 ...
- php练习1——计算器
目标:输入两个数,计算两个数的和/差/积/商 程序如下:两个文件jiSuanQi.html和jiSuanQi.php 结果如下:
- 学习hash_map从而了解如何写stl里面的hash函数和equal或者compare函数
---恢复内容开始--- 看到同事用unordered_map了所以找个帖子学习学习 http://blog.sina.com.cn/s/blog_4c98b9600100audq.html (一)为 ...
- 开发设计模式(八)抽象工厂模式(Abstract Factory Pattern)
抽象工厂模式主要有以下角色: 抽象工厂角色:担任这个角色的是工厂方法模式的核心,它是与应用系统的商业逻辑无关的.通常使用接口或抽象类实现. 具体工厂角色:这个角色直接在客户端的调用下创建产品的实 ...
- codevs 1540 银河英雄传说
题目描述 Description 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米 ...
- Matlab使用心得
1..*和*的区别 .*只能用于两个同型矩阵相乘,且是相对应的元素做乘法运算,其运算规则和我们线性代数里的乘法规则是不一样的:而*用于两个矩阵相乘,如mxn,nxk两个矩阵相乘,它的运算规则和线性代数 ...
- Eclipse导入git上的maven web项目 部署
1 Eclipse中导入Git的maven项目 方法1: (1)首先当然是拉代码. 在Eclipse里面有个Git Repositories Exploring.就是Git仓库,clone a git ...
- HDU 5015 233 Matrix
题意:给定一个矩阵的第0列的第1到n个数,第一行第1个数开始每个数分别为233, 2333........,求第n行的第m个数. 分析: 其实也没那么难,自己想了半天还没往对的方向想,m最大1e9,应 ...
- 还是编码 汉字(GB2312和GBK)的ASCII码对照表
GB2312和GBK每一个汉字由2个字节组成,这2个字节的ASCII码大小分别是:gb2312: high8 = 0xa1-->0xfe (161 - 254)low8 = 0xa1--> ...