MVC架构探究及其源码实现(5)-相关组件实现

博学,切问,近思--詹子知(http://blog.csdn.net/zhiqiangzhan)

本文将讨论HandlerMapping,HandlerAdapter,ViewResolver组件类的具体实现。

URLHandlerMapping,利用request中包含的url信息,找到对应Handler对象,URLHandlerMapping是最典型的映射方式。 package com.google.mvc.web.servlet.handler;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.google.mvc.web.context.WebApplicationContext;
import com.google.mvc.web.context.WebApplicationContextAware;
import com.google.mvc.web.servlet.HandlerMapping;
import com.google.mvc.web.servlet.mvc.Controller;
import com.google.mvc.web.servlet.mvc.HttpRequestHandler;

public class URLHandlerMapping implements HandlerMapping, WebApplicationContextAware{

private static final Logger LOGGER = Logger.getLogger(URLHandlerMapping.class);
private WebApplicationContext wac;
private Map<String, Object> handlerMap = new ConcurrentHashMap<String, Object>();
private AtomicBoolean initialize = new AtomicBoolean(false);

@Override
public void setWebApplicationContext(WebApplicationContext wac) {
this.wac = wac;
}

@Override
public Object getHandler(HttpServletRequest request) throws Exception {
if(LOGGER.isDebugEnabled()){
LOGGER.debug("Find handler for request " + request.getServletPath());
}

if(initialize.compareAndSet(false, true)){
Map<String, HttpRequestHandler> map1 = wac.beansOfType(HttpRequestHandler.class);
for(String key : map1.keySet()){
handlerMap.put(key, map1.get(key));
}
Map<String, Controller> map2 = wac.beansOfType(Controller.class);
for(String key : map2.keySet()){
handlerMap.put(key, map2.get(key));
}
}
Object handler = handlerMap.get(getHandlerName(request));
if(handler == null){
handler = handlerMap.get("404");
}
return handler;
}

protected String getHandlerName(HttpServletRequest request){
String path = request.getServletPath();
int index = path.lastIndexOf('/');
String handleName = path.substring(index + 1, path.length());
return handleName;
}

}

HandlerAdapter用于把不同的Handler对象处理的结果封装成一个统一的对象ModelAndView,以达到逻辑上的一致处理。在这里,我们定义两种Handler类型

  1. Controller:用于处理用户的业务逻辑,并返回具体ModelAndView对象,具体接口定义如下 package com.google.mvc.web.servlet.mvc;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import com.google.mvc.web.servlet.ModelAndView;

    public interface Controller {

    ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception;
    }

  2. HttpRequestHandler:用于处理简单的HTTP请求。接口定义如下 package com.google.mvc.web.servlet.mvc;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public interface HttpRequestHandler {

    void handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception;

    }
    我们来看一下HandlerFor404的简单实现。 package com.google.mvc.web.servlet.mvc;

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public class HandlerFor404 implements HttpRequestHandler {

    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.sendRedirect("404.html");
    }

    }

我们需要为每一种不同的Handler类型指定一种HandlerAdapter,但这不是必须的。
ControllerHandlerAdapter package com.google.mvc.web.servlet.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.mvc.web.servlet.HandlerAdapter;
import com.google.mvc.web.servlet.ModelAndView;

public class ControllerHandlerAdapter implements HandlerAdapter{

public boolean supports(Object handler) {
return (handler instanceof Controller);
}

public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {

return ((Controller) handler).handleRequest(request, response);
}

public long getLastModified(HttpServletRequest request, Object handler) {

return -1L;
}

}
HttpRequestHandlerAdapter package com.google.mvc.web.servlet.mvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.mvc.web.servlet.HandlerAdapter;
import com.google.mvc.web.servlet.ModelAndView;

public class HttpRequestHandlerAdapter implements HandlerAdapter {

@Override
public long getLastModified(HttpServletRequest request, Object handler) {
return 0;
}

@Override
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
((HttpRequestHandler) handler).handleRequest(request, response);
return null;
}

