SpringMVC之json数据传递
json是一种常见的传递格式,是一种键值对应的格式。并且数据大小会比较小,方便传递。所以在开发中经常会用到json。
首先看一下json的格式:
{key1:value1,key2:value2}
每一个建对应一个值,每个键值对之间用逗号连接。并且最后一个键值对之后没有逗号,整体需要有大括号括起来。
SpringMVC的前台获取json代码:
annotationTest.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'annotationTest.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="/Spring6Annotation3/js/jquery-1.7.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){
$("#submit").click(
function(){
var username = $("#username").attr("value");
var userage = $("#userage").attr("value");
var userJson={name:username,age:userage};//构造json数据
$.ajax({
url:"/Spring6Annotation3/user/data/showInfoJson",
type:"post",
data:userJson,
success: function(data){
alert("name===>"+data.username+"age"+data.userage);
}//由此我们可以看出,jquary中的ajax也是json格式的
});
});
});
</script>
<body>
<h1>json</h1>
<br/>
<form action="">
姓名:<input type="text" id="username" name="name"/>
年龄:<input type="text" id="userage" name="age"/>
<input type="button" id="submit" value="提交"/>
</form>
</body>
</html>
在controller中
@RequestMapping("/showInfoJson")
public void showInfoJson(User user,HttpServletRequest request,HttpServletResponse response){
String result = "{\"username\":\""+user.getName()+"\",\"userage\":\""+user.getAge()+"\"}";//user接到前台传到的数据,并拼接成新的json对象
response.setContentType("application/json");//设置response的传输格式为json
System.out.println(result);
try {
PrintWriter out = response.getWriter();
out.write(result)//给页面上传输json对象
} catch (IOException e) {
e.printStackTrace();
}
}
SpringMVC之json数据传递的更多相关文章
- 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- SpringMVC(三)-- 视图和视图解析器、数据格式化标签、数据类型转换、SpringMVC处理JSON数据、文件上传
1.视图和视图解析器 请求处理方法执行完成后,最终返回一个 ModelAndView 对象 对于那些返回 String,View 或 ModeMap 等类型的处理方法,SpringMVC 也会在内部将 ...
- 【Spring学习笔记-MVC-4】SpringMVC返回Json数据-方式2
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- 【Spring学习笔记-MVC-3】SpringMVC返回Json数据-方式1
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- SpringMVC返回JSON数据时日期格式化问题
https://dannywei.iteye.com/blog/2022929 SpringMVC返回JSON数据时日期格式化问题 博客分类: Spring 在运用SpringMVC框架开发时,可 ...
- Spring MVC Json数据传递
json是一种常见的传递格式,是一种键值对应的格式.并且数据大小会比较小,方便传递.所以在开发中经常会用到json. 首先看一下json的格式: {key1:value1,key2:value2} 每 ...
- SpringMVC与Json数据交互
简单回顾了一下SpringMVC和前端JSON数据是怎么交互的 接下来详细说一下过程 前端代码写的很简单 主要是为了试验一下JSON数据的前后台传递 前端代码给大家发出来 其实真的很简单 前端接受了 ...
- springmvc 返回json数据给前台jsp页面展示
spring mvc返回json字符串的方式 方案一:使用@ResponseBody 注解返回响应体 直接将返回值序列化json 优点:不需要自己再处理 步骤一:在spring- ...
- SpringMVC的JSON数据交互(七)-@Response,@RestController,@RequestBody用法
1.@RequestBody (自动将请求的数据封装为对象) 作用: @RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConve ...
随机推荐
- 大规模字符串检索-压缩trie树
本文使用压缩trie树实现字符串检索的功能.首先将字符串通过编码转化为二进制串,随后将二进制串插入到trie树中,在插入过程中同时实现压缩的功能. 字符编码采用Huffman,但最终测试发现不采用Hu ...
- apple iphone 3gs 有锁机 刷机 越狱 解锁 全教程(报错3194,3014,1600,短信发不出去等问题可参考)
以自身经历列步骤如下:(基本思路就是刷6.1.6,越狱,降级基带,解锁) 一.准备工作 1.下载3gs 6.1.6官方固件.地址:http://act.feng.com/wetools/index.p ...
- Java学习----创建对象的数组
1.初始化数组的长度 2.初始化每个元素对象 3.调用每个对象的方法 public class Student { private String name; public Student() {} p ...
- Keil的使用-1创建项目和工程
下载keil,注意不要使用MDK版本(主要是arm开发使用),大小约54M 安装过程不再详述 安装Keil成功并运行后,新建项目, 创建新项目,然后弹出下图,选择对应的单片机芯片(双击) ...
- jquery cleditor 光标经常点不进去问题解决方法 bootstrap 富文本框 控件
cleditor 光标点不进去,原因是内嵌的html代码段 body没有赋值默认高度. 解决方法1.赋值options.bodyStyle 设置min-height值.缺点:不能跟随设备更新最低高度 ...
- bzoj2597: [Wc2007]剪刀石头布
Description 在一些一对一游戏的比赛(如下棋.乒乓球和羽毛球的单打)中,我们经常会遇到A胜过B,B胜过C而C又胜过A的有趣情况,不妨形象的称之为剪刀石头布情况.有的时候,无聊的人们会津津乐道 ...
- 转:简单介绍 P3P 技术
原文来自于:http://blog.csdn.net/ghj1976/article/details/4889219 以 Internet Explorer 为例,默认情况下,IE的隐私策略如下图所设 ...
- Facebook IV Winner's Interview: 1st place, Peter Best (aka fakeplastictrees)
Facebook IV Winner's Interview: 1st place, Peter Best (aka fakeplastictrees) Peter Best (aka fakepla ...
- [转]Uploading and Downloading VHDs to Windows Azure
The article shows how to download and upload VHD to Azure. http://michaelwasham.com/windows-azure-po ...
- struts配置时遇到的几个问题
1. struts在配置文件的时候,如果package包继承为 :extends="json-default" ,那么项目中要引入struts2-json-plugin-xxx.j ...