【mvc模式】

前言

mvc模式是做项目中一般采用的设计模式

所以需要去详细的去探究mvc的原理和思路。

mvc的概念

MVC全名是Model View Controller
模型(model)视图(view)控制器(controller) 的缩写,

它是一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码

实际图:
在这里插入图片描述

结构:

V (view)
jsp/ios/android(一般由jsp,ios,android文件组成)
C (controller)
servlet/action(一般由servlet,action文件组成)
M (model)
实体域模型(名词)
过程域模型(动词)

传统mvc的使用

也就是你要实现什么方法的时候就提交到写好了的servlet里面

dao层:也就是数据访问层。
servlet层:也就是逻辑处理层。
jsp文件:也就是ui层

在这里插入图片描述

可是那样的话,servlet会越来越多;web.xml配置也越来越多,
这就十分麻烦了,
在这里插入图片描述
而且在每一个servlet里面都可能有很多重复的代码块

在这里插入图片描述

我们今天就要对其进行一个优化。


mvc的优化

首先,我们的优化思路是这样的。
用一个servlet来代替之前所有的servlet.

思路:

中央控制器(DispatcherServlet)
用来获取发送请求的路径名,并截取。然后交给其他 子控制器
子控制器接口(Action)
用来定义方法。
子控制器(&&Action):
列如: addAction 用来进行逻辑处理。

可以看图:

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

先是结构:一个web,一个framework就行

中央控制器

public class DispatcherServlet extends HttpServlet{
	
	
	private Map<String, action> actionMap = new HashMap<>();//用来存储子控制器的位置
	
	public void init() {
		actionMap.put("add", new addServlet());//增加的子控制器
		actionMap.put("del", new delServlet());
		actionMap.put("select", new selectServlet());
		actionMap.put("update", new updateServlet());
	}
	
	
	@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();//使得map里有值
		String requestURI = req.getRequestURI(); //获取请求的路径
		System.out.println("截取前:"+requestURI);
		String url = requestURI.substring(requestURI.lastIndexOf("/")+1, requestURI.lastIndexOf("."));//截取
		System.out.println("截取后:"+url);
		
		action action = actionMap.get(url); //获取map里面的对应关系
		action.execute(req, resp);//调用方法
		
	}
	
}

如果运行的话就是这样;
在这里插入图片描述

子控制器接口

public interface action {
	void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}

子控制器

用两个来进行举例:

public class addServlet implements action{//乘法

	@Override
	public void 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);
		
		
	}

}
public class delServlet implements action{//除法

	@Override
	public void 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);
		
		
	}

}

jsp的调用

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	function toSubmit(val){
		if(val==1){
			ActionForm.action = "${pageContext.request.contextPath }/add.action";
		}
		else if(val==2){
			ActionForm.action = "${pageContext.request.contextPath }/del.action";
		}
		else if(val==3){
			ActionForm.action = "${pageContext.request.contextPath }/select.action";
		}
		else if(val==4){
			ActionForm.action = "${pageContext.request.contextPath }/update.action";
		}
		ActionForm.submit();
	}
</script>

</head>
<body>

<form name="ActionForm" id="ActionForm" action="${pageContext.request.contextPath }/add.action" method="post">
	num1:<input type="text" name="num1" ><br>
	num2:<input type="text" name="num2" ><br>
	<input type="button" value="+" onclick="toSubmit(1)" >
	<input type="button" value="-" onclick="toSubmit(2)" >
	<input type="button" value="*" onclick="toSubmit(3)" >
	<input type="button" value="/" onclick="toSubmit(4)" >

</form>

</body>
</html>

就这样就可以完成一个简单的优化;

结果

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

没问题;


总结

思路不怎么难,主要还是理解中央控制器,和子控制器这个概念

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值