一、MVC简介
Model View Controller 简称 MVC,由模型(Model),视图(View),控制器(Controller)的组成,是一种软件设计思想。
作用:MVC不能提升程序执行效率,但是会提升开发效率,提高代码的重用
二、自定义MVC工作原理图
三、自定义MVC后台代码实现
案例
DispatcherServlet .java( 中央控制器 )
package com.zsm.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.zsm.servlet.BookAction;
/**
* 中央控制器
* 主要职能:接收浏览器请求,找到对应的处理人
* 不处理任何业务逻辑,只接收请求
* @author zjjt
*/
@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://location:8080/mvc/book.action?me....
String uri = req.getRequestURI();//路径
// 要拿到/book,就是最后一个/到最会一个.的位置
uri = uri.substring(uri.lastIndexOf("/"),
uri.lastIndexOf("."));
Action action = actions.get(uri);
action.execute(req, resp);
}
}
Action.java(子控制器,接口)
package com.zsm.framework;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
*子控制器
*对应请求的处理人
* @author zjjt
*
*/
public interface Action {
//封装继承多态
void execute(HttpServletRequest req, HttpServletResponse resp);
}
ActionSupport .java(implements Action)
package com.zsm.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/...
//前台传递什么方法,就调用当前类对应的方法
try {
Method m = this.getClass()//相当于BookServlet
.getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
//调用当前类实例的methodName方法 传什么就掉什么方法
m.invoke(this, req,resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
BookAction .java
package com.zsm.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zsm.framework.ActionSupport;
public class BookAction extends ActionSupport {
private void lod(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在同一个servlet中调用 回显");
}
private void select(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在同一个servlet中调用 增加");
}
private void update(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在同一个servlet中调用 修改");
}
private void del(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在同一个servlet中调用 删除");
}
private void add(HttpServletRequest req, HttpServletResponse resp) {
System.out.println("在同一个servlet中调用 查看 ");
}
}
demo1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>增删还查</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=update">修改</a>
<a href="${pageContext.request.contextPath }/order.action?methodName=select">查看</a>
<a href="${pageContext.request.contextPath }/order.action?methodName=lod">回显</a>
</body>
</html>
注意:
(1)不能跨层调用;
(2)只能由上往下进行调用;View -> Controller -> Model