自定义mvc框架

1.什么是MVC

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

Model1 jsp+jdbc

Model2 ->MVC

核心思想:各司其职
自定义mvc解决的问题
在这里插入图片描述

2. MVC结构

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

3. 自定义MVC工作原理图

在这里插入图片描述

4. 通过XML对自定义mvc框架进行增强

4.1 将Action的信息配置到xml(反射实例化)

4.2 通过结果码控制页面的跳转

4.3 将一组相关的操作放到一个Action中(反射调用方法)
DispatcherAction
methodName:add/minus/mul/div
CalAction extends DispatcherAction
提供一组与execute方法的参数、返回值相同的方法,只有方法名不一样

4.4 利用ModelDriver接口对Java对象进行赋值(反射读写方法)
BeanUtils.populate(calBean, parameterMap);
ModelDriver接口返回的对象不能为空

4.5 使得框架的配置文件可变
Jar包
在这里插入图片描述
工具类
ActionModel

package com.bk201.framework;

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

/**
 * 用来描述action标签
 * @author Administrator
 *
 */
public class ActionModel implements Serializable{

	private static final long serialVersionUID = 6145949994701469663L;
	
	private Map<String, ForwardModel> forwardModels = new HashMap<String, ForwardModel>();
	
	private String path;
	
	private String type;
	
	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void put(ForwardModel forwardModel){
		forwardModels.put(forwardModel.getName(), forwardModel);
	}
	
	public ForwardModel get(String name){
		return forwardModels.get(name);
	}
	
}

ConfigModel

package com.bk201.framework;

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

/**
 * 用来描述config标签
 * @author Administrator
 *
 */
public class ConfigModel implements Serializable{

	private static final long serialVersionUID = -2334963138078250952L;
	
	private Map<String, ActionModel> actionModels = new HashMap<String, ActionModel>();
	
	public void put(ActionModel actionModel){
		actionModels.put(actionModel.getPath(), actionModel);
	}
	
	public ActionModel get(String name){
		return actionModels.get(name);
	}
	
}

ConfigModelFactory

