java 增删改 json_基于maven+ssm的增删改查之前后端之间使用json进行交互(显示员工信息)...

接上一节。

首先是在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());

%>

SSM-CURD

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值