天天看點

MVC架構探究及其源碼實作(2)-核心元件定義

 博學,切問,近思--詹子知 (https://jameszhan.github.io)

上文中,我們讨論了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.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;

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;

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) {

if (model != null) {

addallobjects(model);

public modelandview(view view, map<string, object> model) {

public modelandview(string viewname, string modelname, object modelobject) {

addobject(modelname, modelobject);

public modelandview(view view, string modelname, object modelobject) {

public void setviewname(string viewname) {

public string getviewname() {

return (this.view instanceof string ? (string) this.view : null);

public void setview(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>();

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);

public modelandview addallobjects(map<string, object> modelmap) {

getmodelmap().putall(modelmap);

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架構探究及其源碼實作(2)-核心元件定義

這些對象需要怎麼注入到系統中呢?這裡當然少不了配置檔案的支援,現在比較流行的mvc架構大多可以使用spring作為其ioc容器,為了簡單起見,我們自己決定模拟spring簡單地實作一個配置管理容器,用于管理我們的各種對象資源。

相關文章:

mvc架構探究及其源碼實作(1)-理論基礎

mvc架構探究及其源碼實作(3)-webapplicationcontext

mvc架構探究及其源碼實作(4)-前端控制器

mvc架構探究及其源碼實作(5)-相關元件實作

mvc架構探究及其源碼實作(6)-簡單示例