Spring自定義MVC
由於struts等開源MVC框架有漏洞,可以考慮自己實現一套輕量級的MVC框架
自定義一個Dispatcher,實現spring的ApplicationContextAware介面
自定義一個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部落格 ” ,連結:http://blog.itpub.net/28912557/viewspace-1129091/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 省掉bean自定義spring mvc註解注入json值BeanSpringMVCJSON
- 如何讓Spring MVC顯示自定義的404 Not Found頁面SpringMVC
- 自定義Spring ShellSpring
- spring 自定義標籤Spring
- 自定義 Spring Boot StarterSpring Boot
- Spring Boot 自定義 starterSpring Boot
- 自定義Spring Boot StarterSpring Boot
- 【spring-boot】自定義starterSpringboot
- Spring Boot 自定義註解失效Spring Boot
- Spring Boot - 自定義 Banner 圖案Spring Boot
- 聊聊自定義SPI如何使用自定義標籤注入到spring容器中Spring
- spring - mvcSpringMVC
- spring、spring MVC、spring BootMVCSpring Boot
- (第五講)自定義Spring Boot StarterSpring Boot
- spring 自定義屬性解析器Spring
- 設計模式(三十)----綜合應用-自定義Spring框架-自定義Spring IOC-定義bean、登錄檔相關類設計模式Spring框架Bean
- 擴充spring元件之自定義標籤Spring元件
- Spring Cloud:自定義 Ribbon 負載均衡策略SpringCloud負載
- 小代學Spring Boot之自定義StarterSpring Boot
- Spring Boot之自定義JSON轉換器Spring BootJSON
- Spring Cloud Gateway---自定義過濾器SpringCloudGateway過濾器
- Spring Cloud Netflix—自定義Ribbon客戶端SpringCloud客戶端
- Spring Cloud自定義引導屬性源SpringCloud
- Spring Boot讀取自定義外部屬性Spring Boot
- spring mvc interceptorsSpringMVC
- spring - mvc - @ScheduledSpringMVC
- spring - mvc - @ValidSpringMVC
- SpringBoot(3)-MVC自動配置及自定義檢視控制器Spring BootMVC
- 設計模式(三十一)----綜合應用-自定義Spring框架-自定義Spring IOC-定義解析器、IOC容器相關類設計模式Spring框架
- Spring Boot之Validation自定義實現總結Spring Boot
- spring boot學習(2): SpringApplication和自定義bannerSpring BootAPP
- Spring 定時器的使用—Xml、Annotation、自定義Spring定時器XML
- Spring 定時器的使用---Xml、Annotation、自定義Spring定時器XML
- 用Spring組合自定義的註釋 - mscharhagSpring
- 你的開發利器Spring自定義註解Spring
- Spring Cloud Gateway-自定義異常處理SpringCloudGateway
- spring boot學習(5): 程式exit code自定義Spring Boot
- 基於Spring-AOP的自定義分片工具Spring
- Spring自定義引數解析器設計Spring