1. 什么是MVC
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写, 它是一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码。
2. MVC结构
V
jsp/ios/android C servlet/action M 实体域模型(名词) 过程域模型(动词) 注1:不能跨层调用 注2:只能出现由上而下的调用
3. 自定义MVC工作原理图
具体代码实现如下(我们就以一个简单计算器作为示例)
1.首先需要导入需要用到的jar包
2.对xml文件进行建模(工厂模式)
建模代码
ForwardModel
package com.hmc.framework;import java.io.Serializable;/** * 用来描述forward标签 * @author Administrator * */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; }}
ActionModel
package com.hmc.framework;import java.io.Serializable;import java.util.HashMap;import java.util.Map;/** * 用来描述action标签 * @author Administrator * */public class ActionModel implements Serializable{ private static final long serialVersionUID = 6145949994701469663L; private MapforwardModels = new HashMap (); 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.hmc.framework;import java.io.Serializable;import java.util.HashMap;import java.util.Map;/** * 用来描述config标签 * @author Administrator * */public class ConfigModel implements Serializable{ private static final long serialVersionUID = -2334963138078250952L; private MapactionModels = new HashMap (); public void put(ActionModel actionModel){ actionModels.put(actionModel.getPath(), actionModel); } public ActionModel get(String name){ return actionModels.get(name); } }
ConfigModelFactory
package com.hmc.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 { private ConfigModelFactory() { } private static ConfigModel configModel = null; public static ConfigModel newInstance() throws Exception { return newInstance("mvc.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); ListactionEleList = 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 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(); } }}
3.编写一个中央控制器(DispatcherServlet)
package com.hmc.framework;import java.io.IOException;import java.lang.reflect.Field;import java.util.HashMap;import java.util.Map;import java.util.Map.Entry;import java.util.Set;import javax.servlet.ServletException;import javax.servlet.annotation.WebInitParam;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.beanutils.BeanUtils;import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;import com.hmc.web.AddCalAction;/** * 中央控制器 * 作用: 接受请求,通过请求寻找处理请求对应的子控制器。 * @author * */public class DispatcherServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; //private MapactionMap = new HashMap<>(); //在configModel对象包含了所有的子控制信息 private ConfigModel configModel; public void init() { // actionMap.put("/addCal", new AddCalAction()); try { String xmlPath=this.getInitParameter("xmlPath"); if(xmlPath==null|| "".equals(xmlPath)) { configModel=ConfigModelFactory.newInstance(); }else { configModel=ConfigModelFactory.newInstance(xmlPath); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { init(); String url= request.getRequestURI();// /***/addCal.action url = url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));// IAction action = actionMap.get(url); ActionModel actionModel = configModel.get(url);// if(actionModel ==null) {// throw new RuntimeException("你没有配置action标签,找不到对应的子控制器处理浏览器发送的请求");// } try { Action action=(Action) Class.forName(actionModel.getType()).newInstance(); //action就是com.zking.web.CalAction if(action instanceof ModelDrivern) { ModelDrivern modelDrivern=(ModelDrivern)action; //此时的model的所有属性值都是null的 Object model = modelDrivern.getModel(); BeanUtils.populate(model, request.getParameterMap()); //我们可以将request.getParameterMap();的值通过反射的方式将其塞进model实例中// Map parameterMap = request.getParameterMap();// Set > entrySet = parameterMap.entrySet();// Class clz = model.getClass();// for (Entry entry : entrySet) {// Field field = clz.getField(entry.getKey());// field.setAccessible(true); // field.set(model, entry.getValue());// } } //返回码 String code = action.execute(request, response); ForwardModel forwardModel = actionModel.get(code); if(forwardModel!=null) { String jspPath = forwardModel.getPath(); if("false".equals(forwardModel.getRedirect())) { //做转发的处理 request.getRequestDispatcher(jspPath).forward(request, response); } else { response.sendRedirect(request.getContextPath()+jspPath); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }// try {// action.execute(req, resp);// } catch (Exception e) {// // TODO Auto-generated catch block// e.printStackTrace();// } } }
编写子控制器接口
Action
package com.hmc.framework;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * 子控制器 * 作用:用来直接处理浏览器发送过来的请求。 * @author * */public interface Action { String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, Exception; }
编写模型驱动接口ModelDrivern
package com.hmc.framework;/** * 模型驱动接口 * 作用是将jsp所有传递过来的参数以及参数值自动封装到浏览器所要操作的实体类中 * @author Administrator * */public interface ModelDrivern{ T getModel();}
增强版的子控制器(ActionSupport)实现上面的子控制器(Action)
package com.hmc.framework;import java.io.IOException;import java.lang.reflect.Method;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * 增强版的子控制器 * 原来的子控制器只能处理一个用户请求 * 作用: * 将一组相关的操作放到一个Action中 * @author Administrator * */public class ActionSupport implements Action{ @Override public final String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, Exception { String menthodName=request.getParameter("menthodName"); //this值的是CalAction它的一个类实例 String code=null; Method m=this.getClass().getDeclaredMethod(menthodName,HttpServletRequest.class,HttpServletResponse.class); m.setAccessible(true); code = (String) m.invoke(this,request,response); return code; }}
编写业务逻辑类CalAction
package com.hmc.web;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.hmc.entity.Cal;import com.hmc.framework.ActionSupport;import com.hmc.framework.ModelDrivern;public class CalAction extends ActionSupport implements ModelDrivern{ private Cal cal=new Cal(); public String add(HttpServletRequest request, HttpServletResponse response) throws Exception, Exception { // String num1=request.getParameter("num1");// String num2=request.getParameter("num2"); //Call cal=new Cal(Integer.valueOf(num1),Integer.valueOf(num2)); request.setAttribute("res", cal.getNum1()+cal.getNum2()); return "res"; } public String minus(HttpServletRequest request, HttpServletResponse response) throws Exception, Exception { // String num1=request.getParameter("num1");// String num2=request.getParameter("num2"); //Call cal=new Cal(Integer.valueOf(num1),Integer.valueOf(num2)); request.setAttribute("res", cal.getNum1()-cal.getNum2()); return "res"; } public String multiply(HttpServletRequest request, HttpServletResponse response) throws Exception, Exception { // String num1=request.getParameter("num1");// String num2=request.getParameter("num2"); //Call cal=new Cal(Integer.valueOf(num1),Integer.valueOf(num2)); request.setAttribute("res", cal.getNum1()*cal.getNum2()); return "res"; } public String divided(HttpServletRequest request, HttpServletResponse response) throws Exception, Exception { // String num1=request.getParameter("num1");// String num2=request.getParameter("num2"); //Call cal=new Cal(Integer.valueOf(num1),Integer.valueOf(num2)); request.setAttribute("res", cal.getNum1()/cal.getNum2()); return "res"; } @Override public Cal getModel() { // TODO Auto-generated method stub return cal; } }
以上就是自定义mvc的代码实现,下面看下演示效果
建一个jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Insert title here
结果如下
加
减
乘
除