CRUD(17):查询-返回分页的json数据

一、改造查询

①index.jsp页面直接发送ajax请求进行员工分页数据的查询;

②服务器将查出的数据,以json字符串的形式返回给浏览器;

③浏览器收到js字符串,可以使用js对json进行解析,使用js通过dom增删改改变页面;

④返回json,实现客户端的无关性。

二、导入json包

在pom.xml中添加:


             <!-- 

https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind 

-->



             <dependency>



                    <groupId>com.fasterxml.jackson.core</groupId>



                    <artifactId>jackson-databind</artifactId>



                    <version>2.8.8</version>



             </dependency>



这个我还是在之前就导入过了哈。

三、重新写Controller


       //需要导入json包



       @RequestMapping("/emps")



       @ResponseBody



       public PageInfo 

getEmpsWithJson(@RequestParam(value="pn",defaultValue="1") Integer pn,Model 

model) {



             //引入pageHelper分页插件



             //传入页码和每页的大小



             PageHelper.startPage(pn, 5);



             //后面紧跟的查询就是分页查询



             List<Employee> emps = employeeService.getAll();



             //使用PageInfo包装查询后的结果,只需要将PageInfo交给页面



             //封装了详细的分页信息,包括我们查询出来的数据



             //连续传入5页



             PageInfo page = new PageInfo(emps,5);



             return page;



             



       }



四、然后,再打开emps页面,我们的浏览器就收到了一个高贵的json文件。

cfa2ebe7074fa4605d5108874fe427ea.png

五、设计一个通用的返回类

在com.amel.crud.bean下新建Msg.java。


package com.amel.crud.bean;







import java.util.HashMap;



import java.util.Map;







import com.github.pagehelper.PageInfo;







/**



 * json数据通用的返回类



 *



 */



public class Msg {



       



       //状态码,返回100代表服务器处理成功;200则处理失败



       private int code;



       //提示信息



       private String msg;



       



       //用户要返回给浏览器的数据



       private Map<String,Object> extend = new HashMap<String, Object>();



       



       //处理成功



       public static Msg success() {



             Msg result = new Msg();



             result.setCode(100);



             result.setMsg("处理成功!");



             return result;



       }







       //处理失败



       public static Msg fail() {



             Msg result = new Msg();



             result.setCode(200);



             result.setMsg("处理失败!");



             return result;



       }



       



       



       public int getCode() {



             return code;



       }







       public void setCode(int code) {



             this.code = code;



       }







       public String getMsg() {



             return msg;



       }







       public void setMsg(String msg) {



             this.msg = msg;



       }







       public Map<String, Object> getExtend() {



             return extend;



       }







       public void setExtend(Map<String, Object> extend) {



             this.extend = extend;



       }



       



       public Msg add(String key, Object value) {



             this.getExtend().put(key,value);



             return this;



       }







}







六、改写Controller


package com.amel.crud.controller;



import java.util.List;



import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody;



import com.amel.crud.bean.Employee;

import com.amel.crud.bean.Msg;

import com.amel.crud.service.EmployeeService;

import com.github.pagehelper.PageHelper;

import com.github.pagehelper.PageInfo;



/**

 * 处理员工的CRUD请求

 * 

 * @author lenovo

 *

 */

@Controller

public class EmployeeController {



    @Autowired

    EmployeeService employeeService;

    /*

     * 显示员工数据(分页查询)

     */



    // 需要导入json包

    @RequestMapping("/emps")

    @ResponseBody

    public Msg getEmpsWithJson(@RequestParam(value = "pn", defaultValue = 
"1") Integer pn, Model model) {

        // 引入pageHelper分页插件

        // 传入页码和每页的大小

        PageHelper.startPage(pn, 5);

        // 后面紧跟的查询就是分页查询

        List<Employee> emps = employeeService.getAll();

        // 使用PageInfo包装查询后的结果,只需要将PageInfo交给页面

        // 封装了详细的分页信息,包括我们查询出来的数据

        // 连续传入5页

        PageInfo page = new PageInfo(emps, 5);

        return Msg.success().add("pageInfo",page);



    }





}

posted on 2021-02-16 00:07  northwest  阅读(222)  评论(0)    收藏  举报

导航