1.什么是mvc:
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范。用一种业务逻辑、数据、界面显示分离的方法,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。
2.mvc框架的定义:
avaMVC(Model-View-Controller)框架是一种用于构建Java应用程序的软件设计模式。它将一个应用程序的逻辑分为三个主要部分:模型(Model)、视图(View)和控制器(Controller)。
- Model:是应用程序中用于处理应用程序数据逻辑的部分,通常模型对象负责在数据库中存取数据。
- View:是应用程序中处理数据显示的部分,通常视图是依据模型数据创建的。
- Controller:是应用程序中处理用户交互的部分,通常控制器负责从视图读取数据,控制用户输入,并向模型发送数据。
MVC是一个框架模式,它强制性的使应用程序的输入、处理和输出分开。使用MVC应用程序被分成三个核心部件:模型、视图、控制器。它们各自处理自己的任务。最典型的MVC就是JSP + servlet + javabean的模式
- Model:常用javabean去实现,通过各种类来对数据库的数据进行获取,并封装在对象当中。
- View:常用JSP来实现,通过可直接观察的JSP页面来展示我们从数据库中获取的数据。
- Controller:常用servlet来实现,通过servlet来获取经过javabean包装过的对象(已存入数据库中的数据),然后再发送数据传输到JSP界面。
1)不能跨层调用; 2)只能由上往下进行调用:View -> Controller -> Model
3.Mvc与三层架构的区别:
- 三层架构是基于业务逻辑来分的,而MVC是基于页面来分的;
- 三层是种软件架构,通过接口实现编程,MVC模式是一种复合设计模式,一种解决方案;
- 三层架构模式是体系结构模式,MVC是设计模式;
- 三层架构模式又可归于部署模式,MVC可归于表示模式。
4.mvc三层架构代码的演化:
三层架构代码演化:
困难一点:4个servlet:
弊端:每一张表对应的每一个操作,都要写一个servlet类来处理,但是结果都是一样的
增加:
package com.xiaoye.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.add"); } }
删除:
package com.xiaoye.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("bookDelServlet.del"); } }
修改:
package com.xiaoye.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("bookUpdServlet.upd"); } }
查询:
package com.xiaoye.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("bookListServlet.list"); } }
容易一点:一个servlet:
优点:出了错误只需要在一个servlet里面查错,但是结果都是一样的
弊端:虽然每一张表对应的每一个操作,只要写一个servlet类来处理,但是每增加一个操作都需要改变原有的代码块,换句话说要增加条件分支
package com.xiaoye.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; import sun.awt.geom.AreaOp.AddOp; @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); } } private void list(HttpServletRequest req, HttpServletResponse resp) { System.out.println("BookServlet.add"); } private void upd(HttpServletRequest req, HttpServletResponse resp) { System.out.println("BookServlet.upd"); } private void del(HttpServletRequest req, HttpServletResponse resp) { System.out.println("BookServlet.del"); } private void add(HttpServletRequest req, HttpServletResponse resp) { System.out.println("BookServlet.list"); } }
使用反射方法:
弊端:虽然解决了if条件分支代码冗余的问题,但是放到项目的范围内,反射的代码是重复的
package com.xiaoye.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; import sun.awt.geom.AreaOp.AddOp; @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); // } try { Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class); m.setAccessible(true); m.invoke(this, req,resp); } catch (Exception e) { e.printStackTrace(); } } private void list(HttpServletRequest req, HttpServletResponse resp) { System.out.println("BookServlet.add"); } private void upd(HttpServletRequest req, HttpServletResponse resp) { System.out.println("BookServlet.upd"); } private void del(HttpServletRequest req, HttpServletResponse resp) { System.out.println("BookServlet.del"); } private void add(HttpServletRequest req, HttpServletResponse resp) { System.out.println("BookServlet.list"); } }
输出结果:三个输出结果都一样
5.mvc框架的初步实现:
自定义MVC工作原理:
使用MVC框架:
解决代码反射重复问题
写一个DispathServlet对应图中的ActionServlet:中央控制器
package com.xiaoye.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.xiaoye.web.BookAction; /** * 对应图中的ActionServlet:中央控制器 * @author bing人 * @com.xiaoye.framework * @DispathServlet */ @WebServlet("*.action") public class DispathServlet 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 uri = req.getRequestURI(); uri =uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf(".")); Action action = actionMap.get(uri); action.execut(req, resp); } }
再写一个Ation:子控制器 真正做事,处理浏览器发送的请求的类
package com.xiaoye.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 bing人 * @com.xiaoye.framework * @Action */ public class Action { protected void execut(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) { e.printStackTrace(); } } }
再写一个BookAction继承子控制器Action:
package com.xiaoye.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.xiaoye.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"); } }
输出结果:
输出结果: