> clazz =Class.forName(className);if(clazz.isAnnotationPresent(MyController.class)){ioc.put(toLowerFirstword(clazz.getSimpleName),clazz.newInstance);}else{continue;}} catch (Exception e) {e.printStackTrace;continue;}}}private void initHandlerMapping{if(ioc.isEmpty){return;}try {for (Entry entry: ioc.entrySet) {Class extends Object> clazz = entry.getValue.getClass;if(!clazz.isAnnotationPresent(MyController.class)){continue;}//拼url时,是controller头的url拼上方法上的urlString baseUrl ="";if(clazz.isAnnotationPresent(MyRequestMapping.class)){MyRequestMapping annotation = clazz.getAnnotation(MyRequestMapping.class);baseUrl=annotation.value;}Method methods = clazz.getMethods;for (Method method : methods) {if(!method.isAnnotationPresent(MyRequestMapping.class)){continue;}MyRequestMapping annotation = method.getAnnotation(MyRequestMapping.class);String url = annotation.value;url =(baseUrl+"/"+url).replaceAll("/+", "/");handlerMapping.put(url,method);controllerMap.put(url,clazz.newInstance);System.out.println(url+","+method);}}} catch (Exception e) {e.printStackTrace;}}/*** 把字符串的首字母小写* @param name* @return*/private String toLowerFirstWord(String name){char charArray = name.toCharArray;charArray[0] += 32;return String.valueOf(charArray);}}这里我们就开发完了自己的SpringMVC,现在我们测试一下:
@MyController@MyRequestMapping("/test")public class TestController {@MyRequestMapping("/doTest")public void test1(HttpServletRequest request, HttpServletResponse response,@MyRequestParam("param") String param){System.out.println(param);try {response.getWriter.write( "doTest method success! param:"+param);} catch (IOException e) {e.printStackTrace;}}@MyRequestMapping("/doTest2")public void test2(HttpServletRequest request, HttpServletResponse response){try {response.getWriter.println("doTest2 method success!");} catch (IOException e) {e.printStackTrace;}}}访问
http://localhost:8080/liughMVC/test/doTest?param=liugh 如下:

文章插图
访问一个不存在的试试:

文章插图
到这里我们就大功告成了!
源码地址:
https://github.com/qq53182347/liughMVC
【手撸了一个 Spring MVC 框架】
推荐阅读
-
逗儿妈妈男孩说出一番话,让母亲哑口无言,6岁男孩挑食被母亲呵斥
-
【iPhone】面对疫情,苹果公布新政策,除中国市场,果粉可享140元补贴
-
「微信」微信“新规定”确认,两大功能不再免费,网友:逼我放弃?
-
华为mate▲华为P50Pro概念图:这就是四曲真全屏,比华为Mate40更值得期待
-
海外网|旅美大熊猫美香幼崽体重曝光 性别将在数周后公布
-
硬拳大卫君|印度官员依旧云淡风轻,国际权威雷霆发声,灾难性局面已经酿成
-
河南省|刚喝完“秋天的第一杯奶茶”,第一家同名公司就成立了?
-
中国足球|李霄鹏:蒿俊闵伤情比较严重,或缺席鲁能此后比赛
-
PAPI最时尚|你喜欢的品牌在什么位置呢,腕表王国官位表
-
爱疯电子峰哥苹果史上最耐用的手机,虽降价幅度大,但市场份额不降反增
-
三体|真实感扑面而来,炸裂震撼!《流浪地球2》绝不辜负你我的期待
-
-
-
营收|中国纸巾“第一品牌”,年营收225亿,维达和洁柔都不是对手
-
花朵|3种花人见人爱,植株低矮小巧,花开一整年,超级好养
-
水镜晓先生|俄3万吨潜艇启动测试,可引发超级海啸吞没城市,威力远超核导弹
-
「搞笑男人心」加入双电侧滑门,1.5T+7DCT,7.98万起,别克GL8碰到对手
-
张艺谋|《满江红》中五个谋女郎均没有以往的惊艳感,张艺谋看走眼了?
-
-