目录
1、什么是MVC?
MVC的全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,是一种软件设计典范。它是用一种业务逻辑、数据与界面显示分离的方法来组织代码,将众多的业务逻辑聚集到一个部件里面,在需要改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑,达到减少编码的时间。
2、MVC的好处
视图层和业务层分离,这样就允许更改视图层代码而不用重新编译模型和控制器代码,同样,一个应用的业务流程或者业务规则的改变只需要改动MVC的模型层即可。因为模型与控制器和视图相分离,所以很容易改变应用程序的数据层和业务规则。
- <%@ 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>
- <h3>目前增删改查的方法</h3>
- <a href="${pageContext.request.contextPath }/book/add">增加</a>
- <a href="${pageContext.request.contextPath }/book/del">删除</a>
- <a href="${pageContext.request.contextPath }/book/edit">修改</a>
- <a href="${pageContext.request.contextPath }/book/list">查询</a>
- </body>
- </html>
- package com.zwc.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("/book/del")
- public class DelBookServlet extends HttpServlet{
- @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 {
- System.out.println("处理书籍的删除业务,调用bookbiz");
- }
- }
- package com.zwc.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("/book/list")
- public class AddBookServlet extends HttpServlet{
- @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 {
- System.out.println("处理书籍的查询业务,调用bookbiz");
- }
- }
- package com.zwc.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("/book/edit")
- public class EditBookServlet extends HttpServlet{
- @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 {
- System.out.println("处理书籍的修改业务,调用bookbiz");
- }
- }
- package com.zwc.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("/book/add")
- public class ListBookServlet extends HttpServlet{
- @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 {
- System.out.println("处理书籍的增加业务,调用bookbiz");
- }
- }

- <%@ 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>
- <h3>目前增删改查的方法</h3>
- <a href="${pageContext.request.contextPath }/book/add">增加</a>
- <a href="${pageContext.request.contextPath }/book/del">删除</a>
- <a href="${pageContext.request.contextPath }/book/edit">修改</a>
- <a href="${pageContext.request.contextPath }/book/list">查询</a>
-
- <h3>类数量过多问题的优化</h3>
- <a href="${pageContext.request.contextPath }/book.action?methodName=add">增加</a>
- <a href="${pageContext.request.contextPath }/book.action?methodName=del">删除</a>
- <a href="${pageContext.request.contextPath }/book.action?methodName=edit">修改</a>
- <a href="${pageContext.request.contextPath }/book.action?methodName=list">查询</a>
- <a href="${pageContext.request.contextPath }/book.action?methodName=load">回显</a>
- </body>
- </html>
- package com.zwc.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 {
- 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("edit".equals(methodName)) {
- edit(req,resp);
- }
- else if("list".equals(methodName)) {
- list(req,resp);
- }
- }
-
- private void list(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在同样一个Servlet中调用list方法");
-
- }
-
- private void edit(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在同样一个Servlet中调用edit方法");
- }
-
- private void del(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在同样一个Servlet中调用del方法");
- }
-
- private void add(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在同样一个Servlet中调用add方法");
- }
- private void load(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在同样一个Servlet中调用load方法");
- }
- }
运行结果

- package com.zwc.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 {
- doPost(req, resp);
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- // 为了区分当前请求的目的,增删改查的目的,就从前台将要调用的方法名传递到后台
- String methodName = req.getParameter("methodName");
- // methodName可以是add/del/list/edit/.....
- // 前台传递上面方法,就调用当前类的对应方法
- try {
- Method m = this.getClass()//BookSerlvet.class
- .getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
- m.setAccessible(true);
- // 调用当前类实例的methodName 方法
- m.invoke(this, req,resp);
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
-
- private void list(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在同样一个Servlet中调用list方法");
-
- }
-
- private void edit(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在同样一个Servlet中调用edit方法");
- }
-
- private void del(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在同样一个Servlet中调用del方法");
- }
-
- private void add(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在同样一个Servlet中调用add方法");
- }
- private void load(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在同样一个Servlet中调用load方法");
- }
- }
同样测试运行:



- package com.zwc.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.zwc.web.BookAction;
-
- /**
- * 中央控制器:
- * 主要职能:接受浏览器请求,找到对应的处理人
- * @author Administrator
- *
- */
- @WebServlet("*.action")
- public class DispatcherServlet extends HttpServlet{
- private Map<String,Action> actions = new HashMap<String,Action>();
-
- // 程序启动时,只会加载一次
- @Override
- public void init() throws ServletException {
- actions.put("/book",new BookAction());
- actions.put("/order",new BookAction());
- }
- @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 {
- //http://localhost:8080/mvc/book.action?methodName=list
- String uri = req.getRequestURI();
- // 要拿到book,最后一个/到最后一个.为止
- uri = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));
- Action action = actions.get(uri);
- action.execute(req, resp);
- }
- }
- package com.zwc.framework;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- /**
- * 子控制器:
- * 对应请求的处理人
- * @author Administrator
- *
- */
- public interface Action {
- void execute(HttpServletRequest req, HttpServletResponse resp);
- }
- package com.zwc.framework;
-
- import java.lang.reflect.Method;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- public class ActionSupport implements Action{
-
- @Override
- public void execute(HttpServletRequest req, HttpServletResponse resp) {
- // 为了区分当前请求的目的,增删改查的目的,就从前台将要调用的方法名传递到后台
- String methodName = req.getParameter("methodName");
- // methodName可以是add/del/list/edit/.....
- // 前台传递上面方法,就调用当前类的对应方法
- try {
- Method m = this.getClass()//BookSerlvet.class
- .getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
- m.setAccessible(true);
- // 调用当前类实例的methodName 方法
- m.invoke(this, req,resp);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- }
- package com.zwc.web;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import com.zwc.framework.Action;
- import com.zwc.framework.ActionSupport;
-
- public class BookAction extends ActionSupport{
-
- private void list(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在同样一个Servlet中调用list方法");
-
- }
-
- private void edit(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在同样一个Servlet中调用edit方法");
- }
-
- private void del(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在同样一个Servlet中调用del方法");
- }
-
- private void add(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在同样一个Servlet中调用add方法");
- }
- private void load(HttpServletRequest req, HttpServletResponse resp) {
- System.out.println("在同样一个Servlet中调用load方法");
- }
-
- }
运行结果:
