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();
}
}

Spring MVC Json数据传递的更多相关文章

  1. Spring MVC 前后台数据交互

    本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址地址:<Spring MVC 前后台数据交互> 1.服务端数据到客户端 (1)返回页面,Controller中方法 ...

  2. spring mvc json 返回乱码问题解决(vestion:3.x.x)

    本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:<spring mvc json 返回乱码问题解决(vestion:3.x.x)> 工程中用springmvc返 ...

  3. 0060 Spring MVC的数据类型转换--ConversionService--局部PropertyEditor--全局WebBindingInitializer

    浏览器向服务器提交的数据,多是字符串形式,而有些时候,浏览器需要Date.Integer等类型的数据,这时候就需要数据类型的转换器 使用Spring的ConversionService及转换器接口 下 ...

  4. 0061 Spring MVC的数据格式化--Formatter--FormatterRegistrar--@DateTimeFormat--@NumberFormat

    Converter只完成了数据类型的转换,却不负责输入输出数据的格式化工作,日期时间.货币等虽都以字符串形式存在,却有不同的格式. Spring格式化框架要解决的问题是:从格式化的数据中获取真正的数据 ...

  5. spring mvc: json练习

    spring mvc: json练习 本例需要用到的json包: 如下: jackson-databind jackson-core jackson-annotations <!-- https ...

  6. Spring MVC JSON自己定义类型转换(续)

    前面提到了两种转换类型的方法(Spring MVC JSON自己定义类型转换),这里针对Json转换提供一种更简便的方法. 通过配置全局的日期转换来避免使用麻烦的注解. 首先用到了一个简单的日期工具类 ...

  7. [Spring MVC] - JSON

    Spring MVC中使用JSON,先必需引用两个包:jackson-core-asl-1.9.13.jar.jackson-mapper-asl-1.9.13.jar 因为需要使用到jquery测试 ...

  8. spring Mvc json返回json的日期格式问题

    (一)输出json数据 springmvc中使用jackson-mapper-asl即可进行json输出,在配置上有几点: 1.使用mvc:annotation-driven 2.在依赖管理中添加ja ...

  9. Spring MVC JSON 实现JsonSerializer Date类型转换

    转载至:http://blog.csdn.net/lantianzhange/article/details/40920933 在Spring MVC中存在两大类的类型转换,一类是Json,一个是Sp ...

随机推荐

  1. redhat 安装GCC-4.8.3

    1.下载gcc-4.8.3安装包 gcc各版本浏览地址:http://ftp.gnu.org/gnu/gcc/ yum install gccyum install gcc-c++ 2.将gcc-4. ...

  2. OpenGL红宝书例子2.2 uniform变量的使用

    1. 简单介绍一下OpenGL可编程渲染管线的流程 顶点着色 --> 细分着色 --> 几何着色 --> 片元着色 --> 计算着色 一般我们主要参与的阶段是顶点着色和片元着色 ...

  3. JPA 系列教程3-单向多对一

    JPA中的@ManyToOne 主要属性 - name(必需): 设定"many"方所包含的"one"方所对应的持久化类的属性名 - column(可选): 设 ...

  4. gSoap工具wsdl2h及soapcpp2指令汇总

    gSoap开发包的下载地址http://sourceforge.net/projects/gsoap2,在bin目录下提供了两个工具: 1:wsdl2h:The gSOAP wsdl2h tool i ...

  5. 对比React Native、dcloud、LuaView三个框架技术(内部)

    转载自:http://www.jianshu.com/p/ee1cdb33db8d主要对比React Native和5+SDK(就是dcloud的SDK)两个: 开发语言:三个都是用其他语言来统一开发 ...

  6. 深入解析FileInputStream和FileOutputStream

    http://swiftlet.net/archives/1363 FileInputStream和FileOutputStream类属于字节类,可以操作任意类型的文件.在数据流的处理过程中,有两种情 ...

  7. SAX,DOM,JAXP,JDOM,DOM4J比较

    dom,sax,jdom,dom4j的技术特点: 1: DOMDOM 是用与平台和语言无关的方式表示 XML 文档的官方 W3C 标准.DOM 是以层次结构组织的节点或信息片断的集合.这个层次结构允许 ...

  8. AndroidStudio使用注意事项

    今天在引入GitHUb上的开源框架时,写好依赖后编译时,报以下错误: Error:Execution failed for task ':app:processDebugResources'.> ...

  9. Using Sphinx to index CNS database

    1, look at the sphinx.person.address.conf to see how to configure the conf file2, index the database ...

  10. JSP标准标签库(JSTL)--国际化标签库 fmt

    JSTL中使用fmt.tld作为格式化标签库的定义文件 No. 功能分类 标签名称 描述 1 国际化标签 <fmt:setLocale> 设置一个全局的地区代码 2 <fmt:req ...