@Override
public boolean supports(Object handler) {
return (handler instanceof HttpRequestHandler);
}
}

ViewResolver用于指定View的生成方式,我们先来看下AbstractView的定义 package com.google.mvc.web.servlet.mvc;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.mvc.web.servlet.View;

public abstract class AbstractView implements View {

private static final Logger LOGGER = Logger.getLogger(AbstractView.class);

public static final String INCLUDE_REQUEST_URI_ATTRIBUTE = "javax.servlet.include.request_uri";
public static final String FORWARD_REQUEST_URI_ATTRIBUTE = "javax.servlet.forward.request_uri";
public static final String FORWARD_CONTEXT_PATH_ATTRIBUTE = "javax.servlet.forward.context_path";
public static final String FORWARD_SERVLET_PATH_ATTRIBUTE = "javax.servlet.forward.servlet_path";
public static final String FORWARD_PATH_INFO_ATTRIBUTE = "javax.servlet.forward.path_info";
public static final String FORWARD_QUERY_STRING_ATTRIBUTE = "javax.servlet.forward.query_string";
public static final String DEFAULT_CONTENT_TYPE = "text/html;charset=ISO-8859-1";
private String contentType = DEFAULT_CONTENT_TYPE;
private boolean alwaysInclude = false;

@Override
public String getContentType() {
return contentType;
}

protected void exposeModelAsRequestAttributes(Map<String, Object> model, HttpServletRequest request){
Iterator<Entry<String, Object>> it = model.entrySet().iterator();
while (it.hasNext()) {
Entry<String, Object> entry = it.next();

String modelName = entry.getKey();
Object modelValue = entry.getValue();
if (modelValue != null) {
request.setAttribute(modelName, modelValue);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Added model object '" + modelName + "' of type [" + modelValue.getClass().getName() +
"] to request in view with name '" + this + "'");
}
} else {
request.removeAttribute(modelName);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Removed model object '" + modelName + "' from request in view with name '"
+ this + "'");
}
}
}

}

protected boolean useInclude(HttpServletRequest request, HttpServletResponse response) {
return (this.alwaysInclude || request.getAttribute(INCLUDE_REQUEST_URI_ATTRIBUTE) != null || response
.isCommitted());
}

protected void exposeForwardRequestAttributes(HttpServletRequest request) {
exposeRequestAttributeIfNotPresent(request, FORWARD_REQUEST_URI_ATTRIBUTE, request.getRequestURI());
exposeRequestAttributeIfNotPresent(request, FORWARD_CONTEXT_PATH_ATTRIBUTE, request.getContextPath());
exposeRequestAttributeIfNotPresent(request, FORWARD_SERVLET_PATH_ATTRIBUTE, request.getServletPath());
exposeRequestAttributeIfNotPresent(request, FORWARD_PATH_INFO_ATTRIBUTE, request.getPathInfo());
exposeRequestAttributeIfNotPresent(request, FORWARD_QUERY_STRING_ATTRIBUTE, request.getQueryString());
}

private void exposeRequestAttributeIfNotPresent(ServletRequest request, String name, Object value) {
if (request.getAttribute(name) == null) {
request.setAttribute(name, value);
}
}

public void setContentType(String contentType) {
this.contentType = contentType;
}

public boolean isAlwaysInclude() {
return alwaysInclude;
}

public void setAlwaysInclude(boolean alwaysInclude) {
this.alwaysInclude = alwaysInclude;
}
}
在这里我们仅实现一种View类型,也就是对jsp页面的简单处理。 package com.google.mvc.web.servlet.mvc;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

