MVC模式
- 什么是MVC
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,
它是一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码
MVC结构
-
MVC结构
V
jsp/ios/android
C
servlet/action
M
实体域模型(名词)
过程域模型(动词)jsp
<%%>
web 做浏览器请求分发
service 调用dao处理项目业务的
dao 操作数据库
mvc工作原理图
5个增强版mvc步骤
-
1、对存放子控制器action容器的增强
原来为了完成业务需求,需要不断修改框架的代码,这种设计是不合理的
处理方式:参照web.xml的实际方法,来完成中央控制器管理子控制器的动态配置 -
2、 处理结果码的跳转方式:
达到简化代码 -
3、将基础操作放到一个子控制器
-
4、处理jsp传递到后台的参数配置
-
5、解决框架配置文件重名冲突的问题
计算器案例:
1.建模,对 mvc.xml 解析
mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<!-- <action path="/cal_add" type="com.hyf.web.AddCalAction">
<forward name="rs" path="/rs.jsp" redirect="false" />
</action>
<action path="/cal_del" type="com.hyf.web.DelCalAction">
<forward name="rs" path="/rs.jsp" redirect="false"/>
</action> -->
<!--第3次 加强 把上面重复需要的代码简化成下面一个 -->
<action path="/cal" type="com.hyf.web.CalAction">
<forward name="rs" path="/rs.jsp" redirect="false"/>
</action>
</config>
ActionModel ——用来描述 action 标签
package com.hyf.framework;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* 用来描述action标签
*/
public class ActionModel implements Serializable{
private static final long serialVersionUID = 6145949994701469663L;
private Map<String, ForwardModel> forwardModels = new HashMap<String, ForwardModel>();
private String path;
private String type;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public void put(ForwardModel forwardModel){
forwardModels.put(forwardModel.getName(), forwardModel);
}
public ForwardModel get(String name){
return forwardModels.get(name);
}
}
ConfigModel ——用来描述config标签
package com.hyf.framework;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* 用来描述config标签
*/
public class ConfigModel implements Serializable{
private static final long serialVersionUID = -2334963138078250952L;
private Map<String, ActionModel> actionModels = new HashMap<String, ActionModel>();
public void put(ActionModel actionModel){
actionModels.put(actionModel.getPath(), actionModel);
}
public ActionModel get(String name){
return actionModels.get(name);
}
}
ForwardModel 类 ——用来描述config标签
package com.hyf.framework;
import java.io.Serializable;
/**
* 用来描述config标签
*/
public class ForwardModel implements Serializable {
private static final long serialVersionUID = -8587690587750366756L;
private String name;
private String path;
private String 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 String getRedirect() {
return redirect;
}
public void setRedirect(String redirect) {
this.redirect = redirect;
}
}
ConfigModelFactory ——工厂模式创建config建模对象
package com.hyf.framework;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ConfigModelFactory {
private ConfigModelFactory() {
}
private static ConfigModel configModel = null;
public static ConfigModel newInstance() throws Exception {
return newInstance("mvc.xml");
}
/**
* 工厂模式创建config建模对象
*
* @param path
* @return
* @throws Exception
*/
public static ConfigModel newInstance(String path) throws Exception {
if (null != configModel) {
return configModel;
}
ConfigModel configModel = new ConfigModel();
InputStream is = ConfigModelFactory.class.getResourceAsStream(path);
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(is);
List<Element> actionEleList = doc.selectNodes("/config/action");
ActionModel actionModel = null;
ForwardModel forwardModel = null;
for (Element actionEle : actionEleList) {
actionModel = new ActionModel();
actionModel.setPath(actionEle.attributeValue("path"));
actionModel.setType(actionEle.attributeValue("type"));
List<Element> forwordEleList = actionEle.selectNodes("forward");
for (Element forwordEle : forwordEleList) {
forwardModel = new ForwardModel();
forwardModel.setName(forwordEle.attributeValue("name"));
forwardModel.setPath(forwordEle.attributeValue("path"));
forwardModel.setRedirect(forwordEle.attributeValue("redirect"));
actionModel.put(forwardModel);
}
configModel.put(actionModel);
}
return configModel;
}
}
Cal——实体类
package com.hyf.entity;
public class Cal {
private String num1;
private String num2;
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 String toString() {
return "Cal [num1=" + num1 + ", num2=" + num2 + "]";
}
public Cal(String num1, String num2) {
super();
this.num1 = num1;
this.num2 = num2;
}
public Cal() {
super();
}
}
DispatcherServlet ——中央控制器
package com.hyf.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 org.apache.commons.beanutils.BeanUtils;
/**
* 中央控制器
* 作用:
* 接受用户请求,通过用户请求的url寻找指定的子控制器去处理业务
*
* @author 17628
*
*/
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = -3841666976794538752L;
/**
* 用来存放 子控制器
*/
private ConfigModel configModel = null;
public void init() {
// actionMap.put("/cal_add", new AddCalAction());
// actionMap.put("/cal_del", new DelCalAction());
try {
String mvcXmlLocation = this.getInitParameter("mvcXmlLocation"); // 5.mvc的默认地址
if(null == mvcXmlLocation || "" .equals(mvcXmlLocation)) {
mvcXmlLocation="mvc.xml";
}
System.out.println(mvcXmlLocation);
// 1加强:获得到 action 标签 <action path="/cal" type="com.hyf.web.CalAction">
//<forward name="rs" path="/rs.jsp" redirect="false"/></action>
configModel =ConfigModelFactory.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String url=req.getRequestURI();
url=url.substring(url.lastIndexOf("/"),url.lastIndexOf("."));
/* Action action = actionMap.get(url);*/
ActionModel actionModel = configModel.get(url);
try {
// 2 2如果客户没有配置 需要提示
if(actionModel == null) {
throw new RuntimeException("您还未配置子控制器来处理用户请求");
}
//1 类地址 进行实例化
Action action = (Action) Class.forName(actionModel.getType()).newInstance();
//4 加强
if(action instanceof ModelDriver) {
ModelDriver modelDriver = (ModelDriver) action;
Object model = modelDriver.getModel();
// 给 model 赋值了,那么意味这在调用add/del不在是空值
BeanUtils.populate(model, req.getParameterMap());
}
// 3跳转页面加强. 获取到实现接口 Action 的 ActionSupport类返回的值,
//在然后通过 code获在actionModel 类中的forwarModel类.就可以提取到 forwar 标签中的 name(code值) path (跳转的jsp页面) redirect(是否重定向)
String code = action.execute(req, resp);
ForwardModel forwardModel = actionModel.get(code);
if("false".equals(forwardModel.getRedirect())) {
req.getRequestDispatcher(forwardModel.getPath()).forward(req, resp);
}else {
// 2注意:默认会缺损项目名 req.getContextPath()
resp.sendRedirect(req.getContextPath()+ forwardModel.getPath());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Action 接口——子控制器
package com.hyf.framework;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 子控制器:
* 作用:具体处理用户请求的类(实现Action接口类)
*
* @author 17628
*
*/
public interface Action {
String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}
ActionSupport ——增强控制器
package com.hyf.framework;
import java.io.IOException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 增强控制器
* 作用: 将一组操作数组放到一个子控制器去完成
*
* @author 17628
*
*/
public class ActionSupport implements Action {
public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 从前台传递需要调用的方法名到后台,实现动态方法调用 url后面加?methodName_xxx(add/del)方法名
// 这个类主要是通过获 methodName 调用CalAction类中的方法名字和methodName相等,得到返回值code,返回值则代表着跳转到哪一个 jsp 页面 this代表 CalAction类
String code =null;
String methodName = req.getParameter("methodName");
try {
Method m = this.getClass().getDeclaredMethod(methodName, Http ServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
// 获得方法 返回值
code=(String) m.invoke(this, req,resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return code;
}
}
ModelDriver 接口——模型驱动接口
作用:给对应处理业务的子控制器中包含的实体类进行jsp参数封装
package com.hyf.framework;
/**
* 模型驱动接口
* 作用:给对应处理业务的子控制器中包含的实体类进行jsp参数封装
* @author 17628
*
*/
public interface ModelDriver<T> {
T getModel();
}
CalAction 类
作用:把方法都写在一起,通过jsp前端methodName传值调用这个类中的方法
package com.hyf.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.hyf.entity.Cal;
import com.hyf.framework.ActionSupport;
import com.hyf.framework.ModelDriver;
public class CalAction extends ActionSupport implements ModelDriver<Cal>{
private Cal cal = new Cal();
// 加
public String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 第4部加强 替代的代码
/*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()));
// req.getRequestDispatcher("rs.jsp").forward(req, resp);
return "rs";
}
// 减
public String del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("rs",Integer.valueOf(cal.getNum1())-Integer.valueOf(cal.getNum2()));
return "rs";
}
//乘
public String ride(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("rs",Integer.valueOf(cal.getNum1())*Integer.valueOf(cal.getNum2()));
return "rs";
}
// 除
public String divide(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("rs",Integer.valueOf(cal.getNum1())/Integer.valueOf(cal.getNum2()));
return "rs";
}
public Cal getModel() {
return cal;
}
}
rs.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>
<script type="text/javascript">
function doSub(v){
if(v==1){
calForm.action = "${pageContext.request.contextPath}/cal.action?methodName=add";
}else if(v==2){
calForm.action = "${pageContext.request.contextPath}/cal.action?methodName=del";
}else if(v==3){
calForm.action = "${pageContext.request.contextPath}/cal.action?methodName=ride";
}else if(v==4){
calForm.action = "${pageContext.request.contextPath}/cal.action?methodName=divide";
}
calForm.submit();
}
</script>
</head>
<body>
<form id="calForm" action="" method="post">
num1:<input name="num1"><br>
num2:<input name="num2"><br>
<button onclick="doSub(1)">+</button>
<button onclick="doSub(2)">-</button>
<button onclick="doSub(3)">*</button>
<button onclick="doSub(4)">/</button>
</form>
</body>
</html>
rs.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>
结果:${rs}
</body>
</html>
输出:
加:
减:
乘:
除: