1、FruitController
- FruitController已经和Web没有关系了,和Web容器解耦,可以脱离Web容器做单元测试
javapackage com.csdn.fruit.controller; import com.csdn.fruit.dto.PageInfo; import com.csdn.fruit.dto.PageQueryParam; import com.csdn.fruit.dto.Result; import com.csdn.fruit.pojo.Fruit; import com.csdn.fruit.service.FruitService; import com.csdn.mymvc.annotation.*; @Controller @RequestMapping("/fruit") public class FruitController { @Autowire private FruitService fruitService; @GetMapping("/index") public Result index(Integer pageNo,String keyword) { if (pageNo == null) { pageNo = 1; } if (keyword == null) { keyword = ""; } PageQueryParam pageQueryParam = new PageQueryParam(pageNo, 5, keyword); PageInfo<Fruit> pageInfo = fruitService.getFruitPageInfo(pageQueryParam); return Result.OK(pageInfo); } @PostMapping("/add") public Result add(@RequestBody Fruit fruit) { fruitService.addFruit(fruit); return Result.OK(); } @GetMapping("/del") public Result del(Integer fid){ fruitService.delFruit(fid); return Result.OK(); } @GetMapping("/edit") public Result edit(Integer fid){ Fruit fruit = fruitService.getFruitById(fid); return Result.OK(fruit); } @GetMapping("/getFname") public Result getFname(String fname){ //fname是请求参数 Fruit fruit = fruitService.getFruitByFname(fname); return fruit == null ? Result.OK() : Result.Fail(); } @PostMapping("/update") public Result update(@RequestBody Fruit fruit){ //fruit是请求体参数 fruitService.updateFruit(fruit); return Result.OK(); } }
2、DispatcherServlet
javapackage com.csdn.mymvc.core; import com.csdn.fruit.dto.Result; import com.csdn.fruit.util.ResponseUtil; import jakarta.servlet.RequestDispatcher; import jakarta.servlet.ServletContext; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.junit.Test; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; import java.util.Map; @WebServlet("/*") public class DispatcherServlet extends HttpServlet { private final String BEAN_FACTORY = "beanFactory"; private final String CONTROLLER_BEAN_MAP = "controllerBeanMap"; @Test public void uri() { String uri = "/fruit/index"; String[] arr = uri.split("/"); System.out.println(Arrays.toString(arr));//[, fruit, index] } @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String[] staticResourceSuffixes = {".html", ".jsp", ".jpg", ".png", ".gif", ".css", ".js", ".ico"}; String uri = req.getRequestURI(); if (Arrays.stream(staticResourceSuffixes).anyMatch(uri::endsWith)) { RequestDispatcher defaultDispatcher = req.getServletContext().getNamedDispatcher("default"); defaultDispatcher.forward(req, resp); } else { String[] arr = uri.split("/"); if (arr == null || arr.length != 3) { throw new RuntimeException(uri + "非法!"); } //[, fruit, index] String requestMapping = "/" + arr[1]; String methodMapping = "/" + arr[2]; ServletContext application = getServletContext(); ControllerDefinition controllerDefinition = ((Map<String, ControllerDefinition>) application.getAttribute(CONTROLLER_BEAN_MAP)) .get(requestMapping); if (controllerDefinition == null) { throw new RuntimeException(requestMapping + "对应的controller组件不存在!"); } //获取请求方式,例如:get或者post String requestMethodStr = req.getMethod().toLowerCase(); //get_/index Method method = controllerDefinition.getMethodMappingMap().get(requestMethodStr + "_" + methodMapping); Object controllerBean = controllerDefinition.getControllerBean(); try { //第 1 步:参数处理 //第 2 步:方法调用 //调用controllerBean对象中的method方法 method.setAccessible(true); Object returnObj = method.invoke(controllerBean, req, resp); if (returnObj != null && returnObj instanceof Result) { Result result= (Result) returnObj; ResponseUtil.print(resp,result); } } catch (IllegalAccessException e) { e.printStackTrace(); throw new RuntimeException(e); } catch (InvocationTargetException e) { e.printStackTrace(); throw new RuntimeException(e); } } } }
项目实战:中央控制器实现(2)-优化Controller,将共性动作抽取到中央控制器
丁总学Java2023-11-08 20:53
相关推荐
IT学长编程3 分钟前
计算机毕设选题 基于SpringBoot的书店管理系统的设计与实现 网上书店系统 前后端分离 Java毕设项目 毕业设计选题 【附源码+文档报告+安装调试】张较瘦_6 分钟前
应用型本科计算机类专业毕业设计与论文选题指南IT学长编程6 分钟前
计算机毕设选题 基于SpringBoot的房产租赁管理系统 房屋租赁系统 前后端分离 Java毕设项目 毕业设计选题 【附源码+文档报告+安装调试】木易 士心2 小时前
MPAndroidChart 用法解析和性能优化 - Kotlin & Java 双版本后端小张2 小时前
SpringBoot 控制台秒变炫彩特效,秀翻同事指南!好家伙VCC2 小时前
**标题:发散创新:探索AR开发框架的核心技术**随着增强现实(AR)技术的飞速发展,AR开发框架成为了开发者们关注的焦chools2 小时前
学习问题日记-4懒人Ethan3 小时前
解决一个C# 在Framework 4.5反序列化的问题235163 小时前
【MySQL】MVCC:从核心原理到幻读解决方案扶苏-su3 小时前
Java---注解