From 2c705f5cd9273d150cbe933b9e528c50379d4443 Mon Sep 17 00:00:00 2001 From: yunyun22 <470423980@qq.com> Date: Sun, 21 Aug 2016 16:58:55 +0800 Subject: [PATCH] =?UTF-8?q?requestBody=E6=B7=BB=E5=8A=A0equal()=E5=92=8Cha?= =?UTF-8?q?sh()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fastframework/mvc/DispatcherServlet.java | 148 +++++++++--------- .../org/fastframework/mvc/HandlerMapping.java | 98 +++++++----- .../fastframework/mvc/bean/RequestBody.java | 99 ++++++++---- 3 files changed, 196 insertions(+), 149 deletions(-) diff --git a/fast-core/src/main/java/org/fastframework/mvc/DispatcherServlet.java b/fast-core/src/main/java/org/fastframework/mvc/DispatcherServlet.java index c7d2cb4..a72b328 100644 --- a/fast-core/src/main/java/org/fastframework/mvc/DispatcherServlet.java +++ b/fast-core/src/main/java/org/fastframework/mvc/DispatcherServlet.java @@ -1,72 +1,76 @@ -package org.fastframework.mvc; - -import org.fastframework.mvc.bean.HandlerBody; -import org.fastframework.mvc.util.MVCHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -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 java.io.IOException; - -/** - * MVC 前端控制器 - * 1. 初始化相关配置: 类扫描/路由匹配 - * 2. 转发请求到 HandlerMapping - * 3. 反射调用Controller方法,并解耦 - * 4. 根据返回值,响应视图或数据 - * - * Created by bysocket on 16/7/19. - */ -@WebServlet(urlPatterns = "/*", loadOnStartup = 0) -public class DispatcherServlet extends HttpServlet { - - private static final Logger LOGGER = LoggerFactory.getLogger(DispatcherServlet.class); - - /** - * 初始化相关配置 - * 扫描类 - 路由Map - * - * @throws ServletException - */ - @Override - public void init() throws ServletException { - ControllerCollection.init(); - } - - @Override - protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - // 设置请求默认编码 - request.setCharacterEncoding(MVCHelper.REQ_CHARACTER_UTF_8); - // 请求相关信息 - // 请求方法 [POST] [GET] - String requestMethod = request.getMethod(); - // 请求路由 - String requestPath = MVCHelper.getRequestPath(request); - - LOGGER.debug("[fast framework] {} : {}",requestMethod,requestPath); - - // "/" 请求重定向到默认首页 - if (MVCHelper.URL_PATH_SEPARATOR.equals(requestPath)) { - MVCHelper.redirectRequest(MVCHelper.REQ_DEFAULT_HOME_PAGE,request,response); - return; - } - - // 处理器映射 - // 获取 handler - HandlerBody handler = HandlerMapping.getHandler(requestMethod,requestPath); - - // null == handler - if (null == handler) { - response.sendError(HttpServletResponse.SC_NOT_FOUND); - return; - } - - // 调用 Handler - HandlerInvoker.invokeHandler(request,response,handler); - return; - } -} +package org.fastframework.mvc; + +import org.fastframework.mvc.bean.HandlerBody; +import org.fastframework.mvc.util.MVCHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +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 java.io.IOException; + +/** + * MVC 前端控制器 1. 初始化相关配置: 类扫描/路由匹配 2. 转发请求到 HandlerMapping 3. + * 反射调用Controller方法,并解耦 4. 根据返回值,响应视图或数据 + * + * Created by bysocket on 16/7/19. + */ +@WebServlet(urlPatterns = "/*", loadOnStartup = 0) +public class DispatcherServlet extends HttpServlet { + + /** + * + */ + private static final long serialVersionUID = 9014760395662343213L; + private static final Logger LOGGER = LoggerFactory + .getLogger(DispatcherServlet.class); + + /** + * 初始化相关配置 扫描类 - 路由Map + * + * @throws ServletException + */ + @Override + public void init() throws ServletException { + ControllerCollection.init(); + } + + @Override + protected void service(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { + // 设置请求默认编码 + request.setCharacterEncoding(MVCHelper.REQ_CHARACTER_UTF_8); + // 请求相关信息 + // 请求方法 [POST] [GET] + String requestMethod = request.getMethod(); + // 请求路由 + String requestPath = MVCHelper.getRequestPath(request); + + LOGGER.debug("[fast framework] {} : {}", requestMethod, requestPath); + + // "/" 请求重定向到默认首页 + if (MVCHelper.URL_PATH_SEPARATOR.equals(requestPath)) { + MVCHelper.redirectRequest(MVCHelper.REQ_DEFAULT_HOME_PAGE, request, + response); + return; + } + + // 处理器映射 + // 获取 handler + HandlerBody handler = HandlerMapping.getHandler(requestMethod, + requestPath); + + // null == handler + if (null == handler) { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + return; + } + + // 调用 Handler + HandlerInvoker.invokeHandler(request, response, handler); + return; + } +} diff --git a/fast-core/src/main/java/org/fastframework/mvc/HandlerMapping.java b/fast-core/src/main/java/org/fastframework/mvc/HandlerMapping.java index c84a4df..c254d75 100644 --- a/fast-core/src/main/java/org/fastframework/mvc/HandlerMapping.java +++ b/fast-core/src/main/java/org/fastframework/mvc/HandlerMapping.java @@ -1,43 +1,55 @@ -package org.fastframework.mvc; - -import org.fastframework.mvc.annotation.RequestMethod; -import org.fastframework.mvc.bean.HandlerBody; -import org.fastframework.mvc.bean.RequestBody; - -import java.util.Map; - -/** - * 处理器映射 - * - * Created by bysocket on 16/8/9. - */ -public class HandlerMapping { - - /** - * 处理方法体 - * - * @param requestMethod - * @param requestPath - * @return - */ - public static HandlerBody getHandler(String requestMethod, String requestPath) { - HandlerBody handler = null; - - // Controller Map 请求 -> 方法体 的映射 - Map methodMap = ControllerCollection.getMethodMap(); - for (Map.Entry methodEntry : methodMap.entrySet()) { - RequestBody req = methodEntry.getKey(); - String reqPath = req.getRequestPath(); - RequestMethod reqMethod = req.getRequestMethod(); - - if (reqPath.equals(requestPath) && reqMethod.name().equalsIgnoreCase(requestMethod)) { - handler = methodEntry.getValue(); - if (handler != null) { - return handler; - } - } - } - - return handler; - } -} +package org.fastframework.mvc; + +import org.fastframework.mvc.annotation.RequestMethod; +import org.fastframework.mvc.bean.HandlerBody; +import org.fastframework.mvc.bean.RequestBody; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; + +/** + * 处理器映射 + * + * Created by bysocket on 16/8/9. + */ +public class HandlerMapping { + private static final Logger LOGGER = LoggerFactory + .getLogger(HandlerMapping.class); + + /** + * 处理方法体 + * + * @param requestMethod + * @param requestPath + * @return + */ + public static HandlerBody getHandler(String requestMethod, + String requestPath) { + HandlerBody handler = null; + + RequestBody reqTest = new RequestBody( + RequestMethod.valueOf(requestMethod), requestPath); + // Controller Map 请求 -> 方法体 的映射 + Map methodMap = ControllerCollection + .getMethodMap(); + handler = methodMap.get(reqTest); + LOGGER.debug("通过url映射到method"); + // handlerBody.getControllerMethod(); + // for (Map.Entry methodEntry : methodMap + // .entrySet()) { + // RequestBody req = methodEntry.getKey(); + // String reqPath = req.getRequestPath(); + // RequestMethod reqMethod = req.getRequestMethod(); + // + // if (reqPath.equals(requestPath) + // && reqMethod.name().equalsIgnoreCase(requestMethod)) { + // handler = methodEntry.getValue().; + // if (handler != null) { + // return handler; + // } + // } + // } + return handler; + } +} diff --git a/fast-core/src/main/java/org/fastframework/mvc/bean/RequestBody.java b/fast-core/src/main/java/org/fastframework/mvc/bean/RequestBody.java index f19c7bf..218ca7f 100644 --- a/fast-core/src/main/java/org/fastframework/mvc/bean/RequestBody.java +++ b/fast-core/src/main/java/org/fastframework/mvc/bean/RequestBody.java @@ -1,34 +1,65 @@ -package org.fastframework.mvc.bean; - -import org.fastframework.mvc.annotation.RequestMethod; - -/** - * 请求体 - *

- * Created by bysocket on 16/7/19. - */ -public class RequestBody { - - /** - * 请求方法 [POST] [GET] {@link org.fastframework.mvc.annotation.RequestMethod} - */ - private RequestMethod requestMethod; - - /** - * 请求路由 - */ - private String requestPath; - - public RequestBody(RequestMethod requestMethod, String requestPath) { - this.requestMethod = requestMethod; - this.requestPath = requestPath; - } - - public RequestMethod getRequestMethod() { - return requestMethod; - } - - public String getRequestPath() { - return requestPath; - } -} +package org.fastframework.mvc.bean; + +import org.fastframework.mvc.annotation.RequestMethod; + +/** + * 请求体 + *

+ * Created by bysocket on 16/7/19. + */ +public class RequestBody { + + /** + * 请求方法 [POST] [GET] {@link org.fastframework.mvc.annotation.RequestMethod} + */ + private RequestMethod requestMethod; + + /** + * 请求路由 + */ + private String requestPath; + + public RequestBody(RequestMethod requestMethod, String requestPath) { + this.requestMethod = requestMethod; + this.requestPath = requestPath; + } + + public RequestMethod getRequestMethod() { + return requestMethod; + } + + public String getRequestPath() { + return requestPath; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + ((requestMethod == null) ? 0 : requestMethod.hashCode()); + result = prime * result + + ((requestPath == null) ? 0 : requestPath.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + RequestBody other = (RequestBody) obj; + if (requestMethod != other.requestMethod) + return false; + if (requestPath == null) { + if (other.requestPath != null) + return false; + } else if (!requestPath.equals(other.requestPath)) + return false; + return true; + } + +}