自定义mvc框架(1)

自定义mvc框架(1)

1. 什么是MVC

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

Model2 ->MVC

核心思想:各司其职

2. MVC结构

V
jsp/ios/android
C
servlet/action
M
实体域模型(名词)
过程域模型(动词)

3. 自定义MVC工作原理图

主控制动态调用子控制器调用完成具体的业务逻辑
(火车、控制台、车轨)
请求、主控制器、子控制器
在这里插入图片描述


下面展示一些 内联代码片
接口Action

package com.lrc.framework;

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

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

实体类Cal


package com.lrc.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();
	}
	
	
}

DispatcherServlet


package com.lrc.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 com.lrc.web.AddCalAction;
import com.lrc.web.CheCalAction;
import com.lrc.web.ChuCalAction;
import com.lrc.web.DelCalAction;

public class DispatcherServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Map<String, Action> map=new HashMap<String, Action>();
	
	public void init() {
		map.put("/addCal", new AddCalAction());
		map.put("/delCal", new DelCalAction());
		map.put("/cheCal", new CheCalAction());
		map.put("/chuCal", 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 {
		init();
		String url= req.getRequestURI();
		url=url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
		//Action action=new AddCalAction
		Action action=map.get(url);//这行代码就相当于上面的那行
		try {
			action.execute(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
	}
	
}

web.xml配置文件


<?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>mvc</display-name>
  <servlet>
  <servlet-name>dispatcherServlet</servlet-name>
  <servlet-class>com.lrc.framework.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>dispatcherServlet</servlet-name>
  <url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>

AddCalAction(加法)实现接口(Action)


package com.lrc.web;

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

import com.lrc.framework.Action;

public class AddCalAction implements Action{

	@Override
	public void execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		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);
		
	}

}

DelCalAction(减法)实现接口(Action)


package com.lrc.web;

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

import com.lrc.framework.Action;

public class DelCalAction implements Action{

	@Override
	public void execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		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);
		
	}

}

ChrCalAction(乘法)实现接口(Action)


package com.lrc.web;

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

import com.lrc.framework.Action;

public class CheCalAction implements Action{

	@Override
	public void execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		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);
		
	}

}

chuCalAction(除法)实现接口(Action)


package com.lrc.web;

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

import com.lrc.framework.Action;

public class ChuCalAction implements Action{

	@Override
	public void execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		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);
		
	}

}

calRes.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>
结果:${res }
</body>
</html>

cal.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>
<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 }/cheCal.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>

代码效果显示如下
在这里插入图片描述
加法
在这里插入图片描述
减法
在这里插入图片描述
乘法
在这里插入图片描述
除法
在这里插入图片描述
总结:
主控制器:查看是否有对应的子控制器来处理用户请求,如果就调用子控制器来处理请求;没有就报错,就处理不了请求
子控制器:就是处理用户请求用的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值