カスタムMVC(A)

1. MVCは何ですか
ビュー(ビュー) - - MVCフルネームモデルビューコントローラ、モデル(モデル)の略コントローラ(コントローラ)、そして
それは、サービスロジック、データ、ディスプレイ・インターフェースで、ソフトウェアの設計モデルであります組織コードを分離する方法

MODEL1のJSP + JDBC

MODEL2 - > MVC

核となるアイデア:その職務を遂行

2. MVC構造

V
JSP / iOS版/ Androidの
C
サーブレット/アクション
M
エンティティドメインモデル(名詞)
プロセス・ドメイン・モデル(動詞)

Webブラウザが要求し配布しない
サービスコール処理DAOプロジェクト運営の
DAOのデータベース操作を

注1:あなたがいないクロス層の呼び出しが可能
注2:あなただけのトップダウンの外観を呼び出すことができます

図3カスタムMVCは、動作
主制御サブコントローラは、動的に特定のサービスコールの完了ロジック呼び出す
(列車、コンソール、レール)
要求、メインコントローラ、サブコントローラ

4.回路図MVCのカスタム
ここに画像を挿入説明
マスタコントローラ:

package com.luoxiaogang.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.zhuyuncong.web.AddCalAction;
import com.zhuyuncong.web.CccCalAction;
import com.zhuyuncong.web.ChuCalAction;
import com.zhuyuncong.web.DelCalAction;

/**
 * 中央控制器
 *  作用:接收请求,通过请求寻找处理请求对应的子控制器
 * @author MACHENIKE
 *
 */
public class DispatcherServlet extends HttpServlet {

	private static final long serialVersionUID = 9213618038567713003L;

	private Map<String, Action> actionMap = new  HashMap<>();
	
	public void init() {
		actionMap.put("/addCal", new AddCalAction());
		actionMap.put("/delCal", new DelCalAction());
		actionMap.put("/cccCal", new CccCalAction());
		actionMap.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 = actionMap.get(url);
		action.execute(req, resp);
	}
}

サブコントローラ。

package com.luoxiaogang.framework;

import java.io.IOException;

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

/**
 * 子控制器
 * 	作用:用来直接处理浏览器发送过来的请求
 * @author MACHENIKE
 *
 */
public interface Action {
   String execute(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException; 
}

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 mya(n) {
		if(n == 1){
			calForm.action="${pageContext.request.contextPath }/addCal.action";
		}else if(n == 2){
			calForm.action="${pageContext.request.contextPath }/delCal.action";
		}else if(n == 3){
			calForm.action="${pageContext.request.contextPath }/cccCal.action";
		}else if(n == 4){
			calForm.action="${pageContext.request.contextPath }/chuCal.action";
		}
		calForm.submit();
	}
</script>
</head>
<body>
<form name="calForm" action="" method="post">
	num1:<input type="text" name="num1"><br>
	num2:<input type="text" name="num2"><br>
	<input type="button" value="+" onclick="mya(1)">
	<input type="button" value="-" onclick="mya(2)">
	<input type="button" value="*" onclick="mya(3)">
	<input type="button" value="/" onclick="mya(4)">
</form>
</body>
</html>

サブコントローラの処理を追加します。

package com.luoxiaogang.web;

import java.io.IOException;

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

import com.zhuyuncong.framework.Action;

public class AddCalAction implements Action {

	@Override
	public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String num1 = req.getParameter("num1");
		String num2 = req.getParameter("num2");
		req.setAttribute("res", Integer.valueOf(num1)+Integer.valueOf(num2));
		req.getRequestDispatcher("res.jsp").forward(req, resp);;
		return null;
	} 
}

サブコントローラは、減算を処理します。

package com.luoxiaogang.web;

import java.io.IOException;

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

import com.zhuyuncong.framework.Action;

public class DelCalAction implements Action {

	@Override
	public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String num1 = req.getParameter("num1");
		String num2 = req.getParameter("num2");
		req.setAttribute("res", Integer.valueOf(num1)-Integer.valueOf(num2));
		req.getRequestDispatcher("res.jsp").forward(req, resp);;
		return null;
	}

}

サブコントローラは、乗算を処理します。

package com.luoxiaogang.web;

import java.io.IOException;

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

import com.zhuyuncong.framework.Action;

public class CccCalAction implements Action {

	@Override
	public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String num1 = req.getParameter("num1");
		String num2 = req.getParameter("num2");
		req.setAttribute("res", Integer.valueOf(num1)*Integer.valueOf(num2));
		req.getRequestDispatcher("res.jsp").forward(req, resp);;
		return null;
	}

}

サブ分割プロセスコントローラ:

package com.zhuyuncong.web;

import java.io.IOException;

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

import com.zhuyuncong.framework.Action;

public class ChuCalAction implements Action {

	@Override
	public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String num1 = req.getParameter("num1");
		String num2 = req.getParameter("num2");
		req.setAttribute("res", Float.parseFloat(num1)/Float.parseFloat(num2));
		req.getRequestDispatcher("res.jsp").forward(req, resp);;
		return null;
	}

}

概要:
メインコントローラ:あなたが要求を処理するためにサブコントローラを呼び出す場合、ユーザー要求を処理するためのサブコントローラがある場合
、エラーではありませんが、それは要求を処理できません

サブコントローラは、ユーザ要求で処理します

おすすめ

転載: blog.csdn.net/weixin_45177212/article/details/93741304