package com.bk201.framework;
import java.io.InputStream;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ConfigModelFactory {
	private ConfigModelFactory() {

	}

	private static ConfigModel configModel = null;

	public static ConfigModel newInstance() throws Exception {
		return newInstance("mvc.xml");
	}

	/**
	 * 工厂模式创建config建模对象
	 * 
	 * @param path
	 * @return
	 * @throws Exception
	 */
	public static ConfigModel newInstance(String path) throws Exception {
		if (null != configModel) {
			return configModel;
		}

		ConfigModel configModel = new ConfigModel();
		InputStream is = ConfigModelFactory.class.getResourceAsStream(path);
		SAXReader saxReader = new SAXReader();
		Document doc = saxReader.read(is);
		List<Element> actionEleList = doc.selectNodes("/config/action");
		ActionModel actionModel = null;
		ForwardModel forwardModel = null;
		for (Element actionEle : actionEleList) {
			 actionModel = new ActionModel();
			actionModel.setPath(actionEle.attributeValue("path"));
			actionModel.setType(actionEle.attributeValue("type"));
			List<Element> forwordEleList = actionEle.selectNodes("forward");
			for (Element forwordEle : forwordEleList) {
				forwardModel = new ForwardModel();
				forwardModel.setName(forwordEle.attributeValue("name"));
				forwardModel.setPath(forwordEle.attributeValue("path"));
				forwardModel.setRedirect(forwordEle.attributeValue("redirect"));
				actionModel.put(forwardModel);
			}

			configModel.put(actionModel);
		}

		return configModel;
	}
	
	public static void main(String[] args) {
		try {
			ConfigModel configModel = ConfigModelFactory.newInstance();
			ActionModel actionModel = configModel.get("/loginAction");
			ForwardModel forwardModel = actionModel.get("failed");
			System.out.println(actionModel.getType());
			System.out.println(forwardModel.getPath());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

ForwardModel

package com.bk201.framework;

import java.io.Serializable;

/**
 * 用来描述forward标签
 * @author Administrator
 *
 */
public class ForwardModel implements Serializable {

	private static final long serialVersionUID = -8587690587750366756L;

	private String name;
	private String path;
	private String redirect;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getRedirect() {
		return redirect;
	}

	public void setRedirect(String redirect) {
		this.redirect = redirect;
	}

}


mvc.xml

<?xml version="1.0" encoding="UTF-8"?>

<config>
	<!-- <action path="/addCal" type="com.lrc.web.AddCalAction">
		<forward name="res" path="/calRes.jsp" redirect="false" />
	</action>
	<action path="/delCal" type="com.lrc.web.DelCalAction">
		<forward name="res" path="/calRes.jsp" redirect="false" />
	</action>
	<action path="/cheCal" type="com.lrc.web.CheCalAction">
		<forward name="res" path="/calRes.jsp" redirect="false" />
	</action>
	<action path="/chuCal" type="com.lrc.web.ChuCalAction">
		<forward name="res" path="/calRes.jsp" redirect="false" />
	</action> -->
	
	<action path="/cal" type="com.lrc.web.CalAction">
		<forward name="res" path="/calRes.jsp" redirect="false" />
	</action>
	
</config>

cal实体类

package com.bk201.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.bk201.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 org.apache.commons.beanutils.BeanUtils;

/**
 * 中央控制器
 * 作用:
 * 接受用户请求,通过用户请求的url寻求指定的子控制器去处理业务
 * 1.对存放子控制器action 容器的增强
 * ?:原来为了完成业务需求,需要不断修改框架的代码,这样设计是不合理的
 * 处理方式:参照web.xml的设计方法,来完成中央控制器管理子控制器的动态配置
 * @author Felling
 *
 */
public class DispatcherServlet extends HttpServlet{

	private static final long serialVersionUID = 6023597752382711792L;
//	private Map<String, Action> map=new HashMap<String, Action>();
	private ConfigModel configModel;
	
	public void init() {
/*		map.put("/addCal", new AddCalAction());
		map.put("/delCal", new DelCalAction());
		map.put("/cheCal", new CheCalAction());
		map.put("/chuCal", new ChuCalAction());
*/	
		try {
			String xmlPath=this.getInitParameter("xmlPath");
			if(xmlPath==null || "".equals(xmlPath)) {
				configModel=ConfigModelFactory.newInstance();
			}else {
				configModel=ConfigModelFactory.newInstance(xmlPath);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		}
	
	@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);//这行代码就相当于上面的那行
		ActionModel actionModel=configModel.get(url);
		if(actionModel==null) {
			throw new RuntimeException("配置的标签找不到控制器");
		}
		
		try {
			Action action=(Action) Class.forName(actionModel.getType()).newInstance();
			
			//action就是com.lrc.web.CalAction
			if(action instanceof ModelDrivern) {
				ModelDrivern mdDrivern=(ModelDrivern)action;
				//此事的model的所有的属性值是null
				Object model=mdDrivern.getModel();
				BeanUtils.populate(model, req.getParameterMap());
				//可以将req.getParameterMap()的值通过反射的方式将其塞进model的实例中
//				Map<String , String[]> map=req.getParameterMap();
//				Set<Entry<String, String[]>> entrySet=map.entrySet();
//				Class<? extends Object> clz=model.getClass();
//				for (Entry<String, String[]> entry : entrySet) {
//					Field field=clz.getField(entry.getKey());
//					field.setAccessible(true);
//					field.set(model, entry.getValue());
//				}
			}
			
			String code=action.execute(req, resp);
			ForwardModel forwardModel=actionModel.get(code);
			if(forwardModel!=null) {
				String jspPath=forwardModel.getPath();
				if("false".equals(forwardModel.getRedirect())) {
					//做转发处理
					req.getRequestDispatcher(jspPath).forward(req, resp);
				}else {
					resp.sendRedirect(req.getContextPath()+"/"+jspPath);
				}
			}
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}

Action:

package com.bk201.framework;

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

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

ActionSupport

package com.bk201.framework;

import java.lang.reflect.Method;

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

/**
 * 增强版子控制器
 *原来的子控制器只能一个用户器请求
 *当用户有多个请求时,都是操作同一张表
 *原有的子控制器代码繁琐
 *增强版的作用:
 *将一组相关的操作放到一个Action
 */
public class ActionSupport implements Action{
	

	@Override
	public String execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String methodName=req.getParameter("methodName");
		String code=null;
		//this在这里指的是CalAction的一个类实例
		Method m=this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
		m.setAccessible(true);
		code=(String)m.invoke(this, req,resp);
		return null;
	}
	
	
	
	
}

CalAction:

package com.bk201.web;

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

import com.bk201.entity.Cal;
import com.bk201.framework.ActionSupport;
import com.bk201.framework.ModelDrivern;

/**
 * 增强版子控制器
 *原来的子控制器只能一个用户器请求
 *当用户有多个请求时,都是操作同一张表
 *原有的子控制器代码繁琐
 *增强版的作用:
 *将一组相关的操作放到一个Action
 */
public class CalAction extends ActionSupport implements ModelDrivern<Cal>{
	private Cal cal=new Cal();
	public String add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//		String num1=req.getParameter("num1");
//		String num2=req.getParameter("num2");
//		Cal cal=new Cal(num1,num2);
		req.setAttribute("res", Integer.valueOf(cal.getNum1())+Integer.valueOf(cal.getNum2()));
		//req.getRequestDispatcher("calRes.jsp").forward(req, resp);
		return "res";
	}
	
	public String del(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//		String num1=req.getParameter("num1");
//		String num2=req.getParameter("num2");
//		Cal cal=new Cal(num1,num2);
		req.setAttribute("res", Integer.valueOf(cal.getNum1())-Integer.valueOf(cal.getNum2()));
		//req.getRequestDispatcher("calRes.jsp").forward(req, resp);
		return "res";
	}
	
	public String che(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//		String num1=req.getParameter("num1");
//		String num2=req.getParameter("num2");
//		Cal cal=new Cal(num1,num2);
		req.setAttribute("res", Integer.valueOf(cal.getNum1())*Integer.valueOf(cal.getNum2()));
		//req.getRequestDispatcher("calRes.jsp").forward(req, resp);
		return "res";
	}
	
	public String chu(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//		String num1=req.getParameter("num1");
//		String num2=req.getParameter("num2");
//		Cal cal=new Cal(num1,num2);
		req.setAttribute("res", Integer.valueOf(cal.getNum1())/Integer.valueOf(cal.getNum2()));
		//req.getRequestDispatcher("calRes.jsp").forward(req, resp);
		return "res";
	}

	@Override
	public Cal getModel() {
		return cal;
	}
	
}

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>

基于SSM框架的智能家政保洁预约系统,是一个旨在提高家政保洁服务预约效率和管理水平的平台。该系统通过集成现代信息技术,为家政公司、家政服务人员和消费者提供了一个便捷的在线预约和管理系统。 系统的主要功能包括: 1. **用户管理**:允许消费者注册、登录,并管理他们的个人资料和预约历史。 2. **家政人员管理**:家政服务人员可以注册并更新自己的个人信息、服务类别和服务时间。 3. **服务预约**:消费者可以浏览不同的家政服务选项,选择合适的服务人员,并在线预约服务。 4. **订单管理**:系统支持订单的创建、跟踪和管理,包括订单的确认、完成和评价。 5. **评价系统**:消费者可以在家政服务完成后对服务进行评价,帮助提高服务质量和透明度。 6. **后台管理**:管理员可以管理用户、家政人员信息、服务类别、预约订单以及处理用户反馈。 系统采用Java语言开发,使用MySQL数据库进行数据存储,通过B/S架构实现用户与服务的在线交互。系统设计考虑了不同用户角色的需求,包括管理员、家政服务人员和普通用户,每个角色都有相应的权限和功能。此外,系统还采用了软件组件化、精化体系结构、分离逻辑和数据等方法,以便于未来的系统升级和维护。 智能家政保洁预约系统通过提供一个集中的平台,不仅方便了消费者的预约和管理,也为家政服务人员提供了一个展示和推广自己服务的机会。同时,系统的后台管理功能为家政公司提供了强大的数据支持和决策辅助,有助于提高服务质量和管理效率。该系统的设计与实现,标志着家政保洁服务向现代化和网络化的转型,为管理决策和控制提供保障,是行业发展中的重要里程碑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值