1.什么是MVC:
MVC的全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,是一种软件设计典范。它是用一种业务逻辑、数据与界面显示分离的方法来组织代码,将众多的业务逻辑聚集到一个部件里面,在需要改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑,达到减少编码的时间
MVC结构
- Model:是应用程序中用于处理应用程序数据逻辑的部分,通常模型对象负责在数据库中存取数据。
- View:是应用程序中处理数据显示的部分,通常视图是依据模型数据创建的。
- Controller:是应用程序中处理用户交互的部分,通常控制器负责从视图读取数据,控制用户输入,并向模型发送数据。
2.我们为什么要用MVC
MVC是一个框架模式,它强制性的使应用程序的输入、处理和输出分开。使用MVC应用程序被分成三个核心部件:模型、视图、控制器。它们各自处理自己的任务。最典型的MVC就是JSP + servlet + javabean的模式
3.目标:
理解并熟悉自定义MVC框架工作原理
1.mvc框架定义
2.分析S阶段MVC框架的代码的组成及缺陷
组成
BookServlet:
package com.zhanghao.web; import java.io.IOException; import java.lang.reflect.Method; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/book.action") public class BookServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String methodName = req.getParameter("methodName"); // if ("add".equals(methodName)) { // add(req, resp); // }else if ("del".equals(methodName)) { // del(req,resp); // }else if("upd".equals(methodName)){ // upd(req, resp); // }else if("list".equals(methodName)) { // list(req,resp); // }else if ("load".equals(methodName)) { // load(req,resp); // } try { Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class); m.setAccessible(true); m.invoke(this, req,resp); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void load(HttpServletRequest req, HttpServletResponse resp) { // TODO Auto-generated method stub } private void list(HttpServletRequest req, HttpServletResponse resp) { System.out.println("BookaddServlet.lisy..."); } private void upd(HttpServletRequest req, HttpServletResponse resp) { System.out.println("BookaddServlet.upd..."); } private void del(HttpServletRequest req, HttpServletResponse resp) { System.out.println("BookaddServlet.del..."); } private void add(HttpServletRequest req, HttpServletResponse resp) { System.out.println("BookaddServlet.add..."); } }
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <p>版本1 </p> 弊端:每一张表对应的每一个操作,都要写一个servlet类来处理<hr> <a href="bookAdd.action">增加</a> <a href="bookDel.action">删除</a> <a href="bookUpd.action">修改</a> <a href="bookList.action">查询</a> <p>版本2</p> 弊端:虽然每一张表对应的每一个操作,只要写一个servlet类来处理,但是每增加一个操作,都需要改变原有的代码块,换句话说就是增加条件分支<hr> <a href="book.action?methodName=add">增加</a> <a href="book.action?methodName=del">删除</a> <a href="book.action?methodName=upd">修改</a> <a href="book.action?methodName=list">查询</a> <p>版本3</p> 弊端:虽然解决if条件分支代码冗余的问题,但是放到项目范围内,反射的代码是重复的<hr> <a href="book.action?methodName=add">增加</a> <a href="book.action?methodName=del">删除</a> <a href="book.action?methodName=upd">修改</a> <a href="book.action?methodName=list">查询</a> <p>版本4</p> <a href="book.action?methodName=add">增加</a> <a href="book.action?methodName=del">删除</a> <a href="book.action?methodName=upd">修改</a> <a href="book.action?methodName=list">查询</a> </body> </html>
BookUpdServlet
package com.zhanghao.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/bookUpd.action") public class BookUpdServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("BookaddServlet.upd..."); } }
BookListServlet
package com.zhanghao.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/bookList.action") public class BookListServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("BookaddServlet.list..."); } }
BookDelServlet
package com.zhanghao.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/bookDel.action") public class BookDelServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("BookaddServlet.del..."); } }
BookAddServlet
package com.zhanghao.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/bookAdd.action") public class BookAddServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("BookaddServlet"); } }
BookAction
package com.zhanghao.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.zhanghao.framework.Action; public class BookAction extends Action{ public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("BookaddServlet.add..."); } public void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("BookaddServlet.del..."); } public void upd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("BookaddServlet.upd..."); } public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("BookaddServlet.list..."); } }
DispatherServlet:
DispatherServlet 他就是中央控制器
package com.zhanghao.framework; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.zhanghao.web.BookAction; /** * 对应图中的ctionServlet:中央控制器 * @author zhanghao * */ @WebServlet("*.action") public class DispatherServlet extends HttpServlet{ public Map<String,Action > actionMap=new HashMap<String, Action>(); @Override public void init() throws ServletException { actionMap.put("/book", new BookAction()); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String methodName = req.getParameter("methodName"); // http://localhost:80/mvc/book.action?methodName=add // http://localhost:80/mvc/order.action?methodName=add // book -> BookAction -> action String uri = req.getRequestURI(); uri=uri.substring(uri.lastIndexOf("/"),uri.indexOf(".")); Action action = actionMap.get(uri); action.execute(req,resp); } }
Action:子控制器
package com.zhanghao.framework; import java.io.IOException; import java.lang.reflect.Method; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 子控制器 * 真正做事,处理浏览器发送的请求的类 * @author Administrator * */ public class Action { protected void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String methodName = req.getParameter("methodName"); try { Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class); m.setAccessible(true); m.invoke(this, req,resp); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
4.如何利用自定义MVC框架的思想优化S阶段的三层结构
- 三层架构是基于业务逻辑来分的,而MVC是基于页面来分的;
- 三层是种软件架构,通过接口实现编程,MVC模式是一种复合设计模式,一种解决方案;
- 三层架构模式是体系结构模式,MVC是设计模式;
- 三层架构模式又可归于部署模式,MVC可归于表示模式。
总结:
程序设计使用MVC等设计模式,最终目的就是达到天人合一的“高内聚,低耦合”,甚至零耦合