public class InternalResourceView extends AbstractView {

private static final Logger LOGGER = Logger.getLogger(InternalResourceView.class);
private String url;

@Override
public void render(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Rendering view with name '" + this + "' with model " + model);
}

if(model != null){
exposeModelAsRequestAttributes(model, request);
}

RequestDispatcher rd = request.getRequestDispatcher(url);
if (rd == null) {
throw new ServletException("Could not get RequestDispatcher for ["
+ getUrl() + "]: check that this file exists within your WAR");
}

if (useInclude(request, response)) {
response.setContentType(getContentType());
rd.include(request, response);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Included resource [" + getUrl() + "] in InternalResourceView '" + url + "'");
}
}else {
exposeForwardRequestAttributes(request);
rd.forward(request, response);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Forwarded to resource [" + getUrl() + "] in InternalResourceView '" + url + "'");
}
}
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String toString(){
return this.url;
}

}
好了,到这里,我们再来看看ViewResolver类的实现 package com.google.mvc.web.servlet.mvc;

import org.apache.log4j.Logger;

import com.google.mvc.web.servlet.View;
import com.google.mvc.web.servlet.ViewResolver;

public class DefaultViewResolver implements ViewResolver
{
private static final Logger LOGGER = Logger.getLogger(DefaultViewResolver.class);
private String prefix = "";
private String suffix = "";
private Class<View> viewClass;

@Override
public View resolveViewName(String viewName) throws Exception {
View view = viewClass.newInstance();
if(view instanceof InternalResourceView){
((InternalResourceView)view).setUrl(prefix + viewName + suffix);
}
return view;
}

public void setViewClass(String viewClass){
try {
this.viewClass = (Class<View>) this.getClass().getClassLoader().loadClass(viewClass);
} catch (ClassNotFoundException e) {
LOGGER.error("Can't load view class " + viewClass, e);
}
}

public String getPrefix() {
return prefix;
}

public void setPrefix(String prefix) {
this.prefix = prefix;
}

public String getSuffix() {
return suffix;
}

public void setSuffix(String suffix) {
this.suffix = suffix;
}
}

到这里,我们在整个MVC架构的源码实现已经完成了,下一篇,我们将介绍一个基于我们这个MVC架构的Demo。

相关文章:

  1. MVC架构探究及其源码实现(1)-理论基础
  2. MVC架构探究及其源码实现(2)-核心组件定义
  3. MVC架构探究及其源码实现(3)-WebApplicationContext
  4. MVC架构探究及其源码实现(4)-前端控制器
  5. MVC架构探究及其源码实现(6)-简单示例
时间: 2024-11-30 01:17:56

MVC架构探究及其源码实现(5)-相关组件实现的相关文章

MVC架构探究及其源码实现(6)-简单示例

