首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

从零开始写Javaweb框架读书笔记(1)

从零开始写Javaweb框架读书笔记(1)

请求转发器

    用于处理用户发送给的请求。

我们需要一个Servlet来处理所有的请求
这是HttpServlet的处理请求的service函数源码

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getMethod();
        long errMsg;
        if(method.equals("GET")) {
            errMsg = this.getLastModified(req);
            if(errMsg == -1L) {
                this.doGet(req, resp);
            } else {
                long ifModifiedSince = req.getDateHeader("If-Modified-Since");
                if(ifModifiedSince < errMsg) {
                    this.maybeSetLastModified(resp, errMsg);
                    this.doGet(req, resp);
                } else {
                    resp.setStatus(304);
                }
            }
        } else if(method.equals("HEAD")) {
            errMsg = this.getLastModified(req);
            this.maybeSetLastModified(resp, errMsg);
            this.doHead(req, resp);
        } else if(method.equals("POST")) {
            this.doPost(req, resp);
        } else if(method.equals("PUT")) {
            this.doPut(req, resp);
        } else if(method.equals("DELETE")) {
            this.doDelete(req, resp);
        } else if(method.equals("OPTIONS")) {
            this.doOptions(req, resp);
        } else if(method.equals("TRACE")) {
            this.doTrace(req, resp);
        } else {
            String errMsg1 = lStrings.getString("http.method_not_implemented");
            Object[] errArgs = new Object[]{method};
            errMsg1 = MessageFormat.format(errMsg1, errArgs);
            resp.sendError(501, errMsg1);
        }

    }


我们可以知道 ,这个是通过方法的类型不同进行转向不同的函数进行出处理。
而我们需要把
get:customer_show
post:customer_create
暂时先不讨论符合restful的url
我们可以设置一个注解@Action用来和spring的RequestMapping方法一样
但是我们这个看上去不像RequestMapping中那样的繁琐
具体可参照smart-framework 点击跳转oschina文档首页

    先预处理request与handler的映射
        加载带有Controller注解的类
        扫描Action注解来获取到get:customer_show这个属性的值
        通过正则表达式\\w:/\\w*匹配
        分割字符串
        添加一个handler用于接受请求并执行方法,返回结果
        添加request(格式请求方法类型:请求路径)-handler映射

/**
* Created by xueaohui on 2016/6/22.
*/
public final class ControllerHelper {
    /**
     * 用来存放请求与处理器的映射关系(Action Map)
     */
    private static final Map<Request,Handler> ACTION_MAP = new HashMap<Request, Handler>();

    static {
        Set<Class<?>> controllerClassSet = ClassHelper.getControllerClassSet();


        if(CollectionUtil.isNotEmpty(controllerClassSet)){
            for(Class<?> controllerClass : controllerClassSet){

                Method[] methods = controllerClass.getMethods();

                if(ArrayUtil.isNotEmpty(methods)) {
                    for (Method method : methods) {
                        //如果这个方法被Action注释了
                        if(method.isAnnotationPresent(Action.class)){
                            Action action = method.getAnnotation(Action.class);
                            //获取Action的属性值
                            String mapping = action.value();
                            //验证URL 如get:customer_list post:customer_update
                            if(mapping.matches("\\w+:/\\w*")){
                                String[] array = mapping.split(":");
                                if(ArrayUtil.isNotEmpty(array)){
                                    String requestMethod = array[0];
                                    String requestPath = array[1];

                                    Request request = new Request(requestMethod,requestPath);
                                    Handler handler = new Handler(controllerClass,method);
                                    //初始化handler
                                    ACTION_MAP.put(request,handler);
                                }

                            }

                        }
                    }
                }
            }
        }
    }
    /**
     * 获取Handler
     */
    public static Handler getHandler(String requestMethod ,  String requestPath){
        Request request = new Request(requestMethod,requestPath);
        return ACTION_MAP.get(request);
    }
}
返回列表