自定义MVC

1.思维导图

2.前台传递methodNam到后台,实际就是想调用当前类对象的methodNam方法:

public class BookServlet extends HttpServlet{
	
	@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 methodName = req.getParameter("methodName");
		try {
			Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
			//打开权限
			m.setAccessible(true);
			//调用
			m.invoke(this,req,resp);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	
	private void list(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("bookDao.list()...");
		
	}

	private void edit(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("bookDao.edit()...");
	}

	private void delete(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("bookDao.delete()...");
		
	}

	private void add(HttpServletRequest req, HttpServletResponse resp) {
		System.out.println("bookDao.add()...");
	}
	
	
	private void load(HttpServletRequest req, HttpServletResponse resp) {
		//修改页面数据回显
		System.out.println("bookDao.load()...");
	}
	
	private void ref(HttpServletRequest req, HttpServletResponse resp) {
		
		System.out.println("bookDao.ref()...");
	}

 2.1初始化子控制器容器(集合),经过初始化,actions容器内部就有了子控制器:

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String uri = req.getRequestURI();
		uri = uri.substring(uri.lastIndexOf("/"), uri.lastIndexOf("."));
		//action=BookAction
		//ActionSupport action=new BookAction();
		ActionSupport action = actions.get(uri);
		action.execute(req, resp);
		
	}

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>
目前多数人的增删改查的代码
<a href="${pageContext.request.contextPath }/book/add">增加</a>
<a href="${pageContext.request.contextPath }/book/delete">删除</a>
<a href="${pageContext.request.contextPath }/book/edit">修改</a>
<a href="${pageContext.request.contextPath }/book/list">出现</a>
<hr>

增删改查的代码V2.0
<a href="${pageContext.request.contextPath }/book.action?methodName=add">增加</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=delete">删除</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=edit">修改</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=list">出现</a>
<hr>

增删改查的代码V3.0
<a href="${pageContext.request.contextPath }/book.action?methodName=load">回显</a>
<a href="${pageContext.request.contextPath }/book.action?methodName=ref">关联</a>


</body>
</html>

4.编写过程中遇到的报错

4.1 问题出现在“edit”编写错误

5.  运行的结果:

5.1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值