页面ajax请求传参及java后端数据接收
js ajax请求传参及java后端数据接收
Controller:
package com.ysl.PassingParameters.controller;
import java.util.List;
import java.util.Map; 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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import com.ysl.PassingParameters.bean.User;
import com.ysl.PassingParameters.dto.RetMsg; @Controller
public class TestController { /**
* List<String>传参
* @param listString
* @return
*/
@RequestMapping("/listString")
@ResponseBody
public RetMsg listString(@RequestParam("listString[]") List<String> listString){
System.out.println("listString:"+listString.toString());
return RetMsg.success();
} /**
* List<User>传参
* @param listUser
* @return
*/
@RequestMapping("/listUsers")
@ResponseBody
public RetMsg listUsers(@RequestBody List<User> listUser){
System.out.println("username:"+listUser.get(0).getUsername());
return RetMsg.success();
} /**
* User[]传参
* @param arrayUsers
* @return
*/
@RequestMapping("/arrayUsers")
@ResponseBody
public RetMsg arrayUsers(@RequestBody User[] arrayUsers){
System.out.println("username:"+arrayUsers[0].getUsername());
return RetMsg.success();
} /**
* List<Map<String,Object>>传参
* @param listMap
* @return
*/
@RequestMapping("/listMap")
@ResponseBody
public RetMsg listMap(@RequestBody List<Map<String, String>> listMap){
System.out.println("username:"+listMap.get(0).get("username"));
return RetMsg.success();
} /**
* User对象传参
* @param arrayUsers
* @return
*/
@RequestMapping("/users")
@ResponseBody
public RetMsg users(@RequestBody User users){
System.out.println("username:"+users.getUsername());
System.out.println("username:"+users.getList().get(0).getUsername());
return RetMsg.success();
}
}
页面:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>
<%application.setAttribute("path", request.getContextPath());%>
<head>
<script type="text/javascript" src="${path}/js/jquery.min.js"></script>
</head>
<body>
<h2>Hello World!</h2>
<button id="listString">List<String>传参</button>
<button id="listUser">List<User>传参</button>
<button id="arrayUsers">User[]传参</button>
<button id="listMap">List<Map<String,Object>>传参</button>
<button id="Users">User对象(属性包含List<User>)传参</button>
<script type="text/javascript"> // List<String>传参
$("#listString").click(function(){
var idList = new Array();
idList.push("1");
idList.push("1");
idList.push("1");
$.ajax({
type:"post",
url:"${path}/listString",
data:{"listString":idList},
dataType:"json",
success:function(retMsg){
if(retMsg.code==200){
alert("success");
}else{
alert("false");
}
}
})
}) // List<User>传参
$("#listUser").click(function(){
var userList = new Array();
userList.push({username: "zhangsan",password: "332"});
userList.push({username: "zhangsan",password: "332"});
$.ajax({
type:"post",
url:"${path}/listUsers",
data:JSON.stringify(userList),
dataType:"json",
contentType : 'application/json;charset=utf-8', //设置请求头信息
success:function(retMsg){
if(retMsg.code==200){
alert("success");
}else{
alert("false");
}
}
})
}) //传User对象数组
$("#arrayUsers").click(function(){
var userList = [{username: "李四",password: "123"},{username: "张三",password: "332"}];
$.ajax({
type: "POST",
url: "${path}/arrayUsers",
data: JSON.stringify(userList),//将对象序列化成JSON字符串
dataType:"json",
contentType : 'application/json;charset=utf-8', //设置请求头信息
success:function(retMsg){
if(retMsg.code==200){
alert("success");
}else{
alert("false");
}
}
});
}) // List<Map<String,Object>>传参
$("#listMap").click(function(){
var userList = new Array();
userList.push({username: "zhangsan",password: "332"});
userList.push({username: "zhangsan",password: "332"});
$.ajax({
type:"post",
url:"${path}/listMap",
data:JSON.stringify(userList),
dataType:"json",
contentType : 'application/json;charset=utf-8', //设置请求头信息
success:function(retMsg){
if(retMsg.code==200){
alert("success");
}else{
alert("false");
}
}
})
}) //User对象传参
$("#Users").click(function(){
var list = new Array();
list.push({username: "zhangsan",password: "332"});
list.push({username: "zhangsan",password: "332"});
var user = {};
user.username = "张三";
user.password = "密码";
user.list = list;
$.ajax({
type:"post",
url:"users",
data:JSON.stringify(user),
datatype:"json",
contentType:"application/json;charset=utf-8",
success:function(retMsg){
if(retMsg.code==200){
alert("success");
}else{
alert("false");
}
}
})
})
</script>
</body>
</html>
@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);
GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交。
https://blog.csdn.net/justry_deng/article/details/80972817
页面ajax请求传参及java后端数据接收的更多相关文章
- 前端集合传参,springmvc后端如何接收
废话不多说,上代码 后端接收对象: class ObjectA{ private String a; private String b; private List<ObjectB> lis ...
- 小程序页面跳转传参-this和that的区别-登录流程-下拉菜单-实现画布自适应各种手机尺寸
小程序页面跳转传参 根目录下的 app.json 文件 页面文件的路径.窗口表现.设置网络超时时间.设置多 tab { "pages": [ "pages/index/i ...
- [转]ASP.NET MVC学习系列(二)-WebAPI请求 传参
[转]ASP.NET MVC学习系列(二)-WebAPI请求 传参 本文转自:http://www.cnblogs.com/babycool/p/3922738.html ASP.NET MVC学习系 ...
- ajax请求传过来的json数据直接转成对应的实体类时出错:400 Bad Request 不进入controller
今天开发过程中,在SpringMVC中的Action中处理前台ajax请求传过来的json数据直接转成对应的实体类时出错:400 Bad Request,后台也不报错,400指的的是请求无效(请求有语 ...
- 13.scrapy框架的日志等级和请求传参
今日概要 日志等级 请求传参 如何提高scrapy的爬取效率 今日详情 一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是s ...
- scrapy框架的日志等级和请求传参
日志等级 请求传参 如何提高scrapy的爬取效率 一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息 ...
- scrapy框架之日志等级和请求传参-cookie-代理
一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息. - 日志信息的种类: ERROR : 一般错误 ...
- 爬虫--Scrapy-参数等级和请求传参
日志等级 日志等级(种类): ERROR:错误 WARNING:警告 INFO:一般信息 DEBUG:调试信息(默认) 指定输入某一中日志信息: settings:LOG_LEVEL = ‘ERROR ...
- Scrapy的日志等级和请求传参
日志等级 日志信息: 使用命令:scrapy crawl 爬虫文件 运行程序时,在终端输出的就是日志信息: 日志信息的种类: ERROR:一般错误: WARNING:警告: INFO:一般的信息: ...
随机推荐
- 奇妙的算法【9】YC每个小孩的糖果数,找公约数,最少硬币数
1,每个小孩的糖果数量是多少 有p个小孩,c个糖果,刚开始第1个小孩发一个糖果,第2个小孩发两个糖果,第p个小孩发p个糖果,如果糖果没有发完,就接着[注意]第1个小孩发p+1个糖果.....第p个小孩 ...
- 为什么领域模型对于架构师如此重要? https://blog.csdn.net/qq_40741855/article/details/84835212
为什么领域模型对于架构师如此重要? https://blog.csdn.net/qq_40741855/article/details/84835212 2018年12月05日 14:30:19 绝圣 ...
- Uploadify 之使用
uploadify 3.2.1是 jQuery提供的一个上传插件,其参数详解见 http://www.cnblogs.com/yangy608/p/3915349.html 这里列举一个实际应用的例子 ...
- C 预编译 宏 声明
- idou老师教你学Istio11 : 如何用Istio实现流量熔断
在之前的最佳实践中,已经带大家通过一系列的实践任务领略了Istio的无穷魅力.今天,将向大家介绍如何用Istio实现流量熔断. 熔断机制是创建弹性微服务应用程序的重要模式.熔断可以帮助您自由控制故障影 ...
- pandas库介绍之DataFrame基本操作
怎样删除list中空字符? 最简单的方法:new_list = [ x for x in li if x != '' ] 今天是5.1号. 这一部分主要学习pandas中基于前面两种数据结构的基本操作 ...
- 【产品对比】Word开发工具Aspose.Words和Spire.Doc性能和优劣对比一览
转:evget.com/article/2018/4/3/27885.html 概述:Microsoft Office Word是微软公司的一个文字处理器应用程序,作为办公软件必不可少的神器之一,Wo ...
- 0005SpringBoot中用Junit测试实体类中绑定yml中的值
1.编写SpringBoot的引导类 package springboot_test.springboot_test; import org.springframework.boot.SpringAp ...
- Springboot项目启动报org.springframework.beans.factory.UnsatisfiedDependencyException
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'hom ...
- [hdoj4578][多延迟标记的线段树]
Transformation Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 65535/65536 K (Java/Others)T ...