自定义mvc

1.什么是MVC?

MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,
它是一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码

MVC结构

V
jsp/ios/android
C
servlet/action
M
这样划分的好处:
简单明了,清晰化,易找错
分工明确,
核心思想:各司其职
实体域模型(名词)我们的实体类(Student)
过程域模型(动词)(dao层)驱动数据库
我们首先应该知道,做项目应该经过这几个层
web 做浏览器请求分发
service 调用dao处理项目业务的
dao 操作数据库
注1:不能跨层调用
注2:只能出现由上而下的调用
现今我们某些同学使用的开发模式:
实体类、dao、web
例如做书籍增删查改
我们会做 AddBookServlet
DelBookServlet等
配置:
web.xml
AddBookServlet
DelBookServlet
这种模式就比较繁琐,死板
然而,一些灵活点的同学,就:
只写一个
BookServlet
去里面获取,定义,建立对象,判断,设值,重定向,转发。
配置一个,这种模式比较的方便,
但是,其实这种方法,也有弊端。
1、BookServlet中的if语句判断非常多
代码比较臃肿,那么我们涉及的mvc思想来了
增强在哪里?:
**2、省去jsp传递到后台封装成对象的过程

String methodName = req.getParamater("methodName");
		String methodName = req.getParamater("bname");
		String methodName = req.getParamater("pice");
		String methodName = req.getParamater("type");
		........
		Book b = new Book();
		b.set()
		......
		

这一部分就省去了
3、省去结果集的处理

省去:

req.getResquestDispather("xxx.jsp").forward(req,resp);
		resp.sendRedirect();
		

整体来说,简洁明了,代码量清晰化。
mvc模式应运而生,
两个东西:
中央控制器
子控制器

3. 自定义MVC工作原理图

主控制动态调用子控制器调用完成具体的业务逻辑
(火车、控制台、车轨)
请求、主控制器、子控制器

总结:
主控制器:查看是否有对应的子控制器来处理用户请求,如果就调用子控制器来处理请求
;没有就报错,就处理不了请求

子控制器:就是处理用户请求用的

在这里插入图片描述

图中浏览器发送请求,(*.action),
主控制器接收(ActionServlet),处理,是否有子控制器可以处理,没有就通过View返回,报错,有就利用子控制器(继承主的)通过execute等方法,处理业务逻辑(请求),返回给浏览器

我们来用代码演示一波:
建立一个主控制器:
用来分发请求
在这里插入图片描述
子控制器:
在这里插入图片描述
子控制器里面的cute方法:以及具体附属方法(继承)

public interface Action {
	void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException; 
}

不需要继承,只是定义,专门处理业务逻辑的。
假如我们写一个计算器的方法:
在写一个实体类:

package com.ly.entity;

public class Cal {
private String num1;
private String num2;
public String getNum1() {
	return num1;
}
public void setNum1(String num1) {
	this.num1 = num1;
}
public String getNum2() {
	return num2;
}
public void setNum2(String num2) {
	this.num2 = num2;
}
public Cal(String num1, String num2) {
	super();
	this.num1 = num1;
	this.num2 = num2;
}
public Cal() {
	super();
}

}

jsp文件:

<%@ 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>
<form action="">
	num1:<input type="text" name="num1"><br>
	num2:<input type="text" name="num2"><br>
	<input type="submit">
</form>
</body>
</html>

这基本就是我们的请求了,
接着,我们发送到了主控制器
配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>T224_mvc</display-name>
  <servlet>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<servlet-class>com.ly.framework.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>dispatcherServlet</servlet-name>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>

应该在中央控制器包含子控制器
必然有map,

private Map<String, Action> actionMap = new  HashMap<>();
String图中的*号。Action子控制器
actionMap  所有的子控制器

在主控制器里面写初始方法:

public void init() {
		actionMap.put("addCal", new AddCalAction());
	}

子控制器里的附属子控制器方法(继承)

接下来,获取***.action(URL)

String url = req.getRequestURI();//T224_mvc/.xxx.action

但我们只需要***
截取

url = url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));

通过map去取值:

Action action = actionMap.get(url);

调用execute方法

action.execute(req, resp);

进子控制器处理业务逻辑

package com.ly.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ly.framework.Action;

public class AddCalAction implements Action {

	@Override
	public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String num1 = req.getParameter("num1");
		String num2 = req.getParameter("num2");
		req.setAttribute("res", Integer.valueOf(num1)+ Integer.valueOf(num2));
		req.getRequestDispatcher("calRes.jsp").forward(req, resp);
		
	}

}

<form action="${pageContext.request.contextPath }/addCal.action">
结果:${res }

在这里插入图片描述

在这里插入图片描述
我们拿加减乘除练一下:
前台:

<%@ 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>
<script type="text/javascript">
	function doSub(val) {
		if(val ==1){
			calForm.action = "${pageContext.request.contextPath }/addCal.action";
		}else if(val ==2){
			calForm.action = "${pageContext.request.contextPath }/delCal.action";
		}else if(val ==3){
			calForm.action = "${pageContext.request.contextPath }/ppCal.action";
		}else if (val ==4){
			calForm.action = "${pageContext.request.contextPath }/chuCal.action";
		}
		calForm.submit();
	}
</script>
</head>
<body>
<form id="calForm" name="calForm" action="${pageContext.request.contextPath }/addCal.action">
	num1:<input type="text" name="num1"><br>
	num2:<input type="text" name="num2"><br>
	<button onclick="doSub(1)">+</button>
	<button onclick="doSub(2)">-</button>
	<button onclick="doSub(3)">*</button>
	<button onclick="doSub(4)">/</button>
	
</form>
</body>
</html>

字控制器:::::

package com.ly.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ly.framework.Action;

public class AddCalAction implements Action {

	@Override
	public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String num1 = req.getParameter("num1");
		String num2 = req.getParameter("num2");
		req.setAttribute("res", Integer.valueOf(num1)+ Integer.valueOf(num2));
		req.getRequestDispatcher("calRes.jsp").forward(req, resp);
		
	}

}

public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String num1 = req.getParameter("num1");
		String num2 = req.getParameter("num2");
		req.setAttribute("res", Integer.valueOf(num1)- Integer.valueOf(num2));
		req.getRequestDispatcher("calRes.jsp").forward(req, resp);
		
	}

乘:

public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String num1 = req.getParameter("num1");
		String num2 = req.getParameter("num2");
		req.setAttribute("res", Integer.valueOf(num1)* Integer.valueOf(num2));
		req.getRequestDispatcher("calRes.jsp").forward(req, resp);
		
	}

除:

public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String num1 = req.getParameter("num1");
		String num2 = req.getParameter("num2");
		req.setAttribute("res", Integer.valueOf(num1)/ Integer.valueOf(num2));
		req.getRequestDispatcher("calRes.jsp").forward(req, resp);
		
	}

中央控制器:

        actionMap.put("/addCal", new AddCalAction());
		actionMap.put("/delCal", new DelCalAction());
		actionMap.put("/ppCal", new PpCalAction());
		actionMap.put("/chuCal", new ChuCalAction());

在这里插入图片描述
加:
在这里插入图片描述
减:
在这里插入图片描述
乘:
在这里插入图片描述
除:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值