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 ...
随机推荐
- Jquery Offset, Document, Window 都是什么
From http://www.cnblogs.com/luhe/archive/2012/11/14/2769263.html JQuery Offset实验与应用 我们有时候需要实现这样一种功 ...
- python【第五篇】常用模块学习
一.主要内容 模块介绍 time &datetime模块 random os sys shutil json & pickle shelve xml处理 yaml处理 configpa ...
- Oracle OEM重建
参考博客:http://blog.csdn.net/tianlesoftware/article/details/4702978 测试环境: C:\Users\Administrator>sql ...
- NodeJS+ExpressJS+SocketIO+MongoDB应用模板
OS:Win8.1 with update 关键字:NodeJS,ExpressJS,SocketIO,MongoDB. 1.源代码下载:https://github.com/ldlchina/ESM ...
- Python数据库连接池实例——PooledDB
不用连接池的MySQL连接方法 import MySQLdbconn= MySQLdb.connect(host='localhost',user='root',passwd='pwd',db='my ...
- ubuntu 12.04 安装 nginx+php+mysql web服务器
Nginx 是一个轻量级,以占用系统资源少,运行效率而成为web服务器的后起之秀,国内现在很多大型网站都以使用nginx,包括腾讯.新浪等大型信息网站,还有淘宝网站使用的是nginx二次开发的web服 ...
- C# - ref & out
引用参数和值参数 值参数,是在函数中此变量的任何修改都不影响函数调用中指定的参数,除非把它当作返回值返回,经典例子,交换两个数,但是返回值只有一个. 此时可以用引用参数,函数处理的变量和函数调用中使用 ...
- cocos2dx3.4 保存json文件
头文件: #include "json/document.h" #include "json/stringbuffer.h" #include "js ...
- 对敏捷开发的误解(转自MBAlib)
对敏捷开发的误解 误解一:敏捷对人的要求很高 很多人在尝试实施敏捷时说:敏捷对人的要求太高了,我们没有这样的条件,我们没有这样的人,因此我们没法敏捷.可是,敏捷对人的要求真的那么高么? 软件归根到底还 ...
- Python爬取17吉他网吉他谱
最近学习吉他,一张一张保存吉他谱太麻烦,写个小程序下载吉他谱. 安装 BeautifulSoup,BeautifulSoup是一个解析HTML的库.pip install BeautifulSoup4 ...