springmvc使用json数据传递到bootstrap分页数据
1、服务器将查出的数据,以json字符串的形式返回给浏览器
2、浏览器收到js字符串,可以使用js对json进行解析,使用js通过dom增删改变页面
3、在pom.xml中加入jackson依赖包。 <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.8</version> </dependency>
4、修改控制层返回数据的方式package com.gwolf.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.github.pagehelper.PageHelper;import com.github.pagehelper.PageInfo;import com.gwolf.crud.bean.Employee;import com.gwolf.crud.service.EmployeeService;@Controllerpublic class EmployeeController { @Autowired EmployeeService employeeService; @RequestMapping("/emps") @ResponseBody public PageInfo<Employee> getEmpsWithJson(@RequestParam(value="pn",defaultValue="1") Integer pn,Model model) { PageHelper.startPage(pn, 5); List<Employee> employees = this.employeeService.getAll(); //传入连续显示的页数 PageInfo<Employee> pageInfo = new PageInfo<Employee>(employees,5); return pageInfo; }}
5、访问http://localhost:8080/ssm-crud2/emps,查看json数据是否正确返回。
6、新增的一个通用的数据返回类,这个类有操作的状态码。package com.gwolf.crud.bean;import java.util.HashMap;import java.util.Map;public class Msg implements java.io.Serializable{ //状态码:100-成功 private int code; //提示信息 private String msg; //用户返回给客户的数据 private Map<String,Object> extend = new HashMap(); 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; }}
7、重新修改控制层返回数据的方法:package com.gwolf.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.github.pagehelper.PageHelper;import com.github.pagehelper.PageInfo;import com.gwolf.crud.bean.Employee;import com.gwolf.crud.bean.Msg;import com.gwolf.crud.service.EmployeeService;@Controllerpublic class EmployeeController { @Autowired EmployeeService employeeService; @RequestMapping("/emps") @ResponseBody public Msg getEmpsWithJson(@RequestParam(value="pn",defaultValue="1") Integer pn,Model model) { PageHelper.startPage(pn, 5); List<Employee> employees = this.employeeService.getAll(); //传入连续显示的页数 PageInfo<Employee> pageInfo = new PageInfo<Employee>(employees,5); return Msg.success().add("pageInf", pageInfo); }}