1. 什么是MVC
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,
它是一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码
核心思想:各司其职
2. MVC结构
web 做浏览器请求分发
service 调用dao处理项目业务的
dao 操作数据库
3. 自定义MVC工作原理图
主控制动态调用子控制器调用完成具体的业务逻辑
(火车、控制台、车轨)
请求、主控制器、子控制器
4.上课案例
用mvc完成加减乘除
中央控制器
package com.houyitao.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.houyitao.web.AddCalAction;
import com.houyitao.web.DelCalAction;
import com.houyitao.web.DivisionCalAction;
import com.houyitao.web.MultiplyCalAction;
/**
* 中央控制器
* 作用:接受请求,通过请求寻找请求的对应的子控制器
* @author hou
*
*/
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 1971683367441467463L;
private Map actionMap=new HashMap<>();
public void init() {
actionMap.put("/addCal", new AddCalAction());
actionMap.put("/delCal", new DelCalAction());
actionMap.put("/mtyCal",new MultiplyCalAction());
actionMap.put("/dinCal", new DivisionCalAction());
//
}
@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
init();
String url=req.getRequestURI();
url=url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
Action action = actionMap.get(url);
action.execute(req, resp);
//
}
}
子控制器
package com.houyitao.framework;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 子控制器
* 作用:用来处理浏览器发送过来的请求
* @author hou
*
*/
public interface Action {
void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}
创建四个实现请求的类
加
package com.houyitao.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.houyitao.entity.Cal;
import com.houyitao.framework.Action;
public class MultiplyCalAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String num1=req.getParameter("num1");
String num2=req.getParameter("num2");
Cal cal=new Cal(Integer.parseInt(num1), Integer.parseInt(num2));
req.setAttribute("res", cal.getNum1()*cal.getNum2());
req.getRequestDispatcher("NewFile1.jsp").forward(req, resp);
}
}
减
package com.houyitao.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.houyitao.entity.Cal;
import com.houyitao.framework.Action;
public class DelCalAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String num1=req.getParameter("num1");
String num2=req.getParameter("num2");
Cal cal=new Cal(Integer.parseInt(num1), Integer.parseInt(num2));
req.setAttribute("res", cal.getNum1()-cal.getNum2());
req.getRequestDispatcher("NewFile1.jsp").forward(req, resp);
}
}
乘
package com.houyitao.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.houyitao.entity.Cal;
import com.houyitao.framework.Action;
public class MultiplyCalAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String num1=req.getParameter("num1");
String num2=req.getParameter("num2");
Cal cal=new Cal(Integer.parseInt(num1), Integer.parseInt(num2));
req.setAttribute("res", cal.getNum1()*cal.getNum2());
req.getRequestDispatcher("NewFile1.jsp").forward(req, resp);
}
}
除
package com.houyitao.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.houyitao.entity.Cal;
import com.houyitao.framework.Action;
public class DivisionCalAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String num1=req.getParameter("num1");
String num2=req.getParameter("num2");
Cal cal=new Cal(Integer.parseInt(num1), Integer.parseInt(num2));
req.setAttribute("res", cal.getNum1()/cal.getNum2());
req.getRequestDispatcher("NewFile1.jsp").forward(req, resp);
}
}
创建实体类
package com.houyitao.entity;
public class Cal {
private int num1;
private int num2;
public int getNum1() {
return num1;
}
public void setNum1(int num1) {
this.num1 = num1;
}
public int getNum2() {
return num2;
}
public void setNum2(int num2) {
this.num2 = num2;
}
public Cal() {
super();
}
public Cal(int num1, int num2) {
super();
this.num1 = num1;
this.num2 = num2;
}
}
配置xml
web_mvc
dispatcherServlet
com.houyitao.framework.DispatcherServlet
dispatcherServlet
*.action
jsp展示页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
运行结果页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
结果:${ res }
返回
输出结果
乘法:
** 通过XML对自定义mvc框架进行增强**
首先我们需要的五个架包:
工具类:
ActionModel:用来描述action标签
package com.houyitao.framework;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* 用来描述action标签
* @author Administrator
*
*/
public class ActionModel implements Serializable{
private static final long serialVersionUID = 6145949994701469663L;
private Map forwardModels = new HashMap();
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.houyitao.framework;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* 用来描述config标签
* @author Administrator
*
*/
public class ConfigModel implements Serializable{
private static final long serialVersionUID = -2334963138078250952L;
private Map actionModels = new HashMap();
public void put(ActionModel actionModel){
actionModels.put(actionModel.getPath(), actionModel);
}
public ActionModel get(String name){
return actionModels.get(name);
}
}
ConfigModelFactory工厂模式创建config建模对象
package com.houyitao.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 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 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;
}
public static void main(String[] args) {
try {
ConfigModel configModel = ConfigModelFactory.newInstance();
ActionModel actionModel = configModel.get("/loginAction");
ForwardModel forwardModel = actionModel.get("failed");
System.out.println(actionModel.getType());
System.out.println(forwardModel.getPath());
} catch (Exception e) {
e.printStackTrace();
}
}
}
ForwardModel用来描述forward标签
package com.houyitao.framework;
import java.io.Serializable;
/**
* 用来描述forward标签
* @author Administrator
*
*/
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;
}
}
xml
**解决了在框架代码中去改动,以便于完成客户需求,这个是不合理的**
**DispatcherServlet中央控制器: 作用:接受请求,通过请求寻找请求的对应的子控制器**
package com.houyitao.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.houyitao.web.AddCalAction;
import com.houyitao.web.DelCalAction;
import com.houyitao.web.DivisionCalAction;
import com.houyitao.web.MultiplyCalAction;
/**
* 中央控制器
* 作用:接受请求,通过请求寻找请求的对应的子控制器
* @author hou
*
*/
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 1971683367441467463L;
private Map actionMap=new HashMap<>();
// 在configModel对象中包含了所有子控制器信息
private ConfigModel configModel;
public void init() {
// actionMap.put("/addCal", new AddCalAction());
// actionMap.put("/delCal", new DelCalAction());
// actionMap.put("/mtyCal",new MultiplyCalAction());
// actionMap.put("/dinCal", new DivisionCalAction());
try {
configModel=ConfigModelFactory.newInstance();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@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
init();
String url=req.getRequestURI();
url=url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
// Action action = actionMap.get(url);
ActionModel actionModel = configModel.get(url);
if(actionModel==null) {
throw new RuntimeException("没有配置action标签,找不到对应的子控制器来处理浏览器");
}
try {
Action action =(Action) Class.forName(actionModel.getType()).newInstance();
action.execute(req, resp);
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Action子控制器 作用:用来处理浏览器发送过来的请求
package com.houyitao.framework;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 子控制器
* 作用:用来处理浏览器发送过来的请求
* @author hou
*
*/
public interface Action {
void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}
实体类:(Cal)后续实体类不需要进行修改
package com.houyitao.entity;
public class Cal {
private int num1;
private int num2;
public int getNum1() {
return num1;
}
public void setNum1(int num1) {
this.num1 = num1;
}
public int getNum2() {
return num2;
}
public void setNum2(int num2) {
this.num2 = num2;
}
public Cal() {
super();
}
public Cal(int num1, int num2) {
super();
this.num1 = num1;
this.num2 = num2;
}
}
加(AddCalAction)
package com.houyitao.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.houyitao.entity.Cal;
import com.houyitao.framework.Action;
public class AddCalAction implements Action {
private static final long serialVersionUID = 1L;
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String num1=req.getParameter("num1");
String num2=req.getParameter("num2");
Cal cal=new Cal(Integer.parseInt(num1), Integer.parseInt(num2));
req.setAttribute("res", cal.getNum1()+cal.getNum2());
req.getRequestDispatcher("NewFile1.jsp").forward(req, resp);
}
}
减(DelCalAction)
package com.houyitao.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.houyitao.entity.Cal;
import com.houyitao.framework.Action;
public class DelCalAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String num1=req.getParameter("num1");
String num2=req.getParameter("num2");
Cal cal=new Cal(Integer.parseInt(num1), Integer.parseInt(num2));
req.setAttribute("res", cal.getNum1()-cal.getNum2());
req.getRequestDispatcher("NewFile1.jsp").forward(req, resp);
}
}
jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
结果页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
结果:${ res }
返回
2.1中央控制器(DispatcherServlet ) 作用:接受请求,通过请求寻找请求的对应的子控制器
package com.houyitao.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.houyitao.web.AddCalAction;
import com.houyitao.web.DelCalAction;
import com.houyitao.web.DivisionCalAction;
import com.houyitao.web.MultiplyCalAction;
/**
* 中央控制器
* 作用:接受请求,通过请求寻找请求的对应的子控制器
* @author hou
*
*/
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 1971683367441467463L;
private Map actionMap=new HashMap<>();
// 在configModel对象中包含了所有子控制器信息
private ConfigModel configModel;
public void init() {
try {
configModel=ConfigModelFactory.newInstance();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@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
init();
String url=req.getRequestURI();
url=url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
ActionModel actionModel = configModel.get(url);
try {
Action action =(Action) Class.forName(actionModel.getType()).newInstance();
String code=action.execute(req, resp);
ForwardModel forwardModel = actionModel.get(code);
if(forwardModel!=null) {
String jspPath = forwardModel.getPath();
if("false".equals(forwardModel.getRedirect())) {
// 做转发的处理
req.getRequestDispatcher(jspPath).forward(req, resp);
}else {
resp.sendRedirect(req.getContextPath()+jspPath);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.2子控制器(Action )作用:用来处理浏览器发送过来的请求
package com.houyitao.framework;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 子控制器
* 作用:用来处理浏览器发送过来的请求
* @author hou
*
*/
public interface Action {
String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}
2.3mvc.xml
加(AddCalAction)
package com.houyitao.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.houyitao.entity.Cal;
import com.houyitao.framework.Action;
public class AddCalAction implements Action {
private static final long serialVersionUID = 1L;
@Override
public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String num1=req.getParameter("num1");
String num2=req.getParameter("num2");
Cal cal=new Cal(Integer.parseInt(num1), Integer.parseInt(num2));
req.setAttribute("res", cal.getNum1()+cal.getNum2());
// req.getRequestDispatcher("NewFile1.jsp").forward(req, resp);
return "res";
}
}
减(DelCalAction)
package com.houyitao.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.houyitao.entity.Cal;
import com.houyitao.framework.Action;
public class DelCalAction implements Action {
@Override
public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String num1=req.getParameter("num1");
String num2=req.getParameter("num2");
Cal cal=new Cal(Integer.parseInt(num1), Integer.parseInt(num2));
req.setAttribute("res", cal.getNum1()-cal.getNum2());
// req.getRequestDispatcher("NewFile1.jsp").forward(req, resp);
return "res";
}
}
中央控制器(DispatcherServlet)
package com.houyitao.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.houyitao.web.AddCalAction;
import com.houyitao.web.DelCalAction;
import com.houyitao.web.DivisionCalAction;
import com.houyitao.web.MultiplyCalAction;
/**
* 中央控制器
* 作用:接受请求,通过请求寻找请求的对应的子控制器
* @author hou
*
*/
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 1971683367441467463L;
private Map actionMap=new HashMap<>();
// 在configModel对象中包含了所有子控制器信息
private ConfigModel configModel;
public void init() {
try {
configModel=ConfigModelFactory.newInstance();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@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
init();
String url=req.getRequestURI();
url=url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
ActionModel actionModel = configModel.get(url);
try {
Action action =(Action) Class.forName(actionModel.getType()).newInstance();
String code=action.execute(req, resp);
ForwardModel forwardModel = actionModel.get(code);
if(forwardModel!=null) {
String jspPath = forwardModel.getPath();
if("false".equals(forwardModel.getRedirect())) {
// 做转发的处理
req.getRequestDispatcher(jspPath).forward(req, resp);
}else {
resp.sendRedirect(req.getContextPath()+jspPath);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
** 子控制器(Action)**
package com.houyitao.framework;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 子控制器
* 作用:用来处理浏览器发送过来的请求
* @author hou
*
*/
public interface Action {
String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}
增强版的子控制器(ActionSupport )
package com.houyitao.framework;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ActionSupport implements Action {
/**
* 增强版的子控制器
* 原来的子控制器只能处理一个请求
* 有时候 用户请求是多个 但是都是操作同一个实体类(同一个表)那么原有的子控制器代码编写繁琐
* 增强版的作用就是:将一组的操作放到一个Action中
* @author hou
*
*/
@Override
public final String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String methodName=req.getParameter("methodName");
String code=null;
// this在这里指的是CalAction它的一个类实例
try {
Method m = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
m.setAccessible(true);
code = (String)m.invoke(this, req,resp);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return code;
}
}
mvc.xml
加减乘除:
package com.houyitao.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.houyitao.entity.Cal;
import com.houyitao.framework.ActionSupport;
public class CalAction extends ActionSupport{
public String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String num1=req.getParameter("num1");
String num2=req.getParameter("num2");
Cal cal=new Cal(Integer.parseInt(num1), Integer.parseInt(num2));
req.setAttribute("res", cal.getNum1()+cal.getNum2());
// req.getRequestDispatcher("NewFile1.jsp").forward(req, resp);
return "res";
}
public String del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String num1=req.getParameter("num1");
String num2=req.getParameter("num2");
Cal cal=new Cal(Integer.parseInt(num1), Integer.parseInt(num2));
req.setAttribute("res", cal.getNum1()-cal.getNum2());
// req.getRequestDispatcher("NewFile1.jsp").forward(req, resp);
return "res";
}
public String mty(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String num1=req.getParameter("num1");
String num2=req.getParameter("num2");
Cal cal=new Cal(Integer.parseInt(num1), Integer.parseInt(num2));
req.setAttribute("res", cal.getNum1()/cal.getNum2());
// req.getRequestDispatcher("NewFile1.jsp").forward(req, resp);
return "res";
}
public String din(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String num1=req.getParameter("num1");
String num2=req.getParameter("num2");
Cal cal=new Cal(Integer.parseInt(num1), Integer.parseInt(num2));
req.setAttribute("res", cal.getNum1()*cal.getNum2());
// req.getRequestDispatcher("NewFile1.jsp").forward(req, resp);
return "res";
}
}
jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
结果页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
结果:${ res }
返回
BeanUtils.populate(calBean, parameterMap);
中央控制器(DispatcherServlet)
package com.houyitao.framework;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
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;
import com.houyitao.web.AddCalAction;
import com.houyitao.web.DelCalAction;
import com.houyitao.web.DivisionCalAction;
import com.houyitao.web.MultiplyCalAction;
/**
* 中央控制器
* 作用:接受请求,通过请求寻找请求的对应的子控制器
* @author hou
*
*/
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 1971683367441467463L;
private Map actionMap=new HashMap<>();
// 在configModel对象中包含了所有子控制器信息
private ConfigModel configModel;
public void init() {
try {
configModel=ConfigModelFactory.newInstance();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@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
init();
String url=req.getRequestURI();
url=url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
ActionModel actionModel = configModel.get(url);
try {
Action action =(Action) Class.forName(actionModel.getType()).newInstance();
if(action instanceof ModeIDrivern) {
ModeIDrivern modeIDrivern=(ModeIDrivern) action;
// 此时的model的所有属性值是null的
Object model = modeIDrivern.getModel();
BeanUtils.populate(model, req.getParameterMap());
// 我们可以将req.getParameterMap();的值通过反射的方式将其塞进model实例中
// Map map = req.getParameterMap();
// Set> set = map.entrySet();
// Class extends Object> clz = model.getClass();
// for (Entry entry : set) {
// Field field = clz.getField(entry.getKey());
// field.setAccessible(true);
// field.set(model, entry.getValue());
// }
}
String code=action.execute(req, resp);
ForwardModel forwardModel = actionModel.get(code);
if(forwardModel!=null) {
String jspPath = forwardModel.getPath();
if("false".equals(forwardModel.getRedirect())) {
// 做转发的处理
req.getRequestDispatcher(jspPath).forward(req, resp);
}else {
resp.sendRedirect(req.getContextPath()+jspPath);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
子控制器(Action)
package com.houyitao.framework;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 子控制器
* 作用:用来处理浏览器发送过来的请求
* @author hou
*
*/
public interface Action {
String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException;
}
ModelDriver接口对Java对象进行赋值(CalAction )
package com.houyitao.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.houyitao.entity.Cal;
import com.houyitao.framework.ActionSupport;
import com.houyitao.framework.ModeIDrivern;
public class CalAction extends ActionSupport implements ModeIDrivern{
private Cal cal=new Cal();
public String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
// String num1=req.getParameter("num1");
// String num2=req.getParameter("num2");
// Cal cal=new Cal(Integer.parseInt(num1), Integer.parseInt(num2));
req.setAttribute("res", cal.getNum1()+cal.getNum2());
// req.getRequestDispatcher("NewFile1.jsp").forward(req, resp);
return "res";
}
public String del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
// String num1=req.getParameter("num1");
// String num2=req.getParameter("num2");
// Cal cal=new Cal(Integer.parseInt(num1), Integer.parseInt(num2));
req.setAttribute("res", cal.getNum1()-cal.getNum2());
// req.getRequestDispatcher("NewFile1.jsp").forward(req, resp);
return "res";
}
public String mty(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
// String num1=req.getParameter("num1");
// String num2=req.getParameter("num2");
// Cal cal=new Cal(Integer.parseInt(num1), Integer.parseInt(num2));
req.setAttribute("res", cal.getNum1()*cal.getNum2());
// req.getRequestDispatcher("NewFile1.jsp").forward(req, resp);
return "res";
}
public String din(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
// String num1=req.getParameter("num1");
// String num2=req.getParameter("num2");
// Cal cal=new Cal(Integer.parseInt(num1), Integer.parseInt(num2));
req.setAttribute("res", cal.getNum1()/cal.getNum2());
// req.getRequestDispatcher("NewFile1.jsp").forward(req, resp);
return "res";
}
@Override
public Cal getModel() {
// TODO Auto-generated method stub
return cal;
}
}
mvc.xml
jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
结果页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
结果:${ res }
返回
package com.houyitao.framework;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
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;
import com.houyitao.web.AddCalAction;
import com.houyitao.web.DelCalAction;
import com.houyitao.web.DivisionCalAction;
import com.houyitao.web.MultiplyCalAction;
/**
* 中央控制器
* 作用:接受请求,通过请求寻找请求的对应的子控制器
* @author hou
*
*/
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 1971683367441467463L;
private Map actionMap=new HashMap<>();
// 在configModel对象中包含了所有子控制器信息
private ConfigModel configModel;
public void init() {
//使得框架的配置文件可变
try {
String xmlPath=this.getInitParameter("xmlPath");
if(xmlPath==null||"".equals(xmlPath)) {
configModel=ConfigModelFactory.newInstance();
}else {
configModel=ConfigModelFactory.newInstance(xmlPath);
}
configModel=ConfigModelFactory.newInstance();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@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
init();
String url=req.getRequestURI();
url=url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
ActionModel actionModel = configModel.get(url);
try {
Action action =(Action) Class.forName(actionModel.getType()).newInstance();
if(action instanceof ModeIDrivern) {
ModeIDrivern modeIDrivern=(ModeIDrivern) action;
// 此时的model的所有属性值是null的
Object model = modeIDrivern.getModel();
BeanUtils.populate(model, req.getParameterMap());
// 我们可以将req.getParameterMap();的值通过反射的方式将其塞进model实例中
// Map map = req.getParameterMap();
// Set> set = map.entrySet();
// Class extends Object> clz = model.getClass();
// for (Entry entry : set) {
// Field field = clz.getField(entry.getKey());
// field.setAccessible(true);
// field.set(model, entry.getValue());
// }
}
String code=action.execute(req, resp);
ForwardModel forwardModel = actionModel.get(code);
if(forwardModel!=null) {
String jspPath = forwardModel.getPath();
if("false".equals(forwardModel.getRedirect())) {
// 做转发的处理
req.getRequestDispatcher(jspPath).forward(req, resp);
}else {
resp.sendRedirect(req.getContextPath()+jspPath);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
在不改动framework文件里面的代码前提下,修改mvc.xml文件,如图,新建一个文件夹,复制framework里的mvc.xml文件
然后在改一个名字:
web.xml
web_mvc
dispatcherServlet
com.houyitao.framework.DispatcherServlet
xmlPath
/mvc3.xml
dispatcherServlet
*.action