Spring MVC的@ResponseBody返回JSON格式的数据

一 控制器

package org.fkit.controller;

import java.util.ArrayList;
import java.util.List;
import org.fkit.domain.Book;
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+MyBatis企业应用实战","肖文吉"));
        list.add(new Book(2,"轻量级JavaEE企业应用实战","李刚"));
        return list;
    }

}

二 视图

<%@ 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>

三 领域模型

package org.fkit.domain;
import java.io.Serializable;

public class Book implements Serializable {
     private static final long serialVersionUID = 1L;
     
     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;
     }
     @Override
     public String toString() {
           return "Book [id=" + id + ", name=" + name + ",  author=" + author + "]";
     }
}

四 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
     xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd     
        http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">
        
    <!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件,
      如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean  -->
    <context:component-scan base-package="org.fkit.controller"/>
     <!-- 默认配置方案 -->
    <mvc:annotation-driven/>
     <!-- 静态资源处理 -->
    <mvc:default-servlet-handler/>
     
    
    <!-- 视图解析器  p:prefix属性表示前缀  p:suffix 表示后缀  -->
     <bean id="viewResolver"
           class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/content/" p:suffix=".jsp"/>
    
</beans>

五 测试

Spring MVC的@ResponseBody返回JSON格式的数据

上一篇:解决Spring MVC @ResponseBody返回中文字符串乱码问题


下一篇:[【java】]测试接口为什么返回404,将Controller改为RestConntroller问题成功解决!