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 ...
随机推荐
- nginx+php,502错误
502错误基本就是php进程执行中挂了,其中有个原因就可能是进程执行超时设置导致的比如这个: ; The timeout for serving a single request after whic ...
- MVC 文本转换成html显示
最近在学习ASP.NET MVC,项目中需要将后台传输的HTML文本在前台页面显示:@Html.Raw(HttpUtility.HtmlDecode(ViewBag.DisplayText)).记下来 ...
- jQuery判断文本框是否为空
1.引用jQuery库 <script src="/static/js/jquery_v1.6.1.js" type="text/javascript"& ...
- request 报错The remote server returned an error: (415) Unsupported Media Type.
开发时遇到个问题,程序访问数据库数据,给服务器发送请求时,老是报错,返回的错误页面是: HTTP Status 415 - Unsupported Media Type type Status rep ...
- PHP下编码转换函数mb_convert_encoding与iconv的使用说明
mb_convert_encoding这个函数是用来转换编码的. 不过英文一般不会存在编码问题,只有中文数据才会有这个问题.比如你用Zend Studio或Editplus写程序时,用的是gbk编码, ...
- 那些年被我坑过的Python——牵一发动全身 第十一章MySQL、ORM
#!/usr/bin/env python # -*- coding:utf-8 -*- __Author__ = "Zhang Xuyao" from sqlalchemy im ...
- IIC 概述之1
概述: I²C 是Inter-Integrated Circuit的缩写,发音为"eye-squared cee" or "eye-two-cee" , 它是一 ...
- iOS工程适配64-bit经验分享
终究还是来了.Apple下发了支持64位的最后通牒: As we announced in October, beginning February 1, 2015 new iOS apps submi ...
- linux hadoop 集群安装步骤
http://blog.csdn.net/xjavasunjava/article/details/12013677 1,时间同步hadoop集群的每台机器的时间不能相差太大. 安装集群前最好进行一下 ...
- jquery的笔记
1. 基本选择器 基本的 #id .class element(元素) *(全部元素) $("#id") $(".class") ...