自定义mvc框架(2)

自定义mvc框架(2)

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工作原理图

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

5. 通过XML对自定义mvc框架进行增强
  1. 将Action的信息配置到xml(反射实例化)

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

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

  4. 利用ModelDriver接口对Java对象进行赋值(反射读写方法)
    BeanUtils.populate(calBean, parameterMap);

    ModelDriver接口返回的对象不能为空

  5. 使得框架的配置文件可变

下面展示一些 内联代码片
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();
	}
	
	
	
}

Action接口


package com.lrc.framework;

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

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

ActionModel


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

}

ActionSupport


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

**DispatcherServlet **


package com.lrc.framework;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.management.RuntimeErrorException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.util.ParameterMap;
import org.apache.commons.beanutils.BeanUtils;

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>();
	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();
		}
	}
	
}

ModelDrivern


package com.lrc.framework;

import com.lrc.entity.Cal;

/**
 * 模型驱动接口
 * 作用是:将jsp所有传递的参数以及参数值都自动封装到浏览器索要操作的实体类中
 *
 */
public interface ModelDrivern<T> {
	T getModel();
}

mvc.xml


<?xml version="1.0" encoding="UTF-8"?>
	<!--
		config标签:可以包含0~N个action标签
	-->
<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>

**CalAction **


package com.lrc.web;

import java.lang.reflect.Method;

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

import com.lrc.entity.Cal;
import com.lrc.framework.ActionSupport;
import com.lrc.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;
	}
	
}

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>
  <init-param>
  <param-name>xmlPath</param-name>
  <param-value>/mvc.xml</param-value>
  </init-param>
  </servlet>
  <servlet-mapping>
  <servlet-name>dispatcherServlet</servlet-name>
  <url-pattern>*.action</url-pattern>
  </servlet-mapping>
</web-app>

总结

  1. 主要是针对action动态配置(参考的web.xml)建模
  2. 针对于结果码的处理
    利用了mvc.xml的result配置,在中央控制器中进行页面的跳转
  3. 将用户的一组操作放入一个子控制器中
    反射的动态方法调用
    由jsp传递methodName到后台,完事之后,调用this关键字获取当前的类对象,然后方法对象,进行动态调用
  4. 建立模型驱动接口,专门用来处理jsp传递到后台参数封装成实体的过程
    BeanUtils.propute(model,Map);
  5. 解决框架配置文件重名问题
    web.xml servlet Init-param
数据治理是确保数据准确性、可靠性、安全性、可用性和完整性的体系和框架。它定义了组织内部如何使用、存储、保护和共享数据的规则和流程。数据治理的重要性随着数字化转型的加速而日益凸显,它能够提高决策效率、增强业务竞争力、降低风险,并促进业务创新。有效的数据治理体系可以确保数据在采集、存储、处理、共享和保护等环节的合规性和有效性。 数据质量管理是数据治理中的关键环节,它涉及数据质量评估、数据清洗、标准化和监控。高质量的数据能够提升业务决策的准确性,优化业务流程,并挖掘潜在的商业价值。随着大数据和人工智能技术的发展,数据质量管理在确保数据准确性和可靠性方面的作用愈发重要。企业需要建立完善的数据质量管理和校验机制,并通过数据清洗和标准化提高数据质量。 数据安全与隐私保护是数据治理中的另一个重要领域。随着数据量的快速增长和互联网技术的迅速发展,数据安全与隐私保护面临前所未有的挑战。企业需要加强数据安全与隐私保护的法律法规和技术手段,采用数据加密、脱敏和备份恢复等技术手段,以及加强培训和教育,提高安全意识和技能水平。 数据流程管理与监控是确保数据质量、提高数据利用率、保护数据安全的重要环节。有效的数据流程管理可以确保数据流程的合规性和高效性,而实时监控则有助于及时发现并解决潜在问题。企业需要设计合理的数据流程架构,制定详细的数据管理流程规范,并运用数据审计和可视化技术手段进行监控。 数据资产管理是将数据视为组织的重要资产,通过有效的管理和利用,为组织带来经济价值。数据资产管理涵盖数据的整个生命周期,包括数据的创建、存储、处理、共享、使用和保护。它面临的挑战包括数据量的快速增长、数据类型的多样化和数据更新的迅速性。组织需要建立完善的数据管理体系,提高数据处理和分析能力,以应对这些挑战。同时,数据资产的分类与评估、共享与使用规范也是数据资产管理的重要组成部分,需要制定合理的标准和规范,确保数据共享的安全性和隐私保护,以及建立合理的利益分配和权益保障机制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值