目录
一、自定义mvc简介
1、自定义MVC的概念?
MVC全称名是Model(模型层) View(视图层) controller(控制层),它们分工明确,各司其职,同时也具有高类聚,低耦合的特点。
2、自定义MVC的作用?
节省时间和代码。
比如说:我们在写一个项目时,都是从无到有的,创建的过程要写好多代码,还要花费好多时间。所以我们会写一个MVC来继承它,从而得到通用的效果,以至于有更多的时间与精力去完成自己的项目。
二、最初的增删改查
我们以书本的增删改查为例,先回顾一下最初是怎样部署代码的:
代码:
增删改查servlet
增加
package com.zxw.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 AddBookServlet extends HttpServlet {
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("处理书籍的增加业务、调用BookBiz");
}
}
-------------------------------------------
删除
package com.zxw.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 {
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("处理书籍的删除业务、调用BookBiz");
}
}
------------------------------------------
修改
package com.zxw.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 {
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("处理书籍的修改业务、调用BookBiz");
}
}
------------------------------------------------
查看
package com.zxw.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 ListBookServlet extends HttpServlet {
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("处理书籍的查询业务、调用BookBiz");
}
}
效果演示
三、反射版增删改查
新增一个BookServlet
package com.zxw.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 java.lang.reflect.Method;
//@WebServlet("/book.action")
public class BookServlet extends HttpServlet {
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");
//methodName可以是add/del/edit/list/load/xxx/yyy...
//前台传递什么方法、就调用当前类的对应方法
if("add".equals(methodName)) {
//如果前台传递到后台的是一个新增的请求、那么后台就调用新增方法
add(request,response);
}else if("del".equals(methodName)) {
del(request,response);
}else if("edit".equals(methodName)) {
edit(request,response);
}else if("list".equals(methodName)) {
list(request,response);
}else if("load".equals(methodName)) {
load(request,response);
}
}
private void list(HttpServletRequest request, HttpServletResponse response) {
System.out.println("在同一个servlet中调用list方法");
}
private void load(HttpServletRequest request, HttpServletResponse response) {
System.out.println("在同一个servlet中调用load方法");
}
private void edit(HttpServletRequest request, HttpServletResponse response) {
System.out.println("在同一个servlet中调用edit方法");
}
private void del(HttpServletRequest request, HttpServletResponse response) {
System.out.println("在同一个servlet中调用del方法");
}
private void add(HttpServletRequest request, HttpServletResponse response) {
System.out.println("在同一个servlet中调用add方法"); }
}
jsp代码:
<!--
上述问题
1、关于单个实体/表操作场景越多、造成了项目中类的数量过于庞大
2、当新增了业务、除了要添加该业务对应的方法(load)、同时还要改动原有的代码
3、反射相关的代码、在每一个实体类对应的Servlet中都存在
4、每一个servlet中都有doget、dopost方法
-->
<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>
当增加了业务,除了要添加该业务对应的方法(load),同时还要改动原有的代码
优化BookServlet :
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//为了区分当前请求的目的、增删改查的目的、就从前台要调用的方法名传递到后台
String methodName = request.getParameter("methodName");
//methodName可以是add/del/edit/list/load/xxx/yyy...
//前台传递什么方法、就调用当前类的对应方法
try {
Method m = this.getClass()//BooksServley.calss
.getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
//调用当前类实例的methodName方法
m.invoke(this, request,response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
效果演示:
订单类增删改查
<h3>订单类CRUD</h3>
<a href="${pageContext.request.contextPath }/order.action?methodName=add">增加</a>
<a href="${pageContext.request.contextPath }/order.action?methodName=del">删除</a>
<a href="${pageContext.request.contextPath }/order.action?methodName=edit">修改</a>
<a href="${pageContext.request.contextPath }/order.action?methodName=list">查询</a>
<a href="${pageContext.request.contextPath }/order.action?methodName=load">回显</a>
订单servlet
package com.zxw.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 java.lang.reflect.Method;
//@WebServlet("/order.action")
public class OrderServlet extends HttpServlet {
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");
//methodName可以是add/del/edit/list/load/xxx/yyy...
//前台传递什么方法、就调用当前类的对应方法
try {
Method m = this.getClass()//BooksServley.calss
.getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
//调用当前类实例的methodName方法
m.invoke(this, request,response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// if("add".equals(methodName)) {
// //如果前台传递到后台的是一个新增的请求、那么后台就调用新增方法
// add(request,response);
// }else if("del".equals(methodName)) {
// del(request,response);
// }else if("edit".equals(methodName)) {
// edit(request,response);
// }else if("list".equals(methodName)) {
// list(request,response);
// }else if("load".equals(methodName)) {
// load(request,response);
// }
}
private void list(HttpServletRequest request, HttpServletResponse response) {
System.out.println("在同一个orderservlet中调用list方法");
}
private void load(HttpServletRequest request, HttpServletResponse response) {
System.out.println("在同一个orderservlet中调用load方法");
}
private void edit(HttpServletRequest request, HttpServletResponse response) {
System.out.println("在同一个orderservlet中调用edit方法");
}
private void del(HttpServletRequest request, HttpServletResponse response) {
System.out.println("在同一个orderservlet中调用del方法");
}
private void add(HttpServletRequest request, HttpServletResponse response) {
System.out.println("在同一个orderservlet中调用add方法"); }
}
四、自定义mvch工作原理图以及后台代码实现刷新
我们接下来按照这个原理图去部署代码:
新建一个com.ycx.framework包
DispatcherServlet:
package com.zxw.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.zxw.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.acion?methodName=list
String uri = req.getRequestURI();
//要拿到/book 就是最后一个/到最后一个.的位置
uri = uri.substring(uri.lastIndexOf("/")
, uri.lastIndexOf("."));
Action action = actions.get(uri);
action.execute(req, resp);
}
}
ActionSupport:
package com.zxw.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 request, HttpServletResponse response) {
String methodName = request.getParameter("methodName");
//methodName可以是add/del/edit/list/load/xxx/yyy...
//前台传递什么方法、就调用当前类的对应方法
try {
Method m = this.getClass()//BooksServley.calss
.getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
//调用当前类实例的methodName方法
m.invoke(this, request,response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Action:
package com.zxw.framework;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 子控制器
* 对应请求的处理人
* @author Administrator
*
*/
public interface Action {
void execute(HttpServletRequest request, HttpServletResponse response);
}
在com.ycx.web包内添加一个BookAction类:
BookAction:
package com.zxw.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zxw.framework.Action;
import com.zxw.framework.ActionSupport;
public class BookAction extends ActionSupport{
private void list(HttpServletRequest request, HttpServletResponse response) {
System.out.println("在同一个servlet中调用list方法");
}
private void load(HttpServletRequest request, HttpServletResponse response) {
System.out.println("在同一个servlet中调用load方法");
}
private void edit(HttpServletRequest request, HttpServletResponse response) {
System.out.println("在同一个servlet中调用edit方法");
}
private void del(HttpServletRequest request, HttpServletResponse response) {
System.out.println("在同一个servlet中调用del方法");
}
private void add(HttpServletRequest request, HttpServletResponse response) {
System.out.println("在同一个servlet中调用add方法"); }
}
效果演示:
okk!再见