diff --git a/fast-core/src/main/java/org/fastframework/core/util/TimeUtil.java b/fast-core/src/main/java/org/fastframework/core/util/TimeUtil.java index 176bc96..fdceae2 100644 --- a/fast-core/src/main/java/org/fastframework/core/util/TimeUtil.java +++ b/fast-core/src/main/java/org/fastframework/core/util/TimeUtil.java @@ -1,126 +1,126 @@ -package org.fastframework.core.util; - -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; - -/** - * 基于 JDK 8 time包的时间工具类 - *

- * Created by bysocket on 16/8/23. - */ -public class TimeUtil { - - /** - * 默认时间格式 - */ - private static final String SHORT_DATE_PATTERN_DEFAULT = "yyyy-MM-dd"; - private static final String LONG_DATE_PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss"; - private static final String LONG_DATE_PATTERN_WITH_MILSEC_DEFAULT = "yyyy-MM-dd HH:mm:ss.SSS"; - - /** - * 短时间格式 : 2016-08-25 - */ - private static final String[] SHORT_DATE_PATTERN = {"yyyy-MM-dd", - "yyyy/MM/dd", - "yyyy\\MM\\dd", - "yyyyMMdd"}; - /** - * 长时间格式 : 2016-08-25 20:28:46 - */ - private static final String[] LONG_DATE_PATTERN = {"yyyy-MM-dd HH:mm:ss", - "yyyy/MM/dd HH:mm:ss", - "yyyy\\MM\\dd HH:mm:ss", - "yyyyMMddHHmmss"}; - /** - * 长时间格式 ( 带毫秒 ) : 2016-08-25 20:34:48.956 - */ - private static final String[] LONG_DATE_PATTERN_WITH_MILSEC = {"yyyy-MM-dd HH:mm:ss.SSS", - "yyyy/MM/dd HH:mm:ss.SSS", - "yyyy\\MM\\dd HH:mm:ss.SSS", - "yyyyMMddHHmmssSSS"}; - - /** - * String 转时间 - * - * @param timeStr - * @return - */ - public final static LocalDateTime parseTime(String timeStr) { - LocalDateTime dateTime = LocalDateTime.parse(timeStr, getDefaultDateTimeFormatter()); - return dateTime; - } - - /** - * String 转时间 - * - * @param timeStr - * @param format 时间格式 - * @return - */ - public final static LocalDateTime parseTime(String timeStr, String format) { - LocalDateTime dateTime = LocalDateTime.parse(timeStr, getDateTimeFormatter(format)); - return dateTime; - } - - /** - * 时间转 String - * - * @param time - * @return - */ - public final static String parseTime(LocalDateTime time) { - return getDefaultDateTimeFormatter().format(time); - } - - /** - * 时间转 String - * - * @param time - * @param format 时间格式 - * @return - */ - public final static String parseTime(LocalDateTime time, String format) { - return getDateTimeFormatter(format).format(time); - } - - /** - * 获取当前时间 - * - * @return - */ - public final static String getCurrentDatetime() { - LocalDateTime now = LocalDateTime.now(); - return getDefaultDateTimeFormatter().format(now); - } - - /** - * 获取当前时间 - * - * @param format 时间格式 - * @return - */ - public final static String getCurrentDatetime(String format) { - LocalDateTime now = LocalDateTime.now(); - return getDateTimeFormatter(format).format(now); - } - - /** - * 获取默认时间格式 - * - * @return - */ - private final static DateTimeFormatter getDefaultDateTimeFormatter() { - return DateTimeFormatter.ofPattern(LONG_DATE_PATTERN_DEFAULT); - } - - /** - * 获取时间格式 - * - * @param format 时间格式 - * @return - */ - private final static DateTimeFormatter getDateTimeFormatter(String format) { - return DateTimeFormatter.ofPattern(format); - } - -} +package org.fastframework.core.util; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** + * 基于 JDK 8 time包的时间工具类 + *

+ * Created by bysocket on 16/8/23. + */ +public class TimeUtil { + + /** + * 默认时间格式 + */ + public static final String SHORT_DATE_PATTERN_DEFAULT = "yyyy-MM-dd"; + public static final String LONG_DATE_PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss"; + public static final String LONG_DATE_PATTERN_WITH_MILSEC_DEFAULT = "yyyy-MM-dd HH:mm:ss.SSS"; + + /** + * 短时间格式 : 2016-08-25 + */ + public static final String[] SHORT_DATE_PATTERN = {"yyyy-MM-dd", + "yyyy/MM/dd", + "yyyy\\MM\\dd", + "yyyyMMdd"}; + /** + * 长时间格式 : 2016-08-25 20:28:46 + */ + public static final String[] LONG_DATE_PATTERN = {"yyyy-MM-dd HH:mm:ss", + "yyyy/MM/dd HH:mm:ss", + "yyyy\\MM\\dd HH:mm:ss", + "yyyyMMddHHmmss"}; + /** + * 长时间格式 ( 带毫秒 ) : 2016-08-25 20:34:48.956 + */ + public static final String[] LONG_DATE_PATTERN_WITH_MILSEC = {"yyyy-MM-dd HH:mm:ss.SSS", + "yyyy/MM/dd HH:mm:ss.SSS", + "yyyy\\MM\\dd HH:mm:ss.SSS", + "yyyyMMddHHmmssSSS"}; + + /** + * String 转时间 + * + * @param timeStr + * @return + */ + public final static LocalDateTime parseTime(String timeStr) { + LocalDateTime dateTime = LocalDateTime.parse(timeStr, getDefaultDateTimeFormatter()); + return dateTime; + } + + /** + * String 转时间 + * + * @param timeStr + * @param format 时间格式 + * @return + */ + public final static LocalDateTime parseTime(String timeStr, String format) { + LocalDateTime dateTime = LocalDateTime.parse(timeStr, getDateTimeFormatter(format)); + return dateTime; + } + + /** + * 时间转 String + * + * @param time + * @return + */ + public final static String parseTime(LocalDateTime time) { + return getDefaultDateTimeFormatter().format(time); + } + + /** + * 时间转 String + * + * @param time + * @param format 时间格式 + * @return + */ + public final static String parseTime(LocalDateTime time, String format) { + return getDateTimeFormatter(format).format(time); + } + + /** + * 获取当前时间 + * + * @return + */ + public final static String getCurrentDatetime() { + LocalDateTime now = LocalDateTime.now(); + return getDefaultDateTimeFormatter().format(now); + } + + /** + * 获取当前时间 + * + * @param format 时间格式 + * @return + */ + public final static String getCurrentDatetime(String format) { + LocalDateTime now = LocalDateTime.now(); + return getDateTimeFormatter(format).format(now); + } + + /** + * 获取默认时间格式 + * + * @return + */ + private final static DateTimeFormatter getDefaultDateTimeFormatter() { + return DateTimeFormatter.ofPattern(LONG_DATE_PATTERN_DEFAULT); + } + + /** + * 获取时间格式 + * + * @param format 时间格式 + * @return + */ + private final static DateTimeFormatter getDateTimeFormatter(String format) { + return DateTimeFormatter.ofPattern(format); + } + +} 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; + } + +}