`

MVC架构探究及其源码实现(2)-核心组件定义

 
阅读更多

上文中,我们讨论了MVC的架构的基本原理,这里,我们就要开始着手实现一个简单的WEB MVC前端控制器模型。为了实现这个架构的原型,我们必须引入几个新的概念

  1. DispatcherServlet:前端控制器,也是整个架构的核心,负责处理和分发请求。
  2. HandlerMapping:处理器映射,他主要包含的是控制器的列表,对于特定的请求,根据HandlerMapping的映射关系,可以找到特定的控制器。最简单的便是url到控制器的映射。
  3. HandlerAdapter:对于不同类型的控制器,该类负责把Handler请求处理的结果统一转换成ModelAndView。
  4. ModelAndView:包含数据和视图的信息,一般包含视图名,和这个视图需要用的数据,这里的Model大家不要误会为模型的概念,它只不过同时包含视图信息及这个视图需要显示的相关信息而已。
  5. ViewResolver:它View名称解析成View对象。
  6. View:定义response显示的详细内容。

 

HandlerMapping:

[java] view plaincopy
  1. package com.google.mvc.web.servlet;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4.   
  5.   
  6. public interface HandlerMapping {  
  7.       
  8.     String PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE = HandlerMapping.class.getName() + ".pathWithinHandlerMapping";  
  9.   
  10.     Object getHandler(HttpServletRequest request) throws Exception;  
  11.   
  12. }  

  HandlerAdapter:

[java] view plaincopy
  1. package com.google.mvc.web.servlet;  
  2.   
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletResponse;  
  5.   
  6.   
  7. public interface HandlerAdapter {  
  8.       
  9.     boolean supports(Object handler);     
  10.       
  11.     ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;  
  12.   
  13.     long getLastModified(HttpServletRequest request, Object handler);  
  14.   
  15. }  

  ViewResolver:

[java] view plaincopy
  1. package com.google.mvc.web.servlet;  
  2.   
  3. public interface ViewResolver {  
  4.       
  5.     View resolveViewName(String viewName) throws Exception;  
  6.   
  7. }  

  View:

[java] view plaincopy
  1. package com.google.mvc.web.servlet;  
  2.   
  3. import java.util.Map;  
  4. import javax.servlet.http.HttpServletRequest;  
  5. import javax.servlet.http.HttpServletResponse;  
  6.   
  7. public interface View {  
  8.       
  9.     String getContentType();  
  10.       
  11.     void render(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception;  
  12.   
  13. }  

ModelAndView:

[java] view plaincopy
  1. package com.google.mvc.web.servlet;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. public class ModelAndView {  
  7.       
  8.     private Object view;      
  9.     private Map<String, Object> model;  
  10.     private boolean cleared;  
  11.   
  12.     public ModelAndView() {  
  13.     }  
  14.       
  15.     public ModelAndView(String viewName) {  
  16.         this.view = viewName;  
  17.     }  
  18.       
  19.     public ModelAndView(View view) {  
  20.         this.view = view;  
  21.     }  
  22.   
  23.     public ModelAndView(String viewName, Map<String, Object> model) {  
  24.         this.view = viewName;  
  25.         if (model != null) {  
  26.             addAllObjects(model);  
  27.         }  
  28.     }  
  29.   
  30.   
  31.     public ModelAndView(View view, Map<String, Object> model) {  
  32.         this.view = view;  
  33.         if (model != null) {  
  34.             addAllObjects(model);  
  35.         }  
  36.     }  
  37.       
  38.     public ModelAndView(String viewName, String modelName, Object modelObject) {  
  39.         this.view = viewName;  
  40.         addObject(modelName, modelObject);  
  41.     }  
  42.   
  43.     public ModelAndView(View view, String modelName, Object modelObject) {  
  44.         this.view = view;  
  45.         addObject(modelName, modelObject);  
  46.     }  
  47.   
  48.     public void setViewName(String viewName) {  
  49.         this.view = viewName;  
  50.     }  
  51.   
  52.     public String getViewName() {  
  53.         return (this.view instanceof String ? (String) this.view : null);  
  54.     }  
  55.   
  56.     public void setView(View view) {  
  57.         this.view = view;  
  58.     }  
  59.   
  60.       
  61.     public View getView() {  
  62.         return (this.view instanceof View ? (View) this.view : null);  
  63.     }  
  64.   
  65.     public boolean hasView() {  
  66.         return (this.view != null);  
  67.     }  
  68.   
  69.     public boolean isReference() {  
  70.         return (this.view instanceof String);  
  71.     }  
  72.   
  73.     protected Map<String, Object> getModelInternal() {  
  74.         return this.model;  
  75.     }  
  76.   
  77.     public Map<String, Object> getModelMap() {  
  78.         if (this.model == null) {  
  79.             this.model = new HashMap<String, Object>();  
  80.         }  
  81.         return this.model;  
  82.     }  
  83.       
  84.     public Map<String, Object> getModel() {  
  85.         return getModelMap();  
  86.     }  
  87.   
  88.   
  89.     public ModelAndView addObject(String attributeName, Object attributeValue) {  
  90.         getModelMap().put(attributeName, attributeValue);  
  91.         return this;  
  92.     }  
  93.   
  94.       
  95.     public ModelAndView addObject(Object attributeValue) {  
  96.         getModelMap().put(attributeValue.toString(), attributeValue);  
  97.         return this;  
  98.     }  
  99.   
  100.   
  101.     public ModelAndView addAllObjects(Map<String, Object> modelMap) {  
  102.         getModelMap().putAll(modelMap);  
  103.         return this;  
  104.     }  
  105.   
  106.     public void clear() {  
  107.         this.view = null;  
  108.         this.model = null;  
  109.         this.cleared = true;  
  110.     }  
  111.   
  112.     public boolean isEmpty() {  
  113.         return (this.view == null && this.model == null);  
  114.     }  
  115.   
  116.     public boolean wasCleared() {  
  117.         return (this.cleared && isEmpty());  
  118.     }  
  119.   
  120.     public String toString() {  
  121.         StringBuffer buf = new StringBuffer("ModelAndView: ");  
  122.         if (isReference()) {  
  123.             buf.append("reference to view with name '").append(this.view).append("'");  
  124.         } else {  
  125.             buf.append("materialized View is [").append(this.view).append(']');  
  126.         }  
  127.         buf.append("; model is ").append(this.model);  
  128.         return buf.toString();  
  129.     }  
  130.   
  131. }  

 

这几个类由DispatcherServlet管理和控制,以下是它们的序列图: 
mvc

这些对象需要怎么注入到系统中呢?这里当然少不了配置文件的支持,现在比较流行的MVC框架大多可以使用Spring作为其IOC容器,为了简单起见,我们自己决定模拟Spring简单地实现一个配置管理容器,用于管理我们的各种对象资源。

原文地址:http://blog.csdn.net/zhiqiangzhan/article/details/4762600

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics