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
- MVC自定義檢視規則MVC
- 自定義Spring ShellSpring
- spring 自定義標籤Spring
- Spring Boot 自定義 starterSpring Boot
- 自定義Spring Boot StarterSpring Boot
- 自定義 Spring Boot StarterSpring Boot
- MVC驗證06-自定義錯誤資訊MVC
- 【spring-boot】自定義starterSpringboot
- Spring 自定義註解(上)Spring
- Spring Boot 自定義註解Spring Boot
- Spring Boot - 自定義 Banner 圖案Spring Boot
- Spring Boot 自定義註解失效Spring Boot
- spring2自定義標籤Spring
- 聊聊自定義SPI如何使用自定義標籤注入到spring容器中Spring
- MVC驗證07-自定義Model級別驗證MVC
- Spring Boot(3)---自定義spring boot starter 問題Spring Boot
- (第五講)自定義Spring Boot StarterSpring Boot
- spring security 自定義認證登入Spring
- spring 自定義屬性解析器Spring
- Spring MVC AOP通過自定義註解方式攔截Controller等實現日誌管理SpringMVCController
- 設計模式(三十)----綜合應用-自定義Spring框架-自定義Spring IOC-定義bean、登錄檔相關類設計模式Spring框架Bean
- Spring MVCSpringMVC
- spring - mvcSpringMVC
- Spring Cloud Gateway---自定義過濾器SpringCloudGateway過濾器
- 小代學Spring Boot之自定義StarterSpring Boot
- 擴充spring元件之自定義標籤Spring元件
- Spring Boot讀取自定義外部屬性Spring Boot
- Spring Cloud自定義引導屬性源SpringCloud
- Spring Cloud Config—提供自定義RestTemplateSpringCloudREST
- ASP.NET MVC 學習筆記-7.自定義配置資訊ASP.NETMVC筆記
- 自定義MVC檢視引擎ViewEngine 建立Model的專屬檢視MVCView
- Spring MVC 零配置 / Spring MVC JavaConfigSpringMVCJava
- 設計模式(三十一)----綜合應用-自定義Spring框架-自定義Spring IOC-定義解析器、IOC容器相關類設計模式Spring框架
- Spring Boot/Spring MVCSpring BootMVC
- spring、spring MVC、spring BootMVCSpring Boot
- 自定義View:自定義屬性(自定義按鈕實現)View