自定义MVC框架【二】

MVC模式的概念与处理流程

MVC的概念:

Model(模型):包含数据和行为。不过现在一般都分离开来:Value Object(数据) 和 服务层(行为)。

View(视图):负责进行模型的展示,一般就是展示给用户的界面。

Controller(控制器):接收用户请求,委托给模型进行处理,处理完毕后把返回的模型数据返回给视图,由视图负责展示,起了调度的作用。


处理流程:

1、 客户端发出HTTP请求,服务器接收如果匹配DispatcherServlet的请求映射路径(在web.xml中指定),Web容器将该请求转交给DispatcherServlet处理。

2、DispacherServlet接收到此请求后,根据请求的信息(URL、HTTP方法、请求报头文、请求参数、Cookie等)及HandleMapping的配置匹配Handler,匹配成功,则返回一个HandlerExecutionChain对象(包含一个Handler处理器对象、多个HandlerInterceptor拦截器)。

3、 DispacherServlet得到Handler后,把Handler封装为适配器,以适配器接口真正调用了处理器的功能处理方法(先执行拦截器的preHandler方法),返回ModelAndView(如其名,包含了视图逻辑名和模型数据信息)。

4、DispacherServlet借由ViewResolver完成逻辑视图名到真实对象的解析工作。

5、DispacherServlet得到视图对象View,使用它对ModelAndView中的模型数据进行渲染。

6、客户端得到响应(可能是HTML页面、XML、JSON串、PDF……)


MVC模式的优缺点

1. MVC模式的优点:

1.各施其职,互不干涉
在MVC模式中,三个层各施其职,所以如果一旦哪一层的需求发生了变化,就只需要更改相应的层中的代码而不会影响到其它层中的代码。

2.有利于开发中的分工
在MVC模式中,由于按层把系统分开,那么就能更好的实现开发中的分工。网页设计人员可以进行开发视图层中的JSP,对业务熟悉的开发人员可开发业务层,而其它开发人员可开发控制层。

3.有利于组件的重用
分层后更有利于组件的重用。如控制层可独立成一个能用的组件,视图层也可做成通用的操作界面。

2. MVC模式的缺点

MVC的不足体现在以下几个方面:

(1)增加了系统结构和实现的复杂性。对于简单的界面,严格遵循MVC,使模型、视图与控制器分离,会增加结构的复杂性,并可能产生过多的更新操作,降低运行效率。

(2)视图与控制器间的过于紧密的连接。视图与控制器是相互分离,但确实联系紧密的部件,视图没有控制器的存在,其应用是很有限的,反之亦然,这样就妨碍了他们的独立重用。

(3)视图对模型数据的低效率访问。依据模型操作接口的不同,视图可能需要多次调用才能获得足够的显示数据。对未变化数据的不必要的频繁访问,也将损害操作性能。

(4) 目前,一般高级的界面工具或构造器不支持MVC模式。改造这些工具以适应MVC需要和建立分离的部件的代价是很高的,从而造成使用MVC的困难。


jar包的详细下载教程

jar包的详细下载地址,点击下载
步骤图解:
第一步:
在这里插入图片描述
第二步:
在这里插入图片描述
第三步:
在这里插入图片描述


案例分析【增强MVC】

