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"/>