ITPub博客

首页 > 应用开发 > IT综合 > Spring自定义MVC

Spring自定义MVC

原创 IT综合 作者:541732025 时间:2014-03-25 13:42:05 0 删除 编辑
由于struts等开源MVC框架有漏洞,可以考虑自己实现一套轻量级的MVC框架
自定义一个Dispatcher,实现spring的ApplicationContextAware接口

点击(此处)折叠或打开

  1. public class AjaxDispatcher implements ApplicationContextAware {
  2.     //两个最重要的容器,分别存储请求path对应的method,以及请求path对应的bean。为了以后反射调用method.invoke(bean)
  3.     private static Map<String, Method> ajaxName2Method = new ConcurrentHashMap<String, Method>();
  4.     private static Map<String, Object> ajaxName2Bean = new ConcurrentHashMap<String, Object>();
  5.     ...
  6.     
  7.     @override
  8.     public void setApplicationContext(ApplicationContext ctx) throws BeansException {
  9.         Map<String, Object> map = ctx.getBeansWithAnnotation(AjaxClass.class);//要求每个Ajax类在声明时会加上@AjaxClass,AjaxClass为自定义annotation
  10.         Collection<Object> beans = map.values();
  11.         for (Object bean : beans) {
  12.             logger.info(\"Inspecting AjaxClass: \" + bean.getClass());
  13.             registerAjaxClass(bean.getClass(), bean);
  14.         }
  15.     }

  16.     public static void registerAjaxClass(Class beanClass, Object bean) {
  17.         String prefix = ((AjaxClass) beanClass.getAnnotation(AjaxClass.class)).prefix();
  18.         if (prefix == null) {
  19.             prefix = \"\";
  20.         }
  21.         //将Ajax类的每个方法注册到ajaxName2Method容器中
  22.         Method[] ms = beanClass.getMethods();
  23.         for (int j = 0; j < ms.length; j++) {
  24.             Method m = ms[j];
  25.         //跟AjaxClass一样,Ajax类中的每个method也会声明为AjaxMethod(自定义annotation,其中有path属性)
  26.             AjaxMethod ajaxMethod = m.getAnnotation(AjaxMethod.class);
  27.             if (ajaxMethod != null) {
  28.                 String path = ajaxMethod.path();
  29.                 if (path == null || path.trim().equals(\"\")) {
  30.                     path = m.getName();
  31.                 }
  32.                 String fullPath = prefix + path;
  33.                 ajaxName2Method.put(fullPath, ms[j]);
  34.                 logger.info(\"Registering ajax method at path: \" + fullPath);
  35.                 ajaxName2Bean.put(fullPath, bean);
  36.             }
  37.         }
  38.     }

  39.     //业务逻辑调用,每一个请求都会调用该方法(web.xml中配置相应的servlet,servlet中调用该方法)
  40.     public static String dispatch(Map<String, String> req) throws Exception {
  41.         ObjectMapper mapper = new ObjectMapper();
  42.         String path = req.get(KEY_PATH);
  43.         Object ins = ajaxName2Bean.get(path);
  44.         Method m = ajaxName2Method.get(path);
  45.         if (m == null) {
  46.             logger.error(\"No matching method found for path: \" + path);
  47.             return METHOD_ERROR_RESPONSE;
  48.         }
  49.         Class<?>[] paramClasses = m.getParameterTypes();
  50.         Object[] paramValues = null;
  51.         if (null != paramClasses && 1 == paramClasses.length) {
  52.             paramValues = new Object[paramClasses.length];
  53.             try {
  54.                 paramValues[0] = Long.parseLong(req.get(KEY_USERID));
  55.             } catch (NumberFormatException e) {
  56.                 logger.error(\"userid incorrect\" + req.get(KEY_USERID), e);
  57.                 return PARAM_ERROR_RESPONSE;
  58.             }


  59.         } else if (null != paramClasses && 2 <= paramClasses.length) {
  60.             paramValues = new Object[paramClasses.length];
  61.             try {
  62.                 paramValues[0] = Long.parseLong(req.get(KEY_USERID));
  63.             } catch (NumberFormatException e) {
  64.                 logger.error(\"userid incorrect\" + req.get(KEY_USERID), e);
  65.                 return PARAM_ERROR_RESPONSE;
  66.             }


  67.             try {
  68.                 paramValues[1] = mapper.readValue(req.get(KEY_PARAM), paramClasses[1]);
  69.             } catch (Exception e) {
  70.                 logger.error(\"Can not deserialize json param \" + req.get(KEY_PARAM) + \" to class \" + paramClasses[1], e);
  71.                 return PARAM_ERROR_RESPONSE;
  72.             }
  73.         }

  74.         try {
  75.             Object result = m.invoke(ins, paramValues);
  76.             if (result instanceof String) {
  77.                 return (String) result;
  78.             } else {
  79.                 StringWriter buf = new StringWriter();
  80.                 mapper.writeValue(buf, result);
  81.                 return buf.toString();
  82.             }
  83.         } catch (Throwable e) {
  84.             logger.error(\"Exception occured when invoke Ajax Method, path=\" + path, e);
  85.             return DEFAULT_ERROR_RESPONSE;
  86.         }
  87.     }
  88. }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28912557/viewspace-1129091/,如需转载,请注明出处,否则将追究法律责任。

上一篇: MapReduce初探
下一篇: Spring RMI配置
请登录后发表评论 登录
全部评论

注册时间:2013-05-23

  • 博文量
    127
  • 访问量
    482639