【mvc的增强】

news/2024/7/3 21:13:48

目录

  • 前言
    • mvc之xml配置相应实现类
    • mvc之xml提供返回路径
    • mvc之一个超类自动调用方法(需传method)
    • mvc之自动获取参数值
    • mvc之使得导入的xml可选择
    • 总结

前言

在之前已经完成了一个自定义mvc模式
此次就是对上一个的补充与增强。

这是上次的图解、
在这里插入图片描述
重点:通过XML对自定义mvc框架进行增强
在这里插入图片描述


mvc之xml配置相应实现类

我们之前是用map集合来进行存储一个类的信息。

这样是有弊端的:比如你想添加一个东西的时候,必定要对这个集合的内容进行修改。

但是我们可以用到xml文件来对应每一个类,只需要修改xml文件就可以动态调用到某个类

现在的init()方法:

public void init() {
		config = configModelFactory.build();//调用默认的xml文件
}

可以用doPost获得

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	init();//使得map里有值
	String requestURI = req.getRequestURI(); //获取请求的路径
	System.out.println("截取前:"+requestURI);
	
	String url = requestURI.substring(requestURI.lastIndexOf("/"), requestURI.lastIndexOf("."));//截取
	System.out.println("截取后:"+url);
	
	
	actionModel actionModel = config.pop(url);
	if(actionModel==null) {
		throw new RuntimeException("你没有配置相关的action");
	}
	action action = (action)Class.forName(actionModel.getType()).newInstance();//实列化xml中type里面的内容地址
	String code = action.execute(req, resp);
}

xml文件的配置

<config>

	<action path="/add" type="com.liwangwang.web.addAction">//这个的type就是一个类的路径
		<forward name="calres" path="/calres.jsp" redirect="false" />
	</action>
	<action path="/del" type="com.liwangwang.web.delAction">
		<forward name="calres" path="/calres.jsp" redirect="false" />
	</action>

</config>

mvc之xml提供返回路径

在方法中有这样的req.getRequestDispatcher("calres.jsp").forward(req, resp);可能需要多次运用,所以可以在从xml中获取到返回的路径

所以简单的调用节点.

public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String num1 = req.getParameter("num1");
		String num2 = req.getParameter("num2");
		req.setAttribute("res", Integer.parseInt(num1)+Integer.parseInt(num2));
		//req.getRequestDispatcher("calres.jsp").forward(req, resp);//我们要优化这行代码
		
		return "calres";
	}

实现

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		init();//使得map里有值
		String requestURI = req.getRequestURI(); //获取请求的路径
		System.out.println("截取前:"+requestURI);
		
		String url = requestURI.substring(requestURI.lastIndexOf("/"), requestURI.lastIndexOf("."));//截取
		System.out.println("截取后:"+url);
		
		
		actionModel actionModel = config.pop(url);
		if(actionModel==null) {
			throw new RuntimeException("你没有配置相关的action");
		}
		
		try {
			action action = (action)Class.forName(actionModel.getType()).newInstance();//实列化xml中type里面的内容地址
			
			if(action instanceof ModelDirven) {
				ModelDirven modeldirven = (ModelDirven)action;
				Object model = modeldirven.getModel();
				
				BeanUtils.populate(model, req.getParameterMap());
			}
			
			String code = action.execute(req, resp);//获取返回路径的名字
			forwardModel forwardModel = actionModel.pop(code);
			
			if(forwardModel==null) {
				throw new RuntimeException("你没有配置对应子控制器action的处理方式");
			}
			
			String jsppath = forwardModel.getPath();//获取返回的路径
			
			if(forwardModel.isRedirect()) {//判断是否要重定向
				resp.sendRedirect(req.getContextPath()+jsppath);
			}
			else  {//判断是否要转发
				req.getRequestDispatcher(req.getContextPath()+jsppath).forward(req, resp);
			}
			
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}

mvc之一个超类自动调用方法(需传method)

我们需要用一个方法来调用相对应的方法。

那么就需要用到了反射的机制。

将其全放入到calAction

public class CalAction extends actionSupper{
	public String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			String num1 = req.getParameter("num1");
			String num2 = req.getParameter("num2");
			req.setAttribute("res", Integer.parseInt(cal.getNum1())+Integer.parseInt(cal.getNum2()));
			
			return "calres";//返回路径的名字
		}
	public String del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
			String num1 = req.getParameter("num1");
			String num2 = req.getParameter("num2");
			req.setAttribute("res", Integer.parseInt(cal.getNum1())-Integer.parseInt(cal.getNum2()));
			
			return "calres";//返回的路径名字
		}
}

一个超类,


public class actionSupper implements action {


	public final String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String code = null;
		try {
			Method Method = this.getClass().getDeclaredMethod(req.getParameter("method"), HttpServletRequest.class,HttpServletResponse.class);
			Method.setAccessible(true);
			code = (String) Method.invoke(this, req,resp);//调用相对应的方法
			
			
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		
		return code;
	}

}

