<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">了解了springMVC的执行调度过程,接下来自然是敲代码了,</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">这里我维护了一个matchObstruct,用来存放匹配到的拦截器。</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">整个处理过程在doService方法中</span>
private void doService(HttpServletRequest req, HttpServletResponse resp) {
Handler h = getHandler(req, resp);
MyModelAndView mv = new MyModelAndView();
if (null == h) { // url已被mapping才进行拦截否则直接返回
return;
}
initMatchObstructList(req);
/*
* 这里的处理和springMVC有所不同,这里在beforHandler返回false时直接返回,而
* springMVC会调用afterCompletion方法,此时的afterCompletion和返回ture的
* afterCompletion方法不同,这里以后再做细致分析,先这样好了。
*/
if (!doBeforeHandler(req, resp)) {
return;
}
mv = invokeMappedMethod(h, req, resp);
doAfterHandler(req, resp, mv);
if ((null == mv) || (("").equals(mv.getView()))) {
return;
}
loadView(mv, req, resp);
doAfterViewLoad(req, resp);
}
初始化匹配的拦截器集合,由于在加载所有的拦截器时已经对拦截器集合os按index升序排列,这里不再做处理。
private void initMatchObstructList(HttpServletRequest req) {
matchObstruct.clear();// 清空匹配的拦截器集合
for (int i = 0; i < os.size(); i++) {
Obstruct o = os.get(i);
if (o.pathMatch(req)) {
matchObstruct.add(o);// 可以认为matchObstruct依旧是按照index赠序排列
}
}
}
几个拦截器方法:
private void doAfterViewLoad(HttpServletRequest req,
HttpServletResponse resp) {
for (int i = matchObstruct.size() - 1; i >= 0; i--) {
Obstruct o = matchObstruct.get(i);
try {
o.getInterceptor().afterViewLoad(req, resp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void doAfterHandler(HttpServletRequest req,
HttpServletResponse resp, MyModelAndView mv) {
//降序
for (int i = matchObstruct.size() - 1; i >= 0; i--) {
Obstruct o = matchObstruct.get(i);
try {
o.getInterceptor().afterHandler(req, resp, mv);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private boolean doBeforeHandler(HttpServletRequest req,
HttpServletResponse resp) {
for (Obstruct o : matchObstruct) {
try {
if(!o.getInterceptor().beforeHandler(req, resp)){
return false;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}
匹配handler,执行handler,渲染页面的方法:
/**
* 获得映射到的控制器
*
* @param req
* @param resp
* @return
*/
private Handler getHandler(HttpServletRequest req, HttpServletResponse resp) {
String url = req.getRequestURI();
return hs.get(url + req.getMethod().trim().toUpperCase());
}
/**
* 执行映射方法返回MyModelAndView对象
*
* @param h
* @param req
* @param resp
* @return
*/
private MyModelAndView invokeMappedMethod(Handler h,
HttpServletRequest req, HttpServletResponse resp) {
Model model = new Model();
Method m = h.getMappingMethod();
try {
return (MyModelAndView) m.invoke(h.getController(), new Object[] {
model, req, resp });
} catch (IllegalAccessException e1) {
e1.printStackTrace();
return null;
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
return null;
} catch (InvocationTargetException e1) {
e1.printStackTrace();
return null;
}
}
/**
* 加载页面
*
* @param mv
* @param req
* @param resp
*/
private void loadView(MyModelAndView mv, HttpServletRequest req,
HttpServletResponse resp) {
Model m = mv.getModel();
for (String s : mv.getModel().keySet()) {
req.setAttribute(s, m.get(s));
}
String viewName = mv.getView().trim();
String viewFileName = viewPath + viewName + ".jsp";
try {
this.getServletContext().getRequestDispatcher(viewFileName)
.forward(req, resp);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
完成这些我想一个简单地可以实现基本功能的MVC框架便可以运行了。