• 项目实战:中央控制器实现(2)-优化Controller,将共性动作抽取到中央控制器


     1、FruitController

    • FruitController已经和Web没有关系了,和Web容器解耦,可以脱离Web容器做单元测试
    1. package com.csdn.fruit.controller;
    2. import com.csdn.fruit.dto.PageInfo;
    3. import com.csdn.fruit.dto.PageQueryParam;
    4. import com.csdn.fruit.dto.Result;
    5. import com.csdn.fruit.pojo.Fruit;
    6. import com.csdn.fruit.service.FruitService;
    7. import com.csdn.mymvc.annotation.*;
    8. @Controller
    9. @RequestMapping("/fruit")
    10. public class FruitController {
    11. @Autowire
    12. private FruitService fruitService;
    13. @GetMapping("/index")
    14. public Result index(Integer pageNo,String keyword) {
    15. if (pageNo == null) {
    16. pageNo = 1;
    17. }
    18. if (keyword == null) {
    19. keyword = "";
    20. }
    21. PageQueryParam pageQueryParam = new PageQueryParam(pageNo, 5, keyword);
    22. PageInfo pageInfo = fruitService.getFruitPageInfo(pageQueryParam);
    23. return Result.OK(pageInfo);
    24. }
    25. @PostMapping("/add")
    26. public Result add(@RequestBody Fruit fruit) {
    27. fruitService.addFruit(fruit);
    28. return Result.OK();
    29. }
    30. @GetMapping("/del")
    31. public Result del(Integer fid){
    32. fruitService.delFruit(fid);
    33. return Result.OK();
    34. }
    35. @GetMapping("/edit")
    36. public Result edit(Integer fid){
    37. Fruit fruit = fruitService.getFruitById(fid);
    38. return Result.OK(fruit);
    39. }
    40. @GetMapping("/getFname")
    41. public Result getFname(String fname){ //fname是请求参数
    42. Fruit fruit = fruitService.getFruitByFname(fname);
    43. return fruit == null ? Result.OK() : Result.Fail();
    44. }
    45. @PostMapping("/update")
    46. public Result update(@RequestBody Fruit fruit){ //fruit是请求体参数
    47. fruitService.updateFruit(fruit);
    48. return Result.OK();
    49. }
    50. }

    2、DispatcherServlet

    1. package com.csdn.mymvc.core;
    2. import com.csdn.fruit.dto.Result;
    3. import com.csdn.fruit.util.ResponseUtil;
    4. import jakarta.servlet.RequestDispatcher;
    5. import jakarta.servlet.ServletContext;
    6. import jakarta.servlet.ServletException;
    7. import jakarta.servlet.annotation.WebServlet;
    8. import jakarta.servlet.http.HttpServlet;
    9. import jakarta.servlet.http.HttpServletRequest;
    10. import jakarta.servlet.http.HttpServletResponse;
    11. import org.junit.Test;
    12. import java.io.IOException;
    13. import java.lang.reflect.InvocationTargetException;
    14. import java.lang.reflect.Method;
    15. import java.util.Arrays;
    16. import java.util.Map;
    17. @WebServlet("/*")
    18. public class DispatcherServlet extends HttpServlet {
    19. private final String BEAN_FACTORY = "beanFactory";
    20. private final String CONTROLLER_BEAN_MAP = "controllerBeanMap";
    21. @Test
    22. public void uri() {
    23. String uri = "/fruit/index";
    24. String[] arr = uri.split("/");
    25. System.out.println(Arrays.toString(arr));//[, fruit, index]
    26. }
    27. @Override
    28. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    29. String[] staticResourceSuffixes = {".html", ".jsp", ".jpg", ".png", ".gif", ".css", ".js", ".ico"};
    30. String uri = req.getRequestURI();
    31. if (Arrays.stream(staticResourceSuffixes).anyMatch(uri::endsWith)) {
    32. RequestDispatcher defaultDispatcher = req.getServletContext().getNamedDispatcher("default");
    33. defaultDispatcher.forward(req, resp);
    34. } else {
    35. String[] arr = uri.split("/");
    36. if (arr == null || arr.length != 3) {
    37. throw new RuntimeException(uri + "非法!");
    38. }
    39. //[, fruit, index]
    40. String requestMapping = "/" + arr[1];
    41. String methodMapping = "/" + arr[2];
    42. ServletContext application = getServletContext();
    43. ControllerDefinition controllerDefinition = ((Map) application.getAttribute(CONTROLLER_BEAN_MAP))
    44. .get(requestMapping);
    45. if (controllerDefinition == null) {
    46. throw new RuntimeException(requestMapping + "对应的controller组件不存在!");
    47. }
    48. //获取请求方式,例如:get或者post
    49. String requestMethodStr = req.getMethod().toLowerCase();
    50. //get_/index
    51. Method method = controllerDefinition.getMethodMappingMap().get(requestMethodStr + "_" + methodMapping);
    52. Object controllerBean = controllerDefinition.getControllerBean();
    53. try {
    54. //第 1 步:参数处理
    55. //第 2 步:方法调用
    56. //调用controllerBean对象中的method方法
    57. method.setAccessible(true);
    58. Object returnObj = method.invoke(controllerBean, req, resp);
    59. if (returnObj != null && returnObj instanceof Result) {
    60. Result result= (Result) returnObj;
    61. ResponseUtil.print(resp,result);
    62. }
    63. } catch (IllegalAccessException e) {
    64. e.printStackTrace();
    65. throw new RuntimeException(e);
    66. } catch (InvocationTargetException e) {
    67. e.printStackTrace();
    68. throw new RuntimeException(e);
    69. }
    70. }
    71. }
    72. }

  • 相关阅读:
    uni-app + mui-player & vue + mui-player 播放flv文件
    Linux CentOS 8(网卡的配置与管理)
    循环树和checkbox 相关选中处理
    力扣热题100_链表_141_环形链表
    力扣刷题--数组1
    最长回文子串
    如何区分小角X射线散射和小角X射线衍射?
    MySql数据库-02MySQL环境搭建
    跳跃游戏(动态规划)
    【Linux】yum && vim 基础工具的使用
  • 原文地址:https://blog.csdn.net/m0_65152767/article/details/134297100