mvc之自动获取参数值

因为如果有多个参数的话就比较麻烦,所以需要优化

比如这行:

String num1 = req.getParameter("num1");
		String num2 = req.getParameter("num2");

创建一个接口

public interface ModelDirven<T>{
	T getModel();
}

然后继承

public class CalAction extends actionSupper implements ModelDirven<Cal> {
	
	private Cal cal =new Cal();
	
	public String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	
		req.setAttribute("res", Integer.parseInt(cal.getNum1())+Integer.parseInt(cal.getNum2()));
		
		return "calres";
	}
	public String del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setAttribute("res", Integer.parseInt(cal.getNum1())-Integer.parseInt(cal.getNum2()));
		
		return "calres";
	}
	
	@Override
	public Cal getModel() {
		
		return cal;
	}
}

mvc之使得导入的xml可选择

大家大概也看到了,在之前init()方法是这样的:

public void init() {
				config = configModelFactory.build();
	}

优化后

public void init() {
		try {
			
			//使得调用指定位置的xml文件
			String xmlpath  = this.getInitParameter("xmlpath");
			if(xmlpath==null||"".equals(xmlpath)) {
				config = configModelFactory.build();
			}
			else {
				config = configModelFactory.build(xmlpath);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}

需要在web.xml中配置

在这里插入图片描述


总结

在继承和实现的流程中,我还是有点没搞明白

Thanks♪(・ω・)ノ希望对大家有所帮助


http://www.niftyadmin.cn/n/3726062.html

相关文章

mysql备份还原命令_MYSQL备份还原命令

MYSQL中如何操作备份数据以及还原备份数据&#xff0c;那实际的操作命令是什么呢&#xff1f;下面我们来看看实际测试过程。其中&#xff0c;C:\ProgramFiles\MySQL\MySQL Server 5.1\bin>路径为你MYSQL的安装路径。一、备份命令在 开始——运行 输入cmd 进入cmd命令界面&am…

“GANs 之父”Goodfellow亲身传授:深度学习未来的8大方向和入门AI必备的三大技能...

近日&#xff0c;被称为“GANs 之父”的 Ian Goodfellow 在 Quora 上回答网友提问。在问答环节中&#xff0c;Goodfellow 不仅介绍了谷歌大脑(Google Brian)目前正在进行的工作&#xff0c;还详细阐述了 GANs 目前碰到的各种问题&#xff0c;以及未来的发展方向。作为《Deep Le…

jquery的运用

目录前言jquery库的引用jquery的导入jquery的程序入口jquery的简单选择器jquery与Js的转换jquery插件json的体现形式extend的扩充extend的实列ajax的简单使用ajax的转换前言 jquery 它是一个轻量级的javascript类库 那么实际上&#xff0c;它就是一个别人写好的一个类而已。 …

计算机运行程序多核和单核,电脑CPU单核与多核的区别

满意答案qinxiaoliao2013.01.26采纳率&#xff1a;54% 等级&#xff1a;12已帮助&#xff1a;13103人双核的优势不是频率&#xff0c;而是对付同时处理多件事情。单核同时只能干一件事&#xff0c;比如你同时在后台BT下载&#xff0c;前台一边看电影一边拷贝文件一边QQ。这么…

mysql日期本地化_php date时间本地化问题(转)

php date时间本地化问题(转)今天在写一个东西时&#xff0c;发现时间一直对不上&#xff0c;date("Y-m-d h:i:s") 总是与服务器时间差几个小时&#xff0e;原来从php5.1.0开始&#xff0c;php.ini里加入了date.timezone这个选项&#xff0c;默认情况下是关闭的也就是…

【mvc之后台优化代码】

目录前言中央控制器 DispactherServlet(按照运行顺序来)子控制接口 action子控制器超类 actionSupper总方法集合 StudentAction自动调用参数的 ModelDirven方法 BeanDao方法调用 StudentDaoxml的配置前台调用xml调用代码总结前言 先说好&#xff0c;本次因为运用的是上次【mvc…

计算机联锁控制系统的软件应具备信号操作功能,计算机联锁技术学习包

敌对近路()的条件下&#xff0c;将道岔和敌对进路()&#xff0c;使道岔()转换、敌对进路()建立。7、硬件系统计算机控制系统的硬件主要有()、外部设备、()、输出设备和()等组成。8、支持软件支持软件包括()&#xff0c;高级语言&#xff0c;()&#xff0c;编辑程序&#xff0c;…

python中truncate的用法_Python中truncate()方法有哪些功能?

摘要:下文讲述Python中truncate()的方法的功能简介说明&#xff0c;如下所示:truncate()方法功能:用于截断文件并返回截断的字节长度truncate()方法语法fileObject.truncate([size])--------参数说明--------fileObject:文件对象size:可选当此参数存在时&#xff0c;则文件从开…