栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

手写@RequestMapping和@Controller注解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

手写@RequestMapping和@Controller注解

public class ExtDispatcherServlet extends HttpServlet {
	// mvc bean key=beanid ,value=对象
	private ConcurrentHashMap mvcBeans = new ConcurrentHashMap();
	// mvc 请求方法 key=requestUrl,value=对象
	private ConcurrentHashMap mvcBeanUrl = new ConcurrentHashMap();
	// mvc 请求方法 key=requestUrl,value=方法
	private ConcurrentHashMap mvcMethodUrl = new ConcurrentHashMap();

	
	public void init() throws ServletException {
		try {
			// 1.获取当前包下所有的类
			List> classes = ClassUtil.getClasses("com.itmayiedu.ext.controller");
			// 2.初始化当前包下所有的类,使用Java反射机制初始化对象存放在SpringMVC容器中key(beanId)-value(
			// 当前实例对象)
			findClassMVCBeans(classes);
			// 3.初始化HandlerMapping方法,将url和方法对应上
			handlerMapping(mvcBeans);

		} catch (Exception e) {

		}
	}

	// 2.初始化当前包下所有的类,使用Java反射机制初始化对象存放在SpringMVC容器中key(beanId)-value(
	// 当前实例对象)
	public void findClassMVCBeans(List> classes)
			throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		mvcBeans = new ConcurrentHashMap();
		for (Class classInfo : classes) {
			ExtController extController = classInfo.getDeclaredAnnotation(ExtController.class);
			if (extController != null) {
				// 默认类名小写 作为bean的名称
				String beanId = ClassUtil.toLowerCaseFirstOne(classInfo.getSimpleName());
				mvcBeans.put(beanId, ClassUtil.newInstance(classInfo));
			}
		}

	}

	// 3.初始化HandlerMapping方法,将url和方法对应上
	public void handlerMapping(ConcurrentHashMap mvcBeans) {
		// 遍历mvc bean对象
		for (Map.Entry entry : mvcBeans.entrySet()) {
			// springmvc 注入object对象
			Object mvcObject = entry.getValue();
			// 判断类上是否有@ExtRequestMapping注解
			Class classInfo = mvcObject.getClass();
			String requestbaseUrl = null;
			ExtRequestMapping classExtRequestMapping = classInfo.getAnnotation(ExtRequestMapping.class);
			if (classExtRequestMapping != null) {
				requestbaseUrl = classExtRequestMapping.value();
			}
			// 遍历当前类的所有方法,判断方法上是否有注解
			Method[] declaredMethods = classInfo.getDeclaredMethods();
			for (Method method : declaredMethods) {
				ExtRequestMapping methodExtRequestMapping = method.getDeclaredAnnotation(ExtRequestMapping.class);
				if (methodExtRequestMapping != null) {
					String httpRequestUrl = methodExtRequestMapping.value();
					mvcBeanUrl.put(requestbaseUrl + httpRequestUrl, mvcObject);
					mvcMethodUrl.put(requestbaseUrl + httpRequestUrl, method.getName());
				}
			}
		}
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			doDispatch(req, resp);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public void doDispatch(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		// 1.获取请求url地址
		String requestUrl = req.getRequestURI();
		// 2.使用请求url查找对应mvc 控制器bean
		Object object = mvcBeanUrl.get(requestUrl);
		if (object == null) {
			resp.getWriter().println("http ext not found  controller 404");
			return;
		}
		// 3.获取对应的请求方法
		String methodName = mvcMethodUrl.get(requestUrl);
		if (StringUtils.isEmpty(methodName)) {
			resp.getWriter().println("http ext not found Method 404");
			return;
		}
		// 4.使用java反射技术执行方法
		Class classInfo = object.getClass();
		String resultPage = (String) methodInvoke(classInfo, object, methodName);
		// 5.视图展示
		viewdisplay(resultPage, req, resp);
	}

	// 执行方法
	public Object methodInvoke(Class classInfo, Object object, String methodName) {
		try {
			Method method = classInfo.getMethod(methodName);
			Object result = method.invoke(object);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	// 视图展示
	public void viewdisplay(String pageName, HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		// 获取后缀信息
		String suffix = ".jsp";
		// 页面目录地址
		String prefix = "/";
		req.getRequestDispatcher(prefix + pageName + suffix).forward(req, res);
	}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/328872.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号