由于struts等开源MVC框架有漏洞,可以考虑自己实现一套轻量级的MVC框架
自定义一个Dispatcher,实现spring的ApplicationContextAware接口
-
public class AjaxDispatcher implements ApplicationContextAware {
-
//两个最重要的容器,分别存储请求path对应的method,以及请求path对应的bean。为了以后反射调用method.invoke(bean)
-
private static Map<String, Method> ajaxName2Method = new ConcurrentHashMap<String, Method>();
-
private static Map<String, Object> ajaxName2Bean = new ConcurrentHashMap<String, Object>();
-
...
-
-
@override
-
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
-
Map<String, Object> map = ctx.getBeansWithAnnotation(AjaxClass.class);//要求每个Ajax类在声明时会加上@AjaxClass,AjaxClass为自定义annotation
-
Collection<Object> beans = map.values();
-
for (Object bean : beans) {
-
logger.info(\"Inspecting AjaxClass: \" + bean.getClass());
-
registerAjaxClass(bean.getClass(), bean);
-
}
-
}
-
-
public static void registerAjaxClass(Class beanClass, Object bean) {
-
String prefix = ((AjaxClass) beanClass.getAnnotation(AjaxClass.class)).prefix();
-
if (prefix == null) {
-
prefix = \"\";
-
}
-
//将Ajax类的每个方法注册到ajaxName2Method容器中
-
Method[] ms = beanClass.getMethods();
-
for (int j = 0; j < ms.length; j++) {
-
Method m = ms[j];
-
//跟AjaxClass一样,Ajax类中的每个method也会声明为AjaxMethod(自定义annotation,其中有path属性)
-
AjaxMethod ajaxMethod = m.getAnnotation(AjaxMethod.class);
-
if (ajaxMethod != null) {
-
String path = ajaxMethod.path();
-
if (path == null || path.trim().equals(\"\")) {
-
path = m.getName();
-
}
-
String fullPath = prefix + path;
-
ajaxName2Method.put(fullPath, ms[j]);
-
logger.info(\"Registering ajax method at path: \" + fullPath);
-
ajaxName2Bean.put(fullPath, bean);
-
}
-
}
-
}
-
-
//业务逻辑调用,每一个请求都会调用该方法(web.xml中配置相应的servlet,servlet中调用该方法)
-
public static String dispatch(Map<String, String> req) throws Exception {
-
ObjectMapper mapper = new ObjectMapper();
-
String path = req.get(KEY_PATH);
-
Object ins = ajaxName2Bean.get(path);
-
Method m = ajaxName2Method.get(path);
-
if (m == null) {
-
logger.error(\"No matching method found for path: \" + path);
-
return METHOD_ERROR_RESPONSE;
-
}
-
Class<?>[] paramClasses = m.getParameterTypes();
-
Object[] paramValues = null;
-
if (null != paramClasses && 1 == paramClasses.length) {
-
paramValues = new Object[paramClasses.length];
-
try {
-
paramValues[0] = Long.parseLong(req.get(KEY_USERID));
-
} catch (NumberFormatException e) {
-
logger.error(\"userid incorrect\" + req.get(KEY_USERID), e);
-
return PARAM_ERROR_RESPONSE;
-
}
-
-
-
} else if (null != paramClasses && 2 <= paramClasses.length) {
-
paramValues = new Object[paramClasses.length];
-
try {
-
paramValues[0] = Long.parseLong(req.get(KEY_USERID));
-
} catch (NumberFormatException e) {
-
logger.error(\"userid incorrect\" + req.get(KEY_USERID), e);
-
return PARAM_ERROR_RESPONSE;
-
}
-
-
-
try {
-
paramValues[1] = mapper.readValue(req.get(KEY_PARAM), paramClasses[1]);
-
} catch (Exception e) {
-
logger.error(\"Can not deserialize json param \" + req.get(KEY_PARAM) + \" to class \" + paramClasses[1], e);
-
return PARAM_ERROR_RESPONSE;
-
}
-
}
-
-
try {
-
Object result = m.invoke(ins, paramValues);
-
if (result instanceof String) {
-
return (String) result;
-
} else {
-
StringWriter buf = new StringWriter();
-
mapper.writeValue(buf, result);
-
return buf.toString();
-
}
-
} catch (Throwable e) {
-
logger.error(\"Exception occured when invoke Ajax Method, path=\" + path, e);
-
return DEFAULT_ERROR_RESPONSE;
-
}
-
}
-
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28912557/viewspace-1129091/
,如需转载,请注明出处,否则将追究法律责任。