BillController.java

张创琦

  1. package com.zcq.controller;
  2. import com.github.pagehelper.PageInfo;
  3. import com.zcq.entity.Bill;
  4. import com.zcq.entity.BillType;
  5. import com.zcq.service.BillService;
  6. import com.zcq.service.TypeService;
  7. import org.springframework.stereotype.Controller;
  8. import org.springframework.ui.Model;
  9. import org.springframework.web.bind.annotation.PathVariable;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestParam;
  12. import javax.annotation.Resource;
  13. import java.util.List;
  14. @Controller
  15. @RequestMapping("/bill")
  16. public class BillController {
  17. @Resource
  18. private TypeService typeService;
  19. @Resource
  20. private BillService billService;
  21. @RequestMapping("/list-page")
  22. public String listPage(
  23. @RequestParam(defaultValue = "1") int pageNum,
  24. @RequestParam(defaultValue = "10") int pageSize,
  25. Bill b, Model model) {
  26. List<BillType> types = typeService.list();
  27. model.addAttribute("types", types);
  28. PageInfo<Bill> pageInfo = billService.listPage(b, pageNum, pageSize);
  29. model.addAttribute("page", pageInfo);
  30. model.addAttribute("bill", b);
  31. return "/bill/list-page";
  32. }
  33. // 查询
  34. @RequestMapping("/list")
  35. public String list(Bill b, Model model) {
  36. List<BillType> types = typeService.list();
  37. model.addAttribute("types", types);
  38. List<Bill> list = billService.list(b);
  39. model.addAttribute("list", list);
  40. model.addAttribute("bill", b);
  41. return "/bill/list";
  42. }
  43. // 调到添加页面
  44. // 经测试,无法添加数据
  45. @RequestMapping("/toAdd")
  46. public String toAdd(Model model) {
  47. List<BillType> types = typeService.list();
  48. model.addAttribute("types", types);
  49. return "/bill/add";
  50. }
  51. // 添加
  52. @RequestMapping("/add")
  53. public String add(Bill b) {
  54. billService.add(b);
  55. return "redirect:/bill/list";
  56. }
  57. // 删除
  58. // 可以通过指令删除, 但是无法通过list的点击按钮进行
  59. @RequestMapping("/delete/{id}")
  60. public String delete(@PathVariable("id") Long id) {
  61. billService.delete(id);
  62. return "redirect:/bill/list";
  63. }
  64. // 修改
  65. @RequestMapping("/toUpdate/{id}")
  66. public String toUpdate(@PathVariable("id") Long id, Model model) {
  67. List<BillType> types = typeService.list();
  68. model.addAttribute("types", types);
  69. Bill bill = billService.get(id);
  70. model.addAttribute("bill", bill);
  71. return "/bill/update";
  72. }
  73. // 修改
  74. @RequestMapping("/update")
  75. public String update(Bill b) {
  76. billService.update(b);
  77. return "redirect:/bill/list";
  78. }
  79. }