今天的案例是基于上一次的基础上,如有不明白的地方可以去看我的上一篇博客(自定义MVC框架
所需要的jar包有4个(前面可以自己写底层的反射代码)
在这里插入图片描述
不知道如何下载的,上面有详细下载教程。

先导入工具类【四个】
ActionModel

package com.zyp.beike;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 用来描述action标签
 * @author zhouyanpeng
 *
 */
public class ActionModel implements Serializable{

	private static final long serialVersionUID = 6145949994701469663L;
	
	private Map<String, ForwardModel> forwardModels = new HashMap<String, ForwardModel>();
	
	private String path;
	
	private String type;
	
	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void put(ForwardModel forwardModel){
		forwardModels.put(forwardModel.getName(), forwardModel);
	}
	
	public ForwardModel get(String name){
		return forwardModels.get(name);
	}
	
}

ConfigModel

package com.zyp.beike;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 用来描述config标签
 * @author zhouyanpeng
 *
 */
public class ConfigModel implements Serializable{

	private static final long serialVersionUID = -2334963138078250952L;
	
	private Map<String, ActionModel> actionModels = new HashMap<String, ActionModel>();
	
	public void put(ActionModel actionModel){
		actionModels.put(actionModel.getPath(), actionModel);
	}
	
	public ActionModel get(String name){
		return actionModels.get(name);
	}
	
}

ConfigModelFactory

package com.zyp.beike;

import java.io.InputStream;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ConfigModelFactory {
	private ConfigModelFactory() {

	}

	private static ConfigModel configModel = null;

	public static ConfigModel newInstance() throws Exception {
		return newInstance("config.xml");
	}

	/**
	 * 工厂模式创建config建模对象
	 * 
	 * @param path
	 * @return
	 * @throws Exception
	 */
	public static ConfigModel newInstance(String path) throws Exception {
		if (null != configModel) {
			return configModel;
		}

		ConfigModel configModel = new ConfigModel();
		InputStream is = ConfigModelFactory.class.getResourceAsStream(path);
		SAXReader saxReader = new SAXReader();
		Document doc = saxReader.read(is);
		List<Element> actionEleList = doc.selectNodes("/config/action");
		ActionModel actionModel = null;
		ForwardModel forwardModel = null;
		for (Element actionEle : actionEleList) {
			 actionModel = new ActionModel();
			actionModel.setPath(actionEle.attributeValue("path"));
			actionModel.setType(actionEle.attributeValue("type"));
			List<Element> forwordEleList = actionEle.selectNodes("forward");
			for (Element forwordEle : forwordEleList) {
				forwardModel = new ForwardModel();
				forwardModel.setName(forwordEle.attributeValue("name"));
				forwardModel.setPath(forwordEle.attributeValue("path"));
				forwardModel.setRedirect(forwordEle.attributeValue("redirect"));
				actionModel.put(forwardModel);
			}

			configModel.put(actionModel);
		}

		return configModel;
	}
	
	public static void main(String[] args) {
		try {
			ConfigModel configModel = ConfigModelFactory.newInstance();
			ActionModel actionModel = configModel.get("/loginAction");
			ForwardModel forwardModel = actionModel.get("failed");
			System.out.println(actionModel.getType());
			System.out.println(forwardModel.getPath());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

ForwardModel

package com.zyp.beike;

import java.io.Serializable;

/**
 * 用来描述forward标签
 * @author zhouyanpeng
 *
 */
public class ForwardModel implements Serializable {

	private static final long serialVersionUID = -8587690587750366756L;

	private String name;
	private String path;
	private String redirect;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getRedirect() {
		return redirect;
	}

	public void setRedirect(String redirect) {
		this.redirect = redirect;
	}

}

主控制器

package com.zyp.framework;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

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

import org.apache.commons.beanutils.BeanUtils;

/**
 * 主控制器
 * @author zhouyanpeng
 *
 */
public class DispatcherServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private ConfigModel configModel = null;

	public void init() {
		try {
			//将原有的读取框架的默认配置文件 转变成 可配置路径的配置文件
			String xmlPath = this.getInitParameter("xmlPath");
			if(xmlPath == null || "".equals(xmlPath)) {
				configModel = ConfigModelFactory.build();
			}else {
				configModel = ConfigModelFactory.build(xmlPath);
			}
			//主控制器有哪些子控制器由configModel决定
			configModel = ConfigModelFactory.build();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		init();
		String url = req.getRequestURI();//路径
		url = url.substring(url.lastIndexOf("/"),url.lastIndexOf("."));
		
		//通过 path 拿到  type
		ActionModel actionModel = configModel.pop(url);
		
		//如果没有配置子控制器
		if(actionModel == null) {
			throw new RuntimeException("没有配置对应的子控制器 Action");
		}
		
		/**
		 * 将Action的信息配置到xml(反射实例化)
		 * 
		 * 原来子控制器的来源是map集合,如此,子控制器就被写死在map容器中,代码不灵活
		 * 现将子控制器以配置的方式存在config.xml中,即通过改变config.xml中的内容给中央控制器添加子控制器
		 */
		
		try {
			Action action = (Action)Class.forName(actionModel.getType()).newInstance();//反射实例化
			
			/**
			 * 调用模型驱动接口,获取所要操作的实体类,然后将jsp传递过来的参数封装到实体类中
			 */
			if(action instanceof ModelDriven) {
				ModelDriven modelDriven = (ModelDriven) action;//CalAction向上提升为ModelDriven
				Object model = modelDriven.getModel();//要操作的实体类
				
				/*//String-属性  String[]-属性值
				Map<String, String[]> map = req.getParameterMap();
				for (Map.Entry<String, String[]> entry : map.entrySet()) {//遍历
					//可以获取到类对应的属性、属性值
				}*/  //底层源码
				
				//设置属性、属性值  将所有的参数自动封装到实体类T中
				BeanUtils.populate(model, req.getParameterMap());
			}
			
			/**
			 * 通过结果码控制页面跳转
			 * 
			 * 每个子控制器都要对结果进行处理(重定向/转发),代码重复量较大
			 * 针对这一现象,将其交给配置文件处理。
			 */
			//调用增强版的子控制器处理业务逻辑
			String code = action.execut(req, resp); //要跳转的路径
			ForwardModel forwardModel = actionModel.pop(code);
			
			//如果没有配置子控制器
			if(forwardModel == null) {
				throw new RuntimeException("没有配置对应的子控制器Action的处理方式forwardModel");
			}
			
			String jspPath = forwardModel.getPath();
			if(forwardModel.isRedirect()) {
				resp.sendRedirect(req.getContextPath()+jspPath);//重定向
			}else {
				req.getRequestDispatcher(jspPath).forward(req, resp);//转发
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
}

子控制器

package com.zyp.framework;

import java.io.IOException;

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

/**
 * 子控制器
 * 专门用来处理业务逻辑
 * @author zhouyanpeng
 *
 */
public interface Action {
	String execut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}

增强版子控制器:

package com.zyp.framework;

import java.io.IOException;

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

/**
 * 子控制器
 * 专门用来处理业务逻辑
 * @author zhouyanpeng
 *
 */
public interface Action {
	String execut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}

CalAction类【加减乘除方法】

package com.zyp.web;

import java.io.IOException;

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

import com.zyp.entity.Cal;
import com.zyp.framework.ActionSupport;
import com.zyp.framework.ModelDriven;

public class CalAction extends ActionSupport implements ModelDriven<Cal>{
	
	private Cal cal = new Cal();
	
	//加:
	public String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setAttribute("res", Integer.valueOf(cal.getNum1()) + Integer.valueOf(cal.getNum2()));
		return "calRes";
	}
	
	//减:
	public String del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setAttribute("res", Integer.valueOf(cal.getNum1()) - Integer.valueOf(cal.getNum2()));
		return "calRes";
	}
	
	//乘:
	public String multiply(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setAttribute("res", Integer.valueOf(cal.getNum1()) * Integer.valueOf(cal.getNum2()));
		return "calRes";
	}
	
	//除:
	public String divide(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setAttribute("res", Integer.valueOf(cal.getNum1()) / Integer.valueOf(cal.getNum2()));
		return "calRes";
	}

	@Override
	public Cal getModel() {
		return cal;
	}
}

ModelDriven【模型驱动接口】

package com.zyp.framework;

/**
 * 模型驱动接口
 * 
 * 用来处理jsp页面传递过来的参数,将所有参数自动封装到实体类T中
 * 		
 * @author zhouyanpeng
 *
 * @param <T>
 */
public interface ModelDriven<T> {
	T getModel();
}

解析XML

package com.zyp.framework;

import java.io.InputStream;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ConfigModelFactory {
	
	public static ConfigModel build() throws Exception {
		return build("config.xml");
	}
//为了方便  我将configModel为 1节点  actionModel为2节点 forwardModel为3节点
	
	public static ConfigModel build(String xmlPath) throws Exception {
		ConfigModel configModel = new ConfigModel();  //一个大的1节点
		InputStream in = ConfigModelFactory.class.getResourceAsStream(xmlPath); //获取config.xml文件的读取流
		SAXReader sax = new SAXReader();//用到了dom4j解析方法
		Document doc = sax.read(in);//解析
		ActionModel actionModel = null; //实例化2节点
		ForwardModel forwardModel = null; //实列化3节点
		List<Element> actionNodes = doc.selectNodes("/config/action");  //找到在1节点下的2节点
		for (Element actionNode : actionNodes) { //便利
			 actionModel = new ActionModel();   //如果有的话就创建一个
			 actionModel.setPath(actionNode.attributeValue("path"));  //给2节点里的属性path赋值
			 actionModel.setType(actionNode.attributeValue("type"));   //给2节点里的属性type赋值
			 List<Element> forwardNodes = actionNode.selectNodes("forward"); //查找2节点下有多少个3节点
			 for (Element forwardNode : forwardNodes) {
				forwardModel  = new ForwardModel(); //如果有的话就创建一个
				forwardModel.setName(forwardNode.attributeValue("name"));  //给3节点里的属性name赋值
				forwardModel.setPath(forwardNode.attributeValue("path")); //给3节点里的属性path赋值
				forwardModel.setRedirect(!"false".equals(forwardNode.attributeValue("redirect"))); //给3节点里的属性redirect赋值
				actionModel.push(forwardModel);   //加入到action属性中
			}
			 configModel.push(actionModel);//加入到集合属性中
		}
		return configModel;
	}
	public static void main(String[] args) throws Exception {
		ConfigModel configModel = build();
		ActionModel actionModel = configModel.pop("/add");
		System.out.println("type的值"+actionModel.getType());
		ForwardModel forwardModel = actionModel.pop("calres");
		System.out.println(forwardModel.getPath() +"   "+forwardModel.isRedirect());
	}
}

web文件配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>T224_mvc</display-name>
  
  <servlet>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<servlet-class>com.zyp.framework.DispatcherServlet</servlet-class>
  	
  	<!-- <init-param>
  		<param-name>xmlPath</param-name>
  		<param-value>/config.xml</param-value>
  	</init-param> -->
  	
  	
  </servlet>
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
</web-app>

今天的分享就到这了,如有疑问,联系博主。

猜你喜欢

转载自blog.csdn.net/zyp_baoku/article/details/91344964
今日推荐