接上一节。
首先是在EmployeeController.java中,新建一个返回json数据的方法,注销掉原有的getEmps方法。
EmployeeController.java
packagecom.gong.curd.controller;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.ResponseBody;importcom.github.pagehelper.PageHelper;importcom.github.pagehelper.PageInfo;importcom.gong.curd.bean.Employee;importcom.gong.curd.service.EmployeeService;importcom.gong.curd.utils.Msg;
@Controllerpublic classEmployeeController {
@Autowired
EmployeeService employeeService;
@ResponseBody
@RequestMapping("/emps")public Msg getEmpsWithJson(@RequestParam(value="pn",defaultValue="1") Integer pn) {
PageHelper.startPage(pn,5);
List emps =employeeService.getAll();
PageInfo page = new PageInfo(emps,5);return Msg.success().add("pageInfo",page);
}/*@RequestMapping("/emps")
public String getEmps(@RequestParam(value="pn",defaultValue="1") Integer pn,
Model model) {
PageHelper.startPage(pn,5);
List emps = employeeService.getAll();
PageInfo page = new PageInfo(emps,5);
model.addAttribute("pageInfo", page);
return "list";
}*/}
Msg.java
packagecom.gong.curd.utils;importjava.util.HashMap;importjava.util.Map;public classMsg {//状态码
private intcode;//提示信息
privateString msg;//返回的用户信息
private Map extend = new HashMap();public staticMsg success() {
Msg result= newMsg();
result.setCode(100);
result.setMsg("处理成功");returnresult;
}public staticMsg fail() {
Msg result= newMsg();
result.setCode(200);
result.setMsg("处理失败");returnresult;
}publicMsg add(String key,Object value) {this.getExtend().put(key, value);return this;
}public intgetCode() {returncode;
}public void setCode(intcode) {this.code =code;
}publicString getMsg() {returnmsg;
}public voidsetMsg(String msg) {this.msg =msg;
}public MapgetExtend() {returnextend;
}public void setExtend(Mapextend) {this.extend =extend;
}
}
Msg有状态码、提示信息以及用于添加相关信息的extend属性。
然后在index.jsp同级目录下新建index2.jsp,将原来index.jsp中的数据剪贴过去,然后我们再对index.jsp进行操作。
index.jsp
员工列表pageContext.setAttribute("APP_PATH",request.getContextPath());
%>