StudentController.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package com.lovecoding.springmvc;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.PathVariable;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. import javax.servlet.http.HttpSession;
  8. /**
  9. * RestFil 风格API接口
  10. * 一般我们设计 RESTFIL 风格接口, 会在类上 注解一个 请求路径
  11. * 然后在 方法上 设计 增删改查等操作, 使用不同的 Method 方法处理不同的业务
  12. *
  13. */
  14. @Controller
  15. @RequestMapping("/student")
  16. public class StudentController {
  17. /**
  18. * 新增操作
  19. * @param studentId
  20. * @return
  21. */
  22. @RequestMapping(
  23. value ="/{studentId}",
  24. method = RequestMethod.POST)
  25. public String add(@PathVariable(
  26. value = "studentId",
  27. required = true) Long studentId, HttpSession httpSession){
  28. System.out.println( "新增学员操作成功!!: " + studentId );
  29. httpSession.setAttribute("action", "添加成功 ID:" + studentId);
  30. return "/success.jsp";
  31. }
  32. /**
  33. * 删除操作
  34. * @param studentId
  35. * @return
  36. */
  37. @RequestMapping(
  38. value ="/{studentId}",
  39. method = RequestMethod.DELETE)
  40. @ResponseBody
  41. public String del(@PathVariable(
  42. value = "studentId",
  43. required = true) Long studentId, HttpSession httpSession){
  44. System.out.println( "删除学员操作成功!!: " + studentId );
  45. httpSession.setAttribute("action", "删除成功 ID:" + studentId);
  46. return "Delete Student Success!!: " + studentId;
  47. //return "/success.jsp";
  48. }
  49. /**
  50. * 修改操作
  51. * @param studentId
  52. * @return
  53. */
  54. @RequestMapping(
  55. value ="/{studentId}",
  56. method = RequestMethod.PUT)
  57. @ResponseBody
  58. public String update(@PathVariable(
  59. value = "studentId",
  60. required = true) Long studentId, HttpSession httpSession){
  61. System.out.println( "修改学员操作成功!!: " + studentId );
  62. httpSession.setAttribute("action", "修改成功 ID:" + studentId);
  63. return "Update Student Success!!: " + studentId;
  64. //return "/success.jsp";
  65. }
  66. /**
  67. * 查询操作
  68. * @param studentId
  69. * @return
  70. */
  71. @RequestMapping(
  72. value ="/{studentId}",
  73. method = RequestMethod.GET)
  74. public String get(@PathVariable(
  75. value = "studentId",
  76. required = true) Long studentId, HttpSession httpSession){
  77. System.out.println( "查询学员操作成功!!: " + studentId );
  78. httpSession.setAttribute("action", "查询成功 ID:" + studentId);
  79. return "/success.jsp";
  80. }
  81. }