自定义MVC(一)

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

Model1 jsp+jdbc

Model2 ->MVC

核心思想:各司其职

在这里插入图片描述
在这里插入图片描述

案例:利用自定义MVC原理完成计算器

主界面:包含中央控制器

package com.az.framework;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.ActionMap;

import com.az.web.AddCalAction;
import com.az.web.CfCalAction;
import com.az.web.ChuCalAction;
import com.az.web.DelCalAction;

/**
 * 中央控制器
 * 作用:
 * 接受用户请求,通过用户请求的url寻找指定的子控制器去处理业务
 * @author zuo.fan
 *
 */
public class DispatcherSerclet extends HttpServlet {

	private static final long serialVersionUID = -1292466773695745143L;
	private Map<String, Action> actionMap = new HashMap<>();
	public void intn() {
//		http://localhost:8080/az_mvc1/cal_add.action
		actionMap.put("/cal_add", new AddCalAction());
		actionMap.put("/cal_del", new DelCalAction());
		actionMap.put("/cal_cf", new CfCalAction());
		actionMap.put("/cal_chu", new ChuCalAction());
	}
	@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 {
		intn();
		String url = req.getRequestURI();
		url = url.substring(url.lastIndexOf("/"),url.lastIndexOf("."));
//		AddCalAction action = (AddCalAction) actionMap.get(url);
//		Action a = (Action)action;
	   Action action = actionMap.get(url);
	   action.execute(req, resp);
	}

}

参数类:Cal

package com.az.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;
}
@Override
public String toString() {
	return "Cal [num1=" + num1 + ", num2=" + num2 + "]";
}
public Cal(String num1, String num2) {
	super();
	this.num1 = num1;
	this.num2 = num2;
}
public Cal() {
	super();
}


}

子控制器:Action

package com.az.framework;

import java.io.IOException;

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

/**
 * 子控制器
 * 作用:具体处理用户请求的类(实现了Action接口的类)
 * @author zuo.fan
 *
 */

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

}

web

<?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>az_mvc1</display-name>
  
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>com.az.framework.DispatcherSerclet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>

</web-app>

运算结果显示页面:rs

<%@ 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>
结果: ${rs}
</body>
</html>

编写加减乘除算法

package com.az.web;

import java.io.IOException;

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

import com.az.entity.Cal;
import com.az.framework.Action;

public class AddCalAction implements Action {

	@Override
	public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		String num1 = req.getParameter("num1");
		String num2 = req.getParameter("num2");
		Cal cal = new Cal(num1, num2);
		req.setAttribute("rs", Integer.valueOf(cal.getNum1())+Integer.valueOf(cal.getNum2()));
		req.getRequestDispatcher("/rs.jsp").forward(req, resp);
		return null;
		
	}

}

在这里插入图片描述
修改代码中的运算符就好
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值