Spring MVC返回JSON格式的数据
来源:互联网 发布:淘宝的海外直邮靠谱吗 编辑:程序博客网 时间:2024/04/29 06:24
1.前端页面
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>测试返回JSON格式的数据</title><script type="text/javascript" src="js/jquery-1.11.0.min.js"></script><script type="text/javascript" src="js/json2.js"></script><script type="text/javascript">$(document).ready(function(){testResponseBody();});function testResponseBody(){$.post("${pageContext.request.contextPath}/json/testRequestBody",null,function(data){ $.each(data,function(){var tr = $("<tr align='center'/>"); $("<td/>").html(this.id).appendTo(tr); $("<td/>").html(this.name).appendTo(tr); $("<td/>").html(this.author).appendTo(tr); $("#booktable").append(tr); })},"json");}</script></head><body><table id="booktable" border="1" style="border-collapse: collapse;"><tr align="center"> <th>编号</th> <th>书名</th> <th>作者</th></tr></table></body></html>
2.JavaBean
Book.java
package com.bean;import java.io.Serializable;public class Book implements Serializable {private Integer id;private String name;private String author;public Book() {super();// TODO Auto-generated constructor stub}public Book(Integer id, String name, String author) {super();this.id = id;this.name = name;this.author = author;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}@Overridepublic String toString() {return "Book [id=" + id + ", name=" + name + ", author=" + author + "]";}}
3.控制类
BookController.java
package com.control;import java.util.ArrayList;import java.util.List;import com.bean.*;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controller@RequestMapping("/json")public class BookController { @RequestMapping(value="/testRequestBody") // @ResponseBody会将集合数据转换json格式返回客户端 @ResponseBody public Object getJson() { List<Book> list = new ArrayList<Book>(); list.add(new Book(1,"Spring MVC企业应用实战","肖文吉")); list.add(new Book(2,"轻量级JavaEE企业应用实战","李刚")); return list; }}
4.截图
0 0
- Spring MVC返回JSON格式的数据
- Spring MVC返回JSON格式的数据
- 封装Spring MVC 返回JSON 格式数据
- 使用spring MVC返回json格式的数据
- spring mvc 返回json格式数据的方式
- spring mvc 对返回json格式数据的支持
- Spring mvc 返回json格式
- Spring mvc 返回json格式
- Spring mvc 返回json格式
- Spring mvc 返回json格式
- Spring MVC返回json格式
- Spring mvc 返回json格式
- Spring mvc 返回json格式
- Spring mvc 返回json格式
- spring mvc 返回通过配置ResponseBody返回json格式的数据
- Spring MVC返回XML格式的数据
- Spring MVC返回XML格式的数据
- Spring MVC返回json数据的方法
- IDEA中配置tomcat
- 【luogu2709】小B的询问(莫队)
- 数据库热词提取方式
- 从两个字符串中找到最大字符子串
- Unity3D手游开发实践
- Spring MVC返回JSON格式的数据
- scp命令
- CentOS7 minimal +anaconda+keras 深度学习环境
- github绑定域名
- spring路径通配符
- lua io标准库
- [C++]sleep 和 usleep区别
- 利用倍增算法的后缀数组
- firewall pkgName:com.example.sevenlaps.networktest, result:0x0