1.思维导图
?
2.MVC
什么是mvc Model模型,View视图,controller控制层
?
?3.运行原理
4.代码演示
?
?
?
package com.lia.framework;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lia.web.BookAction;
/**
* 目标:
* 根据自定义mvc框架的原理图完成 框架的研发
* @author zjjt
*中央控制器
*寻找子控制器
*/
@WebServlet("*.action")
public class DispatchServlet extends HttpServlet{
//放子控制器的容器
private Map<String, ActionSupport> actions=new HashMap<String, ActionSupport>();
//初始化子控制器容器(集合),经过初始化,actions容器内存就有了子控制器
//init,service,destroy
@Override
public void init() throws ServletException {
actions.put("/book", new BookAction());
/*actions.put("/order", new BookAction());
actions.put("/OrderItem", new BookAction());*/
}
@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 {
//完成寻找子控制的过程
//浏览器:http://localhost:8080/t266_mvc/book.action?methodName=add
//目标:BookAction.add()..
/**
* 思路:
* 1.从游览器URL中获取到“/book”字符串
* 2.在子控制器容器中拿到BookAction
* 3.BookAction.add()
*/
String uri = req.getRequestURI();
uri = uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
//action=BookAction
ActionSupport action = actions.get(uri);
//ActionSupport action=new BookAction()
action.execute(req, resp);
}
}
|