自定义MVC框架实现

目录

一、中央控制器动态加载存储子控制器

二、参数传递封装优化

三、返回值页面跳转优化

四、框架配置文件可变


一、中央控制器动态加载存储子控制器

一、中央控制器动态加载存储子控制器在上期我们了解的MVC框架的工作原理,但是也存在不足的地方,比如每次访问都要new一个访问类。

对DispatcherServlet进行改造:

package com.zlp.framework;
 
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 
import javax.management.RuntimeErrorException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.zlp.web.BookAction;
 
 
 
/**
 * 中央控制器:
 * 主要职能:接受浏览器请求,找到对应的处理人
 *
 * 
 * @author zlp

 *
 */
@WebServlet("*.action")
public class DispatcherServlet extends HttpServlet{
	//private Map<String, Action> actions=new HashMap<String, Action>();
	
//	通过建模我们可以知道,最终configModel对象会包含config.xml中所有子控制器的信息
//	同时为了解决中央控制器能够动态加载保存子控制器的信息,那么我们只需要引入congigModel对象即可
	private ConfigModel ConfigModel;
	
	
	
	//	程序启动时,只会加载一次
	@Override
	public void init() throws ServletException {
		//actions.put("/book", new BookAction());
//		actions.put("/order", new BooKAction());
		try {
			ConfigModel = (ConfigModel)ConfigModelFactory.bulid();
		} 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 {
		//http://localhost:8080/mvc/book.aciton?methodName=list
		String uri = req.getRequestURI();
//		要拿到/book,就是最后一个/到最后一个点的位置
		uri=uri.substring(uri.lastIndexOf("/")
						, uri.lastIndexOf("."));
		//Action action = actions.get(uri);
		//相比于上一种从map集合中获取子控制器,当前需要获取config.xml中的全路径名,然后反射实例化
		ActionModel actionModel = ConfigModel.pop(uri);
		if(actionModel == null) {
			throw new RuntimeException("action 配置 error");
		}
		String type = actionModel.getType();//配置文件中的全路径名 action子控制器的全路径名
		try {
			Action action = (Action) Class.forName(type).newInstance();//action的实例
			action.execute(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
}

接下来就是我们的页面代码啦:

<%@ 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>
</head>
<body>
<h3>优化</h3>
<a href="${pageContext.request.contextPath }/book.action?methodName=add">增加</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=del">删除</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=edit">修改</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=list">查询</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=load">回显</a>
</body>
</html>

运行结果:

如果我们点击增加的话,控制就会运行如下:

 

二、参数传递封装优化

<h3>参数传递封装优化</h3>
<a href="${pageContext.request.contextPath }/book.action?methodName=add & bid=989898 & bname=laoliu & price=89">增加</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=del">删除</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=edit">修改</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=list">查询</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=load">回显</a>

优化前的BookAction:

package com.zlp.web;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.zlp.entity.Book;
import com.zlp.framework.Action;
import com.zlp.framework.ActionSupport;
 
public class BookAction extends ActionSupport {
	
	private void list(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("在用一个servlet中调用 list 方法");
	}
	
	private void load(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("在用一个servlet中调用 list 方法");
	}
	
	private void edit(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("在用一个servlet中调用 edit 方法");
	}
 
	private void del(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("在用一个servlet中调用 del 方法");
	}
 
	private void add(HttpServletRequest req, HttpServletResponse resp) {
		String bid = req.getParameter("bid");
		String bname = req.getParameter("bname");
		String price = req.getParameter("price");
		Book book = new Book();
		book.setBid(Integer.valueOf(bid));
		book.setBname(bname);
		book.setPrice(Float.valueOf(price));
		System.out.println("在用一个servlet中调用 add 方法"+book);
	}
}
 

我们可以建一个模型驱动接口,使BookAction实现该接口,在中央控制器中将所有要接收的参数封装到模型接口中来实现代码的简易

package com.zlp.framework;
 
/**
 * 模型驱动接口,接收前台JSP传递的参数,并且封装到实体类中
 * @author zlp
 *
 */
public interface ModelDriven<T> {
//	拿到将要封装的类实例   ModelDriven.getModel() ---> new Book();
	T getModel();
}

改造 DispatcherServlet:

package com.zlp.framework;
 
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 
import javax.management.RuntimeErrorException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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.zlp.web.BookAction;
 
 
 
/**
 * 中央控制器:
 * 主要职能:接受浏览器请求,找到对应的处理人
 * 
 * @author zlp
 *
 */
@WebServlet("*.action")
public class DispatcherServlet extends HttpServlet{
	//private Map<String, Action> actions=new HashMap<String, Action>();
	
//	通过建模我们可以知道,最终configModel对象会包含config.xml中所有子控制器的信息
//	同时为了解决中央控制器能够动态加载保存子控制器的信息,那么我们只需要引入congigModel对象即可
	private ConfigModel ConfigModel;
	
	
	
	//	程序启动时,只会加载一次
	@Override
	public void init() throws ServletException {
		//actions.put("/book", new BookAction());
//		actions.put("/order", new BooKAction());
		try {
			ConfigModel = (ConfigModel)ConfigModelFactory.bulid();
		} 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 {
		//http://localhost:8080/mvc/book.aciton?methodName=list
		String uri = req.getRequestURI();
//		要拿到/book,就是最后一个/到最后一个点的位置
		uri=uri.substring(uri.lastIndexOf("/")
						, uri.lastIndexOf("."));
		//Action action = actions.get(uri);
		//相比于上一种从map集合中获取子控制器,当前需要获取config.xml中的全路径名,然后反射实例化
		ActionModel actionModel = ConfigModel.pop(uri);
		if(actionModel == null) {
			throw new RuntimeException("action 配置 error");
		}
		String type = actionModel.getType();//配置文件中的全路径名 action子控制器的全路径名
		try {
			Action action = (Action) Class.forName(type).newInstance();//action的实例
			//因为action是bookAction而bookAction实现了ModelDriven接口
			if(action instanceof ModelDriven) {
				//所以可以将其进行向下转型
				ModelDriven md = (ModelDriven)action;
				//model指的是bookAction中的book类实例
				Object model = md.getModel();
				//给model中的属性赋值,要接受前端jsp传递的参数  req.getParameterMap()
				//PropertyUtils.getIndexedProperty(bean, name) 从某个对象中取某个值
				
				//将前端所有参数值封装进实体类
				BeanUtils.populate(model, req.getParameterMap());
				
			}
//			目标:正式调用此方法之前 book中的属性要被赋值
			action.execute(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
}

三、返回值页面跳转优化

package com.zlp.framework;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * 子控制器:
 * 对应请求的处理人
 * @author zlp
 *
 */
public interface Action {
	String execute(HttpServletRequest req, HttpServletResponse resp);
}
 

以上是对子控制器进行改造

ActionSupport

package com.zlp.framework;
 
import java.lang.reflect.Method;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class ActionSupport implements Action{
 
	@Override
	public String execute(HttpServletRequest req, HttpServletResponse resp) {
		String methodName = req.getParameter("methodName");
//		methodName可能是add/del/edit/list/load/xxx/yyy/aaa...
//		前台传递什么方法,就调用当前类的对应方法
		try {
			Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
			m.setAccessible(true);
//			调用当前类实例的methodName方法
			return (String) m.invoke(this, req,resp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null; 
	}
	
	private void list(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("在用一个servlet中调用 list 方法");
	}
	
	private void load(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("在用一个servlet中调用 list 方法");
	}
	
	private void edit(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("在用一个servlet中调用 edit 方法");
	}
 
	private void del(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("在用一个servlet中调用 del 方法");
	}
 
	private void add(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("在用一个servlet中调用 add 方法");
	}
}

DispatcherServlet

package com.zlp.framework;
 
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 
import javax.management.RuntimeErrorException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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.zlp.web.BookAction;
 
 
 
/**
 * 中央控制器:
 * 主要职能:接受浏览器请求,找到对应的处理人
 * 
 * @author zlp
 *
 */
@WebServlet("*.action")
public class DispatcherServlet extends HttpServlet{
	//private Map<String, Action> actions=new HashMap<String, Action>();
	
//	通过建模我们可以知道,最终configModel对象会包含config.xml中所有子控制器的信息
//	同时为了解决中央控制器能够动态加载保存子控制器的信息,那么我们只需要引入congigModel对象即可
	private ConfigModel ConfigModel;
	
	
	
	//	程序启动时,只会加载一次
	@Override
	public void init() throws ServletException {
		//actions.put("/book", new BookAction());
//		actions.put("/order", new BooKAction());
		try {
			ConfigModel = (ConfigModel)ConfigModelFactory.bulid();
		} 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 {
		//http://localhost:8080/mvc/book.aciton?methodName=list
		String uri = req.getRequestURI();
//		要拿到/book,就是最后一个/到最后一个点的位置
		uri=uri.substring(uri.lastIndexOf("/")
						, uri.lastIndexOf("."));
		//Action action = actions.get(uri);
		//相比于上一种从map集合中获取子控制器,当前需要获取config.xml中的全路径名,然后反射实例化
		ActionModel actionModel = ConfigModel.pop(uri);
		if(actionModel == null) {
			throw new RuntimeException("action 配置 error");
		}
		String type = actionModel.getType();//配置文件中的全路径名 action子控制器的全路径名
		try {
			Action action = (Action) Class.forName(type).newInstance();//action的实例
			//因为action是bookAction而bookAction实现了ModelDriven接口
			if(action instanceof ModelDriven) {
				//所以可以将其进行向下转型
				ModelDriven md = (ModelDriven)action;
				//model指的是bookAction中的book类实例
				Object model = md.getModel();
				//给model中的属性赋值,要接受前端jsp传递的参数  req.getParameterMap()
				//PropertyUtils.getIndexedProperty(bean, name) 从某个对象中取某个值
				
				//将前端所有参数值封装进实体类
				BeanUtils.populate(model, req.getParameterMap());
				
			}
//			正式调用此方法之前 book中的属性要被赋值
			String result = action.execute(req,resp);
			ForwardModel forwardModel = actionModel.pop(result);
//			if(forwardModel == null)
//				throw new RuntimeException("forward config error");
//			/bookList.jsp /index.jsp
			String path = forwardModel.getPath();
			//拿到是否需要转发的配置
			boolean redirect = forwardModel.isRedirect();
			if(redirect)
				resp.sendRedirect(path);
			else
			req.getRequestDispatcher(path).forward(req, resp);
			
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
}

ForwardModel

package com.zlp.framework;
 
/**
 * @author zlp
 *
 */
public class ForwardModel {
//	<forward name="failed" path="/reg.jsp" redirect="false" />
	private String name;
	private String path;
	private Boolean 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 Boolean getRedirect() {
		return redirect;
	}
	public void setRedirect(Boolean redirect) {
		this.redirect = redirect;
	}
	
	public ForwardModel() {
		// TODO Auto-generated constructor stub
	}
	public ForwardModel(String name, String path, Boolean redirect) {
		this.name = name;
		this.path = path;
		this.redirect = redirect;
	}
	public boolean isRedirect() {
		return redirect;
	}
	
	
	
	
}

config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<action path="/book" type="com.zlp.web.BookAction">
		<forward name="success" path="/Demo02.jsp" redirect="false" />
		<forward name="failed" path="/Demo03.jsp" redirect="true" />
	</action>
	
	<action path="/order" type="com.zlp.web.OrderAction">
		<forward name="failed" path="/reg.jsp" redirect="false" />
		<forward name="success" path="/login.jsp" redirect="true" />
	</action>
</config>

界面1:

<%@ 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>
</head>
<body>
<h3>参数传递封装的优化</h3>
<a href="${pageContext.request.contextPath }/book.action?methodName=add&bid=989898&bname=laoliu&price=89">增加</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=del">删除</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=edit">修改</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=list">查询</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=load">回显</a>
 
</body>
</html>

界面2:

<%@ 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>
</head>
<body>
	转发页面
</body>
</html>

界面3:

<%@ 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>
</head>
<body>
重定向页面
</body>
</html>

运行结果:

 

四、框架配置文件可变

DispatcherServlet

package com.zlp.framework;
 
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 
import javax.management.RuntimeErrorException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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.zlp.web.BookAction;
 
 
 
/**
 * 中央控制器:
 * 主要职能:接受浏览器请求,找到对应的处理人
 * 
 * @author zlp
 *
 */
//@WebServlet("*.action")
public class DispatcherServlet extends HttpServlet{
	//private Map<String, Action> actions=new HashMap<String, Action>();
	
//	通过建模我们可以知道,最终configModel对象会包含config.xml中所有子控制器的信息
//	同时为了解决中央控制器能够动态加载保存子控制器的信息,那么我们只需要引入congigModel对象即可
	private ConfigModel ConfigModel;
	
	
	
	//	程序启动时,只会加载一次
	@Override
	public void init() throws ServletException {
		//actions.put("/book", new BookAction());
//		actions.put("/order", new BooKAction());
		try {
			//配置地址
			//getInitParameter的作用是拿到web.xml中的servlet信息配置的参数
			String configLocation = this.getInitParameter("configLocation");
			if(configLocation == null || "".equals(configLocation))
				ConfigModel = ConfigModelFactory.bulid();
			else
				ConfigModel = ConfigModelFactory.bulid(configLocation);
		} 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 {
		//http://localhost:8080/mvc/book.aciton?methodName=list
		String uri = req.getRequestURI();
//		要拿到/book,就是最后一个/到最后一个点的位置
		uri=uri.substring(uri.lastIndexOf("/")
						, uri.lastIndexOf("."));
		//Action action = actions.get(uri);
		//相比于上一种从map集合中获取子控制器,当前需要获取config.xml中的全路径名,然后反射实例化
		ActionModel actionModel = ConfigModel.pop(uri);
		if(actionModel == null) {
			throw new RuntimeException("action 配置 error");
		}
		String type = actionModel.getType();//配置文件中的全路径名 action子控制器的全路径名
		try {
			Action action = (Action) Class.forName(type).newInstance();//action的实例
			//因为action是bookAction而bookAction实现了ModelDriven接口
			if(action instanceof ModelDriven) {
				//所以可以将其进行向下转型
				ModelDriven md = (ModelDriven)action;
				//model指的是bookAction中的book类实例
				Object model = md.getModel();
				//给model中的属性赋值,要接受前端jsp传递的参数  req.getParameterMap()
				//PropertyUtils.getIndexedProperty(bean, name) 从某个对象中取某个值
				
				//将前端所有参数值封装进实体类
				BeanUtils.populate(model, req.getParameterMap());
				
			}
//			正式调用此方法之前 book中的属性要被赋值
			String result = action.execute(req,resp);
			ForwardModel forwardModel = actionModel.pop(result);
//			if(forwardModel == null)
//				throw new RuntimeException("forward config error");
//			/bookList.jsp /index.jsp
			String path = forwardModel.getPath();
			//拿到是否需要转发的配置
			boolean redirect = forwardModel.isRedirect();
			if(redirect)
				resp.sendRedirect(req.getServletContext().getContextPath()+path);
			else
			req.getRequestDispatcher(path).forward(req, resp);
			
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
}

然后将config.xml->wuyanzu.xml就可以啦!

这期分享就到这啦~我们下期再见!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值