博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义MVC上
阅读量:3961 次
发布时间:2019-05-24

本文共 27403 字,大约阅读时间需要 91 分钟。

什么是MVC

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

自定义MVC工作原理图

在这里插入图片描述
主控制动态调用子控制器调用完成具体的业务逻辑
请求、主控制器、子控制器
例子:计算机两个数的加减乘除
Entity封装

package com.zxp.entity;public class Cal {private String url;//地址private String num1;private String num2;public String getUrl() {	return url;}public void setUrl(String url) {	this.url = url;}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();}}

framework子控制器

package com.zxp.framework;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * 子控制器 * 作用: * 具体处理用户请求的类(实现Action接口的类) * @author 玉姬 * */public interface Action {	String execute(HttpServletRequest req, HttpServletResponse resp) ;}

Web请求

package com.zxp.web;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.net.httpserver.spi.HttpServerProvider;import com.zxp.entity.Cal;import com.zxp.framework.Action;public class AddCallAction implements Action {	//处理请求	@Override	public String execute(HttpServletRequest req, HttpServletResponse resp)  {		String num1=req.getParameter("num1");		String num2=req.getParameter("num2");		Cal cal=new Cal(num1,num2);		req.setAttribute("rs", Integer.valueOf(cal.getNum1())+Integer.valueOf(cal.getNum2()));		try {			req.getRequestDispatcher("/rs.jsp").forward(req, resp);		} catch (ServletException | IOException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}		return null;	}	}

package com.zxp.web;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.net.httpserver.spi.HttpServerProvider;import com.zxp.entity.Cal;-import com.zxp.framework.Action;public class DelCallAction implements Action {	//处理请求	@Override	public String execute(HttpServletRequest req, HttpServletResponse resp)  {		String num1=req.getParameter("num1");		String num2=req.getParameter("num2");		Cal cal=new Cal(num1,num2);		req.setAttribute("rs", Integer.valueOf(cal.getNum1())-Integer.valueOf(cal.getNum2()));		try {			req.getRequestDispatcher("/rs.jsp").forward(req, resp);		} catch (ServletException | IOException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}		return null;	}	}

package com.zxp.web;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.net.httpserver.spi.HttpServerProvider;import com.zxp.entity.Cal;import com.zxp.framework.Action;public class MulCallAction implements Action {	//处理请求	@Override	public String execute(HttpServletRequest req, HttpServletResponse resp)  {		String num1=req.getParameter("num1");		String num2=req.getParameter("num2");		Cal cal=new Cal(num1,num2);		req.setAttribute("rs", Integer.valueOf(cal.getNum1())*Integer.valueOf(cal.getNum2()));		try {			req.getRequestDispatcher("/rs.jsp").forward(req, resp);		} catch (ServletException | IOException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}		return null;	}	}

package com.zxp.web;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.net.httpserver.spi.HttpServerProvider;import com.zxp.entity.Cal;import com.zxp.framework.Action;public class DivCallAction implements Action {	//处理请求	@Override	public String execute(HttpServletRequest req, HttpServletResponse resp)  {		String num1=req.getParameter("num1");		String num2=req.getParameter("num2");		Cal cal=new Cal(num1,num2);		req.setAttribute("rs", Integer.valueOf(cal.getNum1())/Integer.valueOf(cal.getNum2()));		try {			req.getRequestDispatcher("/rs.jsp").forward(req, resp);		} catch (ServletException | IOException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}		return null;	}	}

framework中央控制器

package com.zxp.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.zxp.web.AddCallAction;import com.zxp.web.DelCallAction;import com.zxp.web.DivCallAction;import com.zxp.web.MulCallAction;/** * 中央控制器 * 作用: * 接受用户请求,通过用户请求的url寻求指定的子控制器去处理业务 * @author 玉姬 * */public class DispatcherServlet extends HttpServlet {	private static final long serialVersionUID = -2291078179694007198L;//	容器存放	private Map
actionMap=new HashMap
(); // url和uri的区别 地址区别// url :http://localhost:8080/T_mvc/cal.add.action// uri :T_mvc/cal.add.action public void init() {// 初始化 actionMap.put("/cal_add", new AddCallAction());//发出请求 actionMap.put("/cal_del", new DelCallAction());//发出请求 actionMap.put("/cal_mul", new MulCallAction()); actionMap.put("/cal_div", new DivCallAction()); } @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 { String url=req.getRequestURI(); url=url.substring(url.lastIndexOf("/"),url.lastIndexOf("."));// AddCallAction action=(AddCallAction) actionMap.get(url);// Action a=action; Action action=actionMap.get(url); action.execute(req, resp); } }

配置

T_mvc
cal.jsp
dispatcherServlet
com.zxp.framework.DispatcherServlet
dispatcherServlet
*.action

Jsp页面

主页面

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>
Insert title here
num1:
num2:

结果页面

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>
Insert title here结果:${rs }

运行

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

自定义MVC框架五步增强

1、 Action的动态配置

为什么:为了完成业务需求需要不断修改框架代码。这样设计是不合理的
处理:参照web.xml的实际方法,来完成中央控制管理子控制器的动态配置

package com.zxp.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.zxp.web.AddCallAction;import com.zxp.web.DelCallAction;import com.zxp.web.DivCallAction;import com.zxp.web.MulCallAction;/** * 中央控制器 * 作用: * 接受用户请求,通过用户请求的url寻求指定的子控制器去处理业务 *  *  * 1.对存放子控制器action容器的增强 * 为什么:为了完成业务需求需要不断修改框架代码。这样设计是不合理的 * 处理:参照web.xml的实际方法,来完成中央控制管理子控制器的动态配置 * @author 玉姬 * */public class DispatcherServlet extends HttpServlet {	private static final long serialVersionUID = -2291078179694007198L;//	容器存放//	private Map
actionMap=new HashMap
(); private ConfigModel configModel=null;// url和uri的区别 地址区别// url :http://localhost:8080/T_mvc/cal.add.action// uri :T_mvc/cal.add.action public void init() {// 初始化 // actionMap.put("/cal_add", new AddCallAction());//发出请求// actionMap.put("/cal_del", new DelCallAction());//发出请求// actionMap.put("/cal_mul", new MulCallAction());// actionMap.put("/cal_div", new DivCallAction()); try { configModel=ConfigModelFactory.newInstance(); } catch (Exception e) { // TODO Auto-generated catch block 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 { String url=req.getRequestURI(); url=url.substring(url.lastIndexOf("/"),url.lastIndexOf("."));// AddCallAction action=(AddCallAction) actionMap.get(url);// Action a=action;// Action action=actionMap.get(url);// action.execute(req, resp); ActionModel actionModel=configModel.get(url); try { Action action=(Action) getClass().forName(actionModel.getType()).newInstance(); action.execute(req, resp); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

配置

运行加法

在这里插入图片描述

在这里插入图片描述
2、 子控制器返回码的处理
处理结果码的调转形式
达到简化代码的结果

package com.zxp.web;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.net.httpserver.spi.HttpServerProvider;import com.zxp.entity.Cal;import com.zxp.framework.Action;public class AddCallAction implements Action {	//处理请求	@Override	public String execute(HttpServletRequest req, HttpServletResponse resp)  {		String num1=req.getParameter("num1");		String num2=req.getParameter("num2");		Cal cal=new Cal(num1,num2);		req.setAttribute("rs", Integer.valueOf(cal.getNum1())+Integer.valueOf(cal.getNum2()));//		try {//			req.getRequestDispatcher("/rs.jsp").forward(req, resp);//		} catch (ServletException | IOException e) {//			// TODO Auto-generated catch block//			e.printStackTrace();//		}		return "rs";	}	}

package com.zxp.web;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.net.httpserver.spi.HttpServerProvider;import com.zxp.entity.Cal;import com.zxp.framework.Action;public class DelCallAction implements Action {	//处理请求	@Override	public String execute(HttpServletRequest req, HttpServletResponse resp)  {		String num1=req.getParameter("num1");		String num2=req.getParameter("num2");		Cal cal=new Cal(num1,num2);		req.setAttribute("rs", Integer.valueOf(cal.getNum1())-Integer.valueOf(cal.getNum2()));//		try {//			req.getRequestDispatcher("/rs.jsp").forward(req, resp);//		} catch (ServletException | IOException e) {//			// TODO Auto-generated catch block//			e.printStackTrace();//		}		return "rs";	}	}

package com.zxp.web;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.net.httpserver.spi.HttpServerProvider;import com.zxp.entity.Cal;import com.zxp.framework.Action;public class MulCallAction implements Action {	//处理请求	@Override	public String execute(HttpServletRequest req, HttpServletResponse resp)  {		String num1=req.getParameter("num1");		String num2=req.getParameter("num2");		Cal cal=new Cal(num1,num2);		req.setAttribute("rs", Integer.valueOf(cal.getNum1())*Integer.valueOf(cal.getNum2()));//		try {//			req.getRequestDispatcher("/rs.jsp").forward(req, resp);//		} catch (ServletException | IOException e) {//			// TODO Auto-generated catch block//			e.printStackTrace();//		}		return "rs";	}	}

package com.zxp.web;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.net.httpserver.spi.HttpServerProvider;import com.zxp.entity.Cal;import com.zxp.framework.Action;public class DivCallAction implements Action {	//处理请求	@Override	public String execute(HttpServletRequest req, HttpServletResponse resp)  {		String num1=req.getParameter("num1");		String num2=req.getParameter("num2");		Cal cal=new Cal(num1,num2);		req.setAttribute("rs", Integer.valueOf(cal.getNum1())/Integer.valueOf(cal.getNum2()));//		try {//			req.getRequestDispatcher("/rs.jsp").forward(req, resp);//		} catch (ServletException | IOException e) {//			// TODO Auto-generated catch block//			e.printStackTrace();//		}		return "rs";	}	}

中央控制器

package com.zxp.framework;import java.io.IOException;import java.util.HashMap;import java.util.Map;import javax.management.RuntimeErrorException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.zxp.web.AddCallAction;import com.zxp.web.DelCallAction;import com.zxp.web.DivCallAction;import com.zxp.web.MulCallAction;/** * 中央控制器 * 作用: * 接受用户请求,通过用户请求的url寻求指定的子控制器去处理业务 *  *  * 1.对存放子控制器action容器的增强 * 为什么:为了完成业务需求需要不断修改框架代码。这样设计是不合理的 * 处理:参照web.xml的实际方法,来完成中央控制管理子控制器的动态配置 *  * 2。处理结果码的调转形式 * 达到简化代码的结果 * @author 玉姬 * */public class DispatcherServlet extends HttpServlet {	private static final long serialVersionUID = -2291078179694007198L;//	容器存放//	private Map
actionMap=new HashMap
(); private ConfigModel configModel=null;// url和uri的区别 地址区别// url :http://localhost:8080/T_mvc/cal.add.action// uri :T_mvc/cal.add.action public void init() {// 初始化 // actionMap.put("/cal_add", new AddCallAction());//发出请求// actionMap.put("/cal_del", new DelCallAction());//发出请求// actionMap.put("/cal_mul", new MulCallAction());// actionMap.put("/cal_div", new DivCallAction()); try { configModel=ConfigModelFactory.newInstance(); } catch (Exception e) { // TODO Auto-generated catch block 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 { String url=req.getRequestURI(); url=url.substring(url.lastIndexOf("/"),url.lastIndexOf("."));// AddCallAction action=(AddCallAction) actionMap.get(url);// Action a=action;// Action action=actionMap.get(url);// action.execute(req, resp); ActionModel actionModel=configModel.get(url); try { if(actionModel==null) { throw new RuntimeException("你没有配置指定的子控制器来处理用户请求"); } Action action=(Action) getClass().forName(actionModel.getType()).newInstance(); String code=action.execute(req, resp); ForwardModel forwardModel=actionModel.get(code); if("false".equals(forwardModel.getRedirect())) { req.getRequestDispatcher(forwardModel.getPath()).forward(req, resp); }else {// 注意。默认缺少项目名 resp.sendRedirect(req.getContextPath()+forwardModel.getPath());//重定向 } } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

运行减法

在这里插入图片描述
在这里插入图片描述
3、 增强子控制器
将一组操作放到一个子控制器完成

package com.zxp.web;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.zxp.entity.Cal;import com.zxp.framework.Action;import com.zxp.framework.ActionSupport;public class CallAction extends ActionSupport {	public String add(HttpServletRequest req, HttpServletResponse resp)  {		String num1=req.getParameter("num1");		String num2=req.getParameter("num2");		Cal cal=new Cal(num1,num2);		req.setAttribute("rs", Integer.valueOf(cal.getNum1())+Integer.valueOf(cal.getNum2()));//		try {//			req.getRequestDispatcher("/rs.jsp").forward(req, resp);//		} catch (ServletException | IOException e) {//			// TODO Auto-generated catch block//			e.printStackTrace();//		}		return "rs";	}		public String del(HttpServletRequest req, HttpServletResponse resp)  {		String num1=req.getParameter("num1");		String num2=req.getParameter("num2");		Cal cal=new Cal(num1,num2);		req.setAttribute("rs", Integer.valueOf(cal.getNum1())-Integer.valueOf(cal.getNum2()));//		try {//			req.getRequestDispatcher("/rs.jsp").forward(req, resp);//		} catch (ServletException | IOException e) {//			// TODO Auto-generated catch block//			e.printStackTrace();//		}		return "rs";	}	public String mul(HttpServletRequest req, HttpServletResponse resp)  {		String num1=req.getParameter("num1");		String num2=req.getParameter("num2");		Cal cal=new Cal(num1,num2);		req.setAttribute("rs", Integer.valueOf(cal.getNum1())*Integer.valueOf(cal.getNum2()));//		try {//			req.getRequestDispatcher("/rs.jsp").forward(req, resp);//		} catch (ServletException | IOException e) {//			// TODO Auto-generated catch block//			e.printStackTrace();//		}		return "rs";	}	public String div(HttpServletRequest req, HttpServletResponse resp)  {		String num1=req.getParameter("num1");		String num2=req.getParameter("num2");		Cal cal=new Cal(num1,num2);		req.setAttribute("rs", Integer.valueOf(cal.getNum1())/Integer.valueOf(cal.getNum2()));//		try {//			req.getRequestDispatcher("/rs.jsp").forward(req, resp);//		} catch (ServletException | IOException e) {//			// TODO Auto-generated catch block//			e.printStackTrace();//		}		return "rs";	}}

增强版的子控制器

package com.zxp.framework;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * 增强版的子控制器 * 作用:将一组操作放到一个子控制器去完成 * @author 玉姬 * */public class ActionSupport implements Action {	@Override	public String execute(HttpServletRequest req, HttpServletResponse resp) {//		从前台传递需要调用的方法名到后台,实现动态方法调用		String methodName=req.getParameter("methodName");//		思考:如何获取到类实例		String code=null;		try {			Method m=this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);			m.setAccessible(true);			try {				code=(String) m.invoke(this, req,resp);			} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {				// TODO Auto-generated catch block				e.printStackTrace();			}		} catch (NoSuchMethodException | SecurityException e) {			e.printStackTrace();		}		return code;	}}

** 中央控制器**

package com.zxp.framework;import java.io.IOException;import java.util.HashMap;import java.util.Map;import javax.management.RuntimeErrorException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.zxp.web.AddCallAction;import com.zxp.web.DelCallAction;import com.zxp.web.DivCallAction;import com.zxp.web.MulCallAction;/** * 中央控制器 * 作用: * 接受用户请求,通过用户请求的url寻求指定的子控制器去处理业务 *  *  * 1.对存放子控制器action容器的增强 * 为什么:为了完成业务需求需要不断修改框架代码。这样设计是不合理的 * 处理:参照web.xml的实际方法,来完成中央控制管理子控制器的动态配置 *  * 2。处理结果码的调转形式 * 达到简化代码的结果 *  * 3.将一组操作放到一个子控制器完成 *  * 4.处理jsp传递到后台的参数封装 * @author 玉姬 * */public class DispatcherServlet extends HttpServlet {	private static final long serialVersionUID = -2291078179694007198L;//	容器存放//	private Map
actionMap=new HashMap
(); private ConfigModel configModel=null;// url和uri的区别 地址区别// url :http://localhost:8080/T_mvc/cal.add.action// uri :T_mvc/cal.add.action public void init() {// 初始化 // actionMap.put("/cal_add", new AddCallAction());//发出请求// actionMap.put("/cal_del", new DelCallAction());//发出请求// actionMap.put("/cal_mul", new MulCallAction());// actionMap.put("/cal_div", new DivCallAction()); try { configModel=ConfigModelFactory.newInstance(); } catch (Exception e) { // TODO Auto-generated catch block 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 { String url=req.getRequestURI(); url=url.substring(url.lastIndexOf("/"),url.lastIndexOf("."));// AddCallAction action=(AddCallAction) actionMap.get(url);// Action a=action;// Action action=actionMap.get(url);// action.execute(req, resp); ActionModel actionModel=configModel.get(url); try { if(actionModel==null) { throw new RuntimeException("你没有配置指定的子控制器来处理用户请求"); } Action action=(Action) getClass().forName(actionModel.getType()).newInstance(); String code=action.execute(req, resp); ForwardModel forwardModel=actionModel.get(code); if("false".equals(forwardModel.getRedirect())) { req.getRequestDispatcher(forwardModel.getPath()).forward(req, resp); }else {// 注意。默认缺少项目名 resp.sendRedirect(req.getContextPath()+forwardModel.getPath());//重定向 } } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

配置

jsp页面

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>
Insert title here
num1:
num2:

运行乘法

在这里插入图片描述

在这里插入图片描述

4、jsp参数封装增强
处理jsp传递到后台的参数封装
模型驱动接口

package com.zxp.framework;/** * 模型驱动接口 * 作用: * 给对应处理业务的子控制器中包含的实体类进行jsp参数封装 * @author 玉姬 * * @param 
*/public interface ModelDriven
{ T getModel();}

实现模型驱动接口

package com.zxp.web;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.zxp.entity.Cal;import com.zxp.framework.Action;import com.zxp.framework.ActionSupport;import com.zxp.framework.ModelDriven;public class CallAction extends ActionSupport implements ModelDriven
{ private Cal cal=new Cal(); public String add(HttpServletRequest req, HttpServletResponse resp) {// String num1=req.getParameter("num1");// String num2=req.getParameter("num2");// Cal cal=new Cal(num1,num2); req.setAttribute("rs", Integer.valueOf(cal.getNum1())+Integer.valueOf(cal.getNum2()));// try {// req.getRequestDispatcher("/rs.jsp").forward(req, resp);// } catch (ServletException | IOException e) {// // TODO Auto-generated catch block// e.printStackTrace();// } return "rs"; } public String del(HttpServletRequest req, HttpServletResponse resp) {// String num1=req.getParameter("num1");// String num2=req.getParameter("num2");// Cal cal=new Cal(num1,num2); req.setAttribute("rs", Integer.valueOf(cal.getNum1())-Integer.valueOf(cal.getNum2()));// try {// req.getRequestDispatcher("/rs.jsp").forward(req, resp);// } catch (ServletException | IOException e) {// // TODO Auto-generated catch block// e.printStackTrace();// } return "rs"; } public String mul(HttpServletRequest req, HttpServletResponse resp) {// String num1=req.getParameter("num1");// String num2=req.getParameter("num2");// Cal cal=new Cal(num1,num2); req.setAttribute("rs", Integer.valueOf(cal.getNum1())*Integer.valueOf(cal.getNum2()));// try {// req.getRequestDispatcher("/rs.jsp").forward(req, resp);// } catch (ServletException | IOException e) {// // TODO Auto-generated catch block// e.printStackTrace();// } return "rs"; } public String div(HttpServletRequest req, HttpServletResponse resp) {// String num1=req.getParameter("num1");// String num2=req.getParameter("num2");// Cal cal=new Cal(num1,num2); req.setAttribute("rs", Integer.valueOf(cal.getNum1())/Integer.valueOf(cal.getNum2()));// try {// req.getRequestDispatcher("/rs.jsp").forward(req, resp);// } catch (ServletException | IOException e) {// // TODO Auto-generated catch block// e.printStackTrace();// } return "rs"; } @Override public Cal getModel() { // TODO Auto-generated method stub return cal; }}

给model赋值

package com.zxp.framework;import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.util.HashMap;import java.util.Map;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.commons.beanutils.BeanUtils;import org.apache.commons.beanutils.PropertyUtils;import com.zxp.entity.Cal;import com.zxp.web.AddCallAction;import com.zxp.web.DelCallAction;import com.zxp.web.DivCallAction;import com.zxp.web.MulCallAction;/** * 中央控制器 * 作用: * 接受用户请求,通过用户请求的url寻求指定的子控制器去处理业务 *  *  * 1.对存放子控制器action容器的增强 * 为什么:为了完成业务需求需要不断修改框架代码。这样设计是不合理的 * 处理:参照web.xml的实际方法,来完成中央控制管理子控制器的动态配置 *  * 2。处理结果码的调转形式 * 达到简化代码的结果 *  * 3.将一组操作放到一个子控制器完成 *  * 4.处理jsp传递到后台的参数封装 * @author 玉姬 * */public class DispatcherServlet extends HttpServlet {	private static final long serialVersionUID = -2291078179694007198L;//	容器存放//	private Map
actionMap=new HashMap
(); private ConfigModel configModel=null;// url和uri的区别 地址区别// url :http://localhost:8080/T_mvc/cal.add.action// uri :T_mvc/cal.add.action public void init() {// 初始化 // actionMap.put("/cal_add", new AddCallAction());//发出请求// actionMap.put("/cal_del", new DelCallAction());//发出请求// actionMap.put("/cal_mul", new MulCallAction());// actionMap.put("/cal_div", new DivCallAction()); try { configModel=ConfigModelFactory.newInstance(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @SuppressWarnings("static-access") @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String url=req.getRequestURI(); url=url.substring(url.lastIndexOf("/"),url.lastIndexOf("."));// AddCallAction action=(AddCallAction) actionMap.get(url);// Action a=action;// Action action=actionMap.get(url);// action.execute(req, resp); ActionModel actionModel=configModel.get(url); try { if(actionModel==null) { throw new RuntimeException("你没有配置指定的子控制器来处理用户请求"); } Action action=(Action) getClass().forName(actionModel.getType()).newInstance(); if(action instanceof ModelDriven) { ModelDriven modelDriven=(ModelDriven) action; Object model=modelDriven.getModel();// 给model赋值意味着调用add/del方法时cal不在是空值 BeanUtils.populate(model, req.getParameterMap()); } String code=action.execute(req, resp); ForwardModel forwardModel=actionModel.get(code); if("false".equals(forwardModel.getRedirect())) { req.getRequestDispatcher(forwardModel.getPath()).forward(req, resp); }else {// 注意。默认缺少项目名 resp.sendRedirect(req.getContextPath()+forwardModel.getPath());//重定向 } } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

运行除法

在这里插入图片描述

在这里插入图片描述
5、框架配置文件可变

方法 ( <init-param> </init-param>

T_mvc
cal.jsp
dispatcherServlet
com.zxp.framework.DispatcherServlet
mvcXmlLocation
/yu.xml
dispatcherServlet
*.action

调用

写在中央控制器

//			解决框架配置文件重名冲突问题			String mvcXmlLocation=this.getInitParameter("mvcXmlLocation");			if(null==mvcXmlLocation||"".equals("mvcXmlLocation")) {				mvcXmlLocation="mvc.xml";			}			System.out.println(mvcXmlLocation);

冲突

在这里插入图片描述
不冲突
在这里插入图片描述

转载地址:http://pyrzi.baihongyu.com/

你可能感兴趣的文章
echo如何手动输出换行
查看>>
身份证的正确使用方法——非常重要的知识
查看>>
ExtJS & Ajax
查看>>
Tomcat在Windows下的免安装配置
查看>>
JMeter常用测试元件
查看>>
JMeter——使用技巧
查看>>
Hibernate 实体层设计--Table per subclass
查看>>
JavaScriptHelper之 observe_field
查看>>
JavaScriptHelper之 periodically_ajax_tag
查看>>
Ruby on Rails(ROR) 小结(一) 绑定controller and view
查看>>
Ruby on Rails(ROR) 小结(一) 通过Schema Migrations来创建数据表
查看>>
form表单post请求发送及回收
查看>>
confluence5.8.10 安装与破解
查看>>
Testlink使用文档
查看>>
Ruby on Rails(ROR) 实例开发之一 配置数据库Mysql
查看>>
Ruby on Rails(ROR) 实例开发之一 创建开发项目环境
查看>>
Ruby on Rails(ROR) 实例开发之一 创建数据表
查看>>
Android_Note(一)——主题界面设计
查看>>
Android_Note(二)——主界面功能
查看>>
Android开发之——子线程中使用Toast或者更新UI
查看>>