博学,切问,近思--詹子知 (https://jameszhan.github.io) 在前一系列的文章中,我们已经完成了MVC架构模式的简单实现,尽管有些粗糙,有些功能还不完善,但是,麻雀虽小,五脏俱全.我们现在就用这个小小的框架,来实现我们的几个简单的应用. 限于篇幅,我们不可能把应用的所有代码都贴上来,我们先来演示一个Hello World的简单应用. 实现控制器HelloController.javapackage com.google.mvc.web.sample; import ja

MVC架构探究及其源码实现(4)-前端控制器

博学,切问,近思--詹子知 (https://jameszhan.github.io) 前端控制器是整个MVC框架中最为核心的一块,它主要用来拦截符合要求的外部请求,并把请求分发到不同的控制器去处理,根据控制器处理后的结果,生成相应的响应发送到客户端.前端控制器既可以使用Filter实现(Struts2采用这种方式),也可以使用Servlet来实现.这里我们就采用后一种方式来实现我们的MVC框架.   1.配置web.xml,使得我们的前端控制器可以拦截所有符合要求的用户请求,这里我们的前端控制

MVC架构探究及其源码实现(3)-WebApplicationContext

博学,切问,近思--詹子知 (https://jameszhan.github.io) 直接利用web.xml去配置和定义我们的对象组件显然是不灵活和不方便扩展的,由于我们系统中将会需要配置很多个不同的对象资源,比如控制器,View对象,HandlerMapping对象等等,如何对它们进行管理,如何能让我们的前端控制器访问和利用到到它们便是我们不得不面对的问题.还好,现在有了Spring,现在很多流行的MVC框架都支持使用Spring对自己容器里的对象资源进行管理.尽管Spring千好万好,我们

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

 博学,切问,近思--詹子知 (https://jameszhan.github.io) 上文中,我们讨论了MVC的架构的基本原理,这里,我们就要开始着手实现一个简单的WEB MVC前端控制器模型.为了实现这个架构的原型,我们必须引入几个新的概念. DispatcherServlet:前端控制器,也是整个架构的核心,负责处理和分发请求. HandlerMapping:处理器映射,他主要包含的是控制器的列表,对于特定的请求,根据HandlerMapping的映射关系,可以找到特定的控制器.最简单的

AngularJS入门教程之MVC架构实例分析_AngularJS

本文实例讲述了AngularJS的MVC架构.分享给大家供大家参考,具体如下: MVC应用程序架构最早于1970年起源于Smalltalk语言,后来在桌面应用程序开发中使用较为广泛,如今在WEB开发中也非常流行.MVC的核心思想是將数据的管理(Model).业务逻辑控制(Controller)和数据的展示(View)分离开来,使程序的逻辑性和可维护性更强. 对于AngularJS应用来说,视图(View)是DOM(文档对象模型),你可以理解为就是HTML页面.控制器(Controller)是一个

【深入Cocos2d-x】使用MVC架构搭建游戏Four

项目起源 项目Logo: 下面是该游戏的项目地址,各位想参考源代码的同学可以到我的GitHub上下载该项目的源码. 项目主页 GitHub地址 bug反馈及建议 我做这个项目的原始目的是实验MVC在游戏中的应用. Model-View-Controller(MVC)是一种组合设计模式,它体现了一种关注点分离(Separation of concerns,SoC)的思想.MVC主要把逻辑层和表现层进行了解耦,将一个问题划分成了不同的关注点.增强了应用的稳定性,易修改性和易复用性. MVC经常被使用

.NET平台MVC架构

问题描述 .NET平台与EXTJS或FLEX富客户端框架做开发时,MVC架构如何设计,如何才能提高后台代码复用率,不论用什么前端UI框架,后台代码都不怎么需要修改后台代码.有木有例子供参考.求助!!!!!!!!!!!!!!!!各位高手求思路. 解决方案 解决方案二:高内聚,低耦合解决方案三:编程对阵接口解决方案四:搞清楚,你重点问的问题是围绕写javascript代码,不是什么asp.netmvc.解决方案五:对于asp.net来说,除了在客户第一次访问时下载一个基本的html(其实只要有<di

Struts开发指南之MVC架构

模型-视图-控制器(MVC)是80年代Smalltalk-80出现的一种软件设计模式,现在已经被广泛的使用. 1.模型(Model) 模型是应用程序的主体部分.模型表示业务数据,或者业务逻辑. 2.视图(View) 视图是应用程序中用户界面相关的部分,是用户看到并与之交互的界面. 3.控制器(controller) 控制器工作就是根据用户的输入,控制用户界面数据显示和更新model对象状态. 开发指南之MVC架构-springmvc开发指南pdf"> MVC 式的出现不仅实现了功能模块和显

浅谈MVC架构模式

   MVC英文即Model-View-Controller,就是把一个应用分为三个层:视图层.模型层.控制层.    模型层(M)用于封装业务逻辑.    视图层(V)也叫表示层,就是与用户实现交互的界面,通常实现数据的输入和输出功能.    控制层(C)起到控制整个业务流程的作用.简单的说就是调用业务逻辑,然后把得到的数据转发给视图显示给用户. 为了熟悉MVC架构模式,特意将学生查询改成了MVC模式的. 1.首先建立客户端页面(student.jsp): 1 <%@ page languag