自定义mvc

简介: 自定义mvc
  • mvc框架的定义
  • mvc三层架构代码的演变
  • mvc框架初步实现

mvc框架的定义

1.什么是mvc?

MVC开始是存在于桌面程序中的,M是指业务模型,V是指用户界面,C则是控制器,使用MVC的目的是将M和V的实现代码分离,从而使同一个程序可以使用不同的表现形式。比如一批统计数据可以分别用柱状图、饼图来表示。C存在的目的则是确保M和V的同步,一旦M改变,V应该同步更新。


2.三层架构和mvc的区别

三层架构与MVC架构的区别有:三层架构的体系结构是线性的,MVC是三角形的;在层级方面三层中的每个层都与网络分离而MVC每个层之间都是相关的;三层是架构风格,MVC是架构设计模式


mvc三层架构代码的演变

1.第一个版本(写四个servlet)

弊端:每一张表对应的每一个操作,都要写一个servlet类来处理

package com.zhulinjun.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 {
  private static final long serialVersionUID = 1L;
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doPost(request, response);
  }
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("bookAddServlet.add..");
  }
}
package com.zhulinjun.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 {
  private static final long serialVersionUID = 1L;
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doPost(request, response);
  }
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("bookDelServlet.del..");
  }
}
package com.zhulinjun.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 {
  private static final long serialVersionUID = 1L;
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doPost(request, response);
  }
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("bookUpdServlet.upd..");
  }
}
package com.zhulinjun.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 {
  private static final long serialVersionUID = 1L;
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  doPost(request, response);
  }
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("bookUpdServlet.list..");
  }
}
<%@ 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>
<a href="bookAdd.action">增加</a>
<a href="bookDel.action">删除</a>
<a href="bookUpd.action">修改</a>
<a href="bookList.action">查询</a>
</body>
</html>

2.第二个版本(写一个servlet)

弊端:虽然每一张表对应的每一个操作,只要写一个servlet类来处理,但是每增加一个操作,都需要改变原有代码块,换句话说要增加条件分支

package com.zhulinjun.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.action")
public class bookServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
  }
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String methodName = request.getParameter("methodName");
    if("add".equals(methodName)) {
      add(request,response);
    }else if("del".equals(methodName)) {
      del(request,response);
    }else if("upd".equals(methodName)) {
      upd(request,response);
    }else if("list".equals(methodName)) {
      list(request,response);
    }
  }
  private void list(HttpServletRequest request, HttpServletResponse response) {
    System.out.println("bookAddServlet.list..");
  }
  private void upd(HttpServletRequest request, HttpServletResponse response) {
    System.out.println("bookDelServlet.upd..");
  }
  private void del(HttpServletRequest request, HttpServletResponse response) {
    System.out.println("bookUpdServlet.del..");   
  }
  private void add(HttpServletRequest request, HttpServletResponse response) {
    System.out.println("bookListServlet.add..");    
  }
}
<%@ 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>版本2</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>

3.版本3

弊端:虽然解决了if条件代码冗余的问题,但是放到项目的范围内,反射的代码是重复的

package com.zhulinjun.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 {
  private static final long serialVersionUID = 1L;
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
  }
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String methodName = request.getParameter("methodName");
    try {
      Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
      m.setAccessible(true);
      m.invoke(this, request,response);
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  private void load(HttpServletRequest request, HttpServletResponse response) {
    // TODO Auto-generated method stub
  }
  private void list(HttpServletRequest request, HttpServletResponse response) {
    System.out.println("bookAddServlet.list..");
  }
  private void upd(HttpServletRequest request, HttpServletResponse response) {
    System.out.println("bookDelServlet.upd..");
  }
  private void del(HttpServletRequest request, HttpServletResponse response) {
    System.out.println("bookUpdServlet.del..");   
  }
  private void add(HttpServletRequest request, HttpServletResponse response) {
    System.out.println("bookListServlet.add..");    
  }
}
<%@ 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>版本3</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>

mvc框架初步实现

1.版本4

package com.zhulinjun.framework;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletConfig;
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.zhulinjun.web.bookAction;
/**
 * 对应图中的ActionServlet:中央控制器
 * @author 朱
 */
@WebServlet("*.action")
public class DispatcherServlet extends HttpServlet{
public Map<String, Action> actionMap=new HashMap<String, Action>();
@Override
public void init(ServletConfig config) throws ServletException {
  actionMap.put("/book", new bookAction());
  super.init(config);
}
@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 {
//http://locahost:80/mvc/book.action?methodName=add
String uri = req.getRequestURI();
uri= uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
Action action = actionMap.get(uri);
action.excute(req, resp);
}
}
package com.zhulinjun.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zhulinjun.framework.Action;
public class bookAction extends Action{
  public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("bookAction.add..");
    }
  public void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("bookAction.del..");
    }
  public void upd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("bookAction.upd..");
    }
  public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("bookAction.list..");
    }
}
package com.zhulinjun.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 朱
 *
 */
public class Action {
  protected void excute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String methodName = request.getParameter("methodName");   
    try {
      Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
      m.setAccessible(true);
      m.invoke(this, request,response);
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}
<%@ 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>版本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>
目录
相关文章
|
5月前
|
XML 开发框架 前端开发
J2EE之自定义MVC框架知识(中篇)
J2EE之自定义MVC框架知识(中篇)
57 0
|
5月前
|
存储 前端开发 数据可视化
自定义MVC(上)
自定义MVC(上)
48 1
|
10天前
|
前端开发 Java
自定义mvc的增删改查
自定义mvc的增删改查
37 0
|
4月前
|
XML 前端开发 数据格式
自定义MVC引用XML配置文件实现
自定义MVC引用XML配置文件实现
41 0
|
4月前
|
设计模式 前端开发 搜索推荐
自定义mvc框架
自定义mvc框架
35 0
|
4月前
|
XML 前端开发 数据格式
自定义MVC超详细易懂----增删改查
自定义MVC超详细易懂----增删改查
|
4月前
|
存储 前端开发 架构师
自定义MVC实现 很详细(下)---优化版
自定义MVC实现 很详细(下)---优化版
|
4月前
|
存储 设计模式 前端开发
自定义MVC实现
自定义MVC实现
|
4月前
|
安全 Java
自定义mvc----增删改查终极篇
自定义mvc----增删改查终极篇
23 0
|
4月前
|
XML 设计模式 前端开发
自定义MVC---引用XML
自定义MVC---引用XML
35 0