当前位置:网站首页>自定义MVC 1.0
自定义MVC 1.0
2022-07-24 05:18:00 【zsm030616】
一、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
边栏推荐
- 数据类型概括
- special effects - 鼠标点击,自定义 DOM 跟随移动
- 聊聊强缓存与协商缓存
- Modify jupyter save path
- C2 random generation function seed, numpy. Random. Seed(), TF. Random. Set_ Seed Learning + reprint and sorting
- day(0~6)代表每月第一天起始位置,stop代表每月天数,每天之间空两个空格。输入不同的day和stop,输出每月日历的样子。假设day为2,stop为31,则输出样式为
- Opengl在屏幕上绘制一个锥体,该锥体有四个面,每个面都是三角形。为该锥体添加光照和纹理效果
- 赶紧进来!!带你了解什么是多文件,并轻松掌握 extern和static c语言关键字的用法!!!
- canvas - 旋转
- 过渡 效果
猜你喜欢
随机推荐
Some thoughts on being a professional
Function_ generalization
02 mobile terminal page adaptation
JS - 鼠标键盘配置 及 浏览器禁止操作
Sorting out some common server instructions and some binding instructions in csgo
select_ Render small phenomena
C语言从入门到入土(一)
Detailed explanation of string constant pool and intern() of string
根据数组中对象的某个属性值进行排序
OpenGL draws a cone on the screen, which has four faces, each of which is a triangle. Add lighting and texture effects to the cone
[common skills]
index为什么不能作为v-for的key?
flex布局
构造函数_Map构造函数
函数_this关键字
自定义MVC 2.0
Scope and scope chain
Promise_ Async and await
Ain 0722 sign in
canvas - 填充








