MVC全名:Model View Controller,其中Model(模型层)、View(视图层)、Controller(控制层),
它是一种软件设计典范,用于业务逻辑处理、数据、界面显示分离.
Model(模型):是应用程序中用于处理应用程序数据逻辑的部分,
通常模型对象负责在数据库中存取数据。
View(视图):是应用程序中处理数据显示的部分,通常视图是依据模型数据创建的。
Controller(控制器):是应用程序中处理用户交互的部分,
通常控制器负责从视图读取数据,控制用户输入,并向模型发送数据。
注1:不能跨层调用
注2:只能出现由上而下的调用
三层架构是一个经典的分层思想,将开发模式分为三层,每个人专注自己擅长模块即可;
MVC是一种设计模式,其目的是让html和业务逻辑分开。
分层,结构清晰,耦合性低,大型项目代码的复用性得到极大的提高,开发人员分工明确,提高了开发的效率,维护方便,降低了维护成本。
V(视图层) --->JSP/HTML/freemarker
C(控制层) --->Servlet/Action/Controller
M(模型层) --->entity、dao
entity:实体域模型(名词)
dao:过程域模型(动词)
主控制器
作用:接受请求,通过寻找处理请求的对应的子控制器
package com.tanjie.mvc.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.tanjie.mvc.action.AddAction;
import com.tanjie.mvc.action.Hello;
/**
* 自定义mvc
* @author Administrator
*
*/
public class ActionServlet extends HttpServlet{
//定义一个集合
private Map<String, action> map;
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
//子控制器
map=new HashMap<String, action>();
map.put("/addAction", new AddAction());
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//获取请求路径
String re=req.getRequestURI();
//截取
int start = re.lastIndexOf("/");
int end=re.lastIndexOf(".action");
String sb = re.substring(start, end);
//返回 action
action action = map.get(sb);
action.execte(req, resp);
}
}
子控制器接口
作用: 用来直接处理浏览器发送过来的请求
package com.tanjie.mvc.framework;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Administrator
*
*/
public abstract class action {
protected abstract String execte(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}
配置XML文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>jsp_mvc</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 配置Servlet -->
<servlet>
<servlet-name>ActionServlet</servlet-name>
<servlet-class>com.tanjie.mvc.framework.ActionServlet</servlet-class>
</servlet>
<!-- 映射Servlet -->
<servlet-mapping>
<servlet-name>ActionServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
继承action
package com.tanjie.mvc.action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tanjie.mvc.framework.action;
public class AddAction extends action{
@Override
protected String execte(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//得到表单中的值
String jia = req.getParameter("+");
String jian = req.getParameter("-");
String cheng = req.getParameter("*");
String chu = req.getParameter("/");
String num1 = req.getParameter("num1");
String num2 = req.getParameter("num2");
int num3=0;
//条件判断
if("+".equals(jia)) {
num3=Integer.parseInt(num1)+Integer.parseInt(num2);
}
if("-".equals(jian)) {
num3=Integer.parseInt(num1)-Integer.parseInt(num2);
}
if("*".equals(cheng)) {
num3=Integer.parseInt(num1)*Integer.parseInt(num2);
}
if("/".equals(chu)) {
num3=Integer.parseInt(num1)/Integer.parseInt(num2);
}
//保存并跳转
req.setAttribute("num3", num3);
req.getRequestDispatcher("add.jsp").forward(req, resp);
return null;
}
}
JSP界面调用
<%@ 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>
<form action="addAction.action" method="post">
num1:<input name="num1"><br/>
num2:<input name="num2"><br/>
<input type="submit" value="+" name="+">
<input type="submit" value="-" name="-">
<input type="submit" value="*" name="*">
<input type="submit" value="/" name="/">
</form>
<h2>结果:${num3 }</h2>
</body>
</html>