上文中,我们讨论了MVC的架构的基本原理,这里,我们就要开始着手实现一个简单的WEB MVC前端控制器模型。为了实现这个架构的原型,我们必须引入几个新的概念。
- DispatcherServlet:前端控制器,也是整个架构的核心,负责处理和分发请求。
- HandlerMapping:处理器映射,他主要包含的是控制器的列表,对于特定的请求,根据HandlerMapping的映射关系,可以找到特定的控制器。最简单的便是url到控制器的映射。
- HandlerAdapter:对于不同类型的控制器,该类负责把Handler请求处理的结果统一转换成ModelAndView。
- ModelAndView:包含数据和视图的信息,一般包含视图名,和这个视图需要用的数据,这里的Model大家不要误会为模型的概念,它只不过同时包含视图信息及这个视图需要显示的相关信息而已。
- ViewResolver:它View名称解析成View对象。
- View:定义response显示的详细内容。
HandlerMapping:
package com.google.mvc.web.servlet;
import javax.servlet.http.HttpServletRequest;
public interface HandlerMapping {
String PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE = HandlerMapping.class.getName() + ".pathWithinHandlerMapping";
Object getHandler(HttpServletRequest request) throws Exception;
}
HandlerAdapter:
package com.google.mvc.web.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface HandlerAdapter {
boolean supports(Object handler);
ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;
long getLastModified(HttpServletRequest request, Object handler);
}
ViewResolver:
package com.google.mvc.web.servlet;
public interface ViewResolver {
View resolveViewName(String viewName) throws Exception;
}
View:
package com.google.mvc.web.servlet;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface View {
String getContentType();
void render(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception;
}
ModelAndView:
package com.google.mvc.web.servlet;
import java.util.HashMap;
import java.util.Map;
public class ModelAndView {
private Object view;
private Map<String, Object> model;
private boolean cleared;
public ModelAndView() {
}
public ModelAndView(String viewName) {
this.view = viewName;
}
public ModelAndView(View view) {
this.view = view;
}
public ModelAndView(String viewName, Map<String, Object> model) {
this.view = viewName;
if (model != null) {
addAllObjects(model);
}
}
public ModelAndView(View view, Map<String, Object> model) {
this.view = view;
if (model != null) {
addAllObjects(model);
}
}
public ModelAndView(String viewName, String modelName, Object modelObject) {
this.view = viewName;
addObject(modelName, modelObject);
}
public ModelAndView(View view, String modelName, Object modelObject) {
this.view = view;
addObject(modelName, modelObject);
}
public void setViewName(String viewName) {
this.view = viewName;
}
public String getViewName() {
return (this.view instanceof String ? (String) this.view : null);
}
public void setView(View view) {
this.view = view;
}
public View getView() {
return (this.view instanceof View ? (View) this.view : null);
}
public boolean hasView() {
return (this.view != null);
}
public boolean isReference() {
return (this.view instanceof String);
}
protected Map<String, Object> getModelInternal() {
return this.model;
}
public Map<String, Object> getModelMap() {
if (this.model == null) {
this.model = new HashMap<String, Object>();
}
return this.model;
}
public Map<String, Object> getModel() {
return getModelMap();
}
public ModelAndView addObject(String attributeName, Object attributeValue) {
getModelMap().put(attributeName, attributeValue);
return this;
}
public ModelAndView addObject(Object attributeValue) {
getModelMap().put(attributeValue.toString(), attributeValue);
return this;
}
public ModelAndView addAllObjects(Map<String, Object> modelMap) {
getModelMap().putAll(modelMap);
return this;
}
public void clear() {
this.view = null;
this.model = null;
this.cleared = true;
}
public boolean isEmpty() {
return (this.view == null && this.model == null);
}
public boolean wasCleared() {
return (this.cleared && isEmpty());
}
public String toString() {
StringBuffer buf = new StringBuffer("ModelAndView: ");
if (isReference()) {
buf.append("reference to view with name '").append(this.view).append("'");
} else {
buf.append("materialized View is [").append(this.view).append(']');
}
buf.append("; model is ").append(this.model);
return buf.toString();
}
}
这几个类由DispatcherServlet管理和控制,以下是它们的序列图:
![mvc](https://p-blog.csdn.net/images/p_blog_csdn_net/zhiqiangzhan/EntryImages/20091103/mvc.jpg)
这些对象需要怎么注入到系统中呢?这里当然少不了配置文件的支持,现在比较流行的MVC框架大多可以使用Spring作为其IOC容器,为了简单起见,我们自己决定模拟Spring简单地实现一个配置管理容器,用于管理我们的各种对象资源。