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传值乱码问题的更多相关文章

  1. 解决Spring MVC @ResponseBody返回中文字符串乱码问题

    spring mvc使用的默认处理字符串编码为ISO-8859-1 解决方法: 第一种方法: 对于需要返回字符串的方法添加注解,如下: @RequestMapping(value="/use ...

  2. spring mvc form表单提交乱码

    spring mvc form表单submit直接提交出现乱码.导致乱码一般是服务器端和页面之间编码不一致造成的.根据这一思路可以依次可以有以下方案. 1.jsp页面设置编码 <%@ page ...

  3. 【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码

    概述 spring MVC框架controller间跳转,需重定向,主要有如下三种: 不带参数跳转:形如:http://localhost:8080/SpringMVCTest/test/myRedi ...

  4. Spring MVC整合fastjson、EasyUI乱码问题

    一.框架版本 Spring MVC:spring-webmvc-4.0.0.RELEASE fastjson:fastjson-1.2.45 EasyUI:1.5 二.乱码现象 Controller调 ...

  5. Spring MVC @ResponseBody返回中文字符串乱码问题

    朋友做小项目练手的时候遇到的,着实让他郁闷够呛..这个问题的确很恶心.. 项目中引用的json包,直接用@ResponseBody注解返回json字符串..有关这个的乱码问题网上很多,各种花样各种转码 ...

  6. Spring MVC+MySQL保存中文变成乱码

    环境:MySQL,Spring MVC3.2.0,jQuery v2.0.3,使用JdbcTemplate访问数据库,相当于全套Spring解决方案. 现象 直接使用表单POST,或者使用jQuery ...

  7. spring mvc 解决后台传递值乱码问题

    在Web-xml 配置添加过滤器 <!-- 配置过滤器 解决乱码问题 --> <filter> <filter-name>CharacterEncodingFilt ...

  8. spring mvc ModelAndView向前台传值

    今天在做项目的时候遇到一个问题,把第一个页面保存的id传到第三个页面中去用,原来是在controller层加了一个全局变量控制的,但是后来发现这个变量实现不了我要的功能,于是查了一下,原来ModelA ...

  9. 解决Spring MVC前台传参中文乱码问题

    在web.xml文件中配置字符编码过滤器: <filter> <filter-name>CharacterEncoding</filter-name> <fi ...

随机推荐

  1. SQL Server 2012 读写分离设置

    SQL Server 2012 读写分离设置 - AlsoIn 时间 2014-07-21 17:38:00  博客园-所有随笔区 原文  http://www.cnblogs.com/also/p/ ...

  2. CentOS7 IP自动获取

    /etc/sysconfig/network-scripts HWADDR=00:15:5D:00:76:04TYPE=EthernetBOOTPROTO=dhcpDEFROUTE=yesPEERDN ...

  3. WPF MVVM 用户控件完成分页

    项目中经常会有分页查询的情况,在WPF中我们可以通过用户控件完成分页 一下为分页控件的页面代码, <UserControl x:Class="Foundation.UCtrl.Next ...

  4. js简单实现删除记录时的提示效果

    删除记录时的提示效果,挺人性化的,实现的方法有很多,在本文为大家介绍下使用js是如何实现的 样式 复制代码代码如下: <style type="text/css">  ...

  5. 在Linux系详解Linux bash中的变量

    (大讲台:国内首个it在线教育混合式自适应学习) 统中进行日常运维或者是编写脚本时,变量是再熟悉不过的了,但这些变量都有哪些类型,具体的用法又有哪些差异呢?本文整理分享给大家: 一.bash变量类型: ...

  6. 拖尾渲染器 Trail Renderer

    拖尾渲染器(Trail Renderer)用于制作跟在场景中的物体后面的拖尾效果来代表它们在到处移动. 必须给Materials一个材质渲染器设置的Colors才有效. 展示自己的一个demo...

  7. 服务器环境搭建系列(四)-mysql篇

    1.按照上一篇服务器环境搭建系列(三)-JDK篇中的方法检查系统是否已经预装Mysql并卸载. 2.下载mysql,这里是MySQL-server-5.5.25-1.linux2.6.x86_64.r ...

  8. BZOJ 3969 Low Power 解题报告

    我们首先将所有电池排序,那么我们可以找到一组最优方案,使得一台机器的能量之差是相邻两电池的能量之差. 然后我们就二分这个答案,从前往后贪心地选这个数对,然后看是否所有的数对都是满足条件的. 假设这个数 ...

  9. PHP漏洞全解(二)-命令注入攻击

    本文主要介绍针对PHP网站常见的攻击方式中的命令攻击.Command Injection,即命令注入攻击,是指这样一种攻击手段,黑客通过把HTML代码输入一个输入机制(例如缺乏有效验证限制的表格域)来 ...

  10. js amd

    http://www.ruanyifeng.com/blog/2012/10/asynchronous_module_definition.html http://www.adequatelygood ...