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. JAVA 修改 JSESSIONID

    @Action("sidTest") public void sidTest() { HttpSession session = request.getSession(); Str ...

  2. VC++添加工具栏

    VC添加工具栏 方法一:添加ICON图标 1. 新建一个基于对话框的项目:Test 2. 在类CTestDlg中, 添加两个变量: CToolBarCtrl m_ToolBar1; CImageLis ...

  3. python基础学习(二)--函数

    return返回值: python函数都有返回值,函数体内无return,默认返回值None, 函数参数: 1.普通参数 严格按照顺序,将实际参数赋值给形式参数,一一对应. 例: def send(x ...

  4. leetcode解题—Longest Palindromic Substring

    题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  5. hdu 4358 Boring counting 离散化+dfs序+莫队算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意:以1为根节点含有N(N <= 1e5)个结点的树,每个节点有一个权值(weight ...

  6. 【Catalina】

    Tomcat's servlet container was redesigned as Catalina in Tomcat version 4.x. The architect for Catal ...

  7. Unity3d Shader开发(四)UsePass ,GrabPass ,SubShader Tags

    (一)UsePass 命令 使用 来自另一个着色器的命名通道. Syntax 语法 UsePass "Shader/Name" 插入所有来自给定着色器中的给定名字的通道.Shade ...

  8. CODEVS 3000公路修建问题

    题目描述 Description OI island是一个非常漂亮的岛屿,自开发以来,到这儿来旅游的人很多.然而,由于该岛屿刚刚开发不久,所以那里的交通情况还是很糟糕.所以,OIER Associat ...

  9. 如何学习C++[转]

    关于学C++, 我向你推荐一些书(当然能够结合课内项目实践更好) 1.The C++ Programming Language(Bjarne Stroustrup)2. Inside The C++ ...

  10. 静态页面参数传递&回调函数写法&快速排序的实现方法

    相信很多人都有一种陋习,就是收藏的文章,几乎从来都没有回过头来仔细看过.这次借着这次活动的机会,在<无懈可击的web设计>一书的学习过程中,穿插着讲自己曾经收藏过的,现在觉得还有价值的文章 ...