提交 c66349a1 编写于 作者: 黄勇

v2.3.3-SNAPSHOT

- 改进:代码优化
上级 cbcac5ad
......@@ -23,15 +23,15 @@ public class ActionHelper {
/**
* Action Map(HTTP 请求与 Action 方法的映射)
*/
private static final Map<Requestor, Handler> actionMap = new LinkedHashMap<Requestor, Handler>();
private static final Map<Requester, Handler> actionMap = new LinkedHashMap<Requester, Handler>();
static {
// 获取所有 Action 类
List<Class<?>> actionClassList = ClassHelper.getClassListByAnnotation(Action.class);
if (CollectionUtil.isNotEmpty(actionClassList)) {
// 定义两个 Action Map
Map<Requestor, Handler> commonActionMap = new HashMap<Requestor, Handler>(); // 存放普通 Action Map
Map<Requestor, Handler> regexpActionMap = new HashMap<Requestor, Handler>(); // 存放带有正则表达式的 Action Map
Map<Requester, Handler> commonActionMap = new HashMap<Requester, Handler>(); // 存放普通 Action Map
Map<Requester, Handler> regexpActionMap = new HashMap<Requester, Handler>(); // 存放带有正则表达式的 Action Map
// 遍历 Action 类
for (Class<?> actionClass : actionClassList) {
// 获取并遍历该 Action 类中所有的方法
......@@ -49,7 +49,7 @@ public class ActionHelper {
}
}
private static void handleActionMethod(Class<?> actionClass, Method actionMethod, Map<Requestor, Handler> commonActionMap, Map<Requestor, Handler> regexpActionMap) {
private static void handleActionMethod(Class<?> actionClass, Method actionMethod, Map<Requester, Handler> commonActionMap, Map<Requester, Handler> regexpActionMap) {
// 判断当前 Action 方法是否带有 Request 注解
if (actionMethod.isAnnotationPresent(Request.Get.class)) {
String requestPath = actionMethod.getAnnotation(Request.Get.class).value();
......@@ -66,23 +66,23 @@ public class ActionHelper {
}
}
private static void putActionMap(String requestMethod, String requestPath, Class<?> actionClass, Method actionMethod, Map<Requestor, Handler> commonActionMap, Map<Requestor, Handler> regexpActionMap) {
private static void putActionMap(String requestMethod, String requestPath, Class<?> actionClass, Method actionMethod, Map<Requester, Handler> commonActionMap, Map<Requester, Handler> regexpActionMap) {
// 判断 Request Path 中是否带有占位符
if (requestPath.matches(".+\\{\\w+\\}.*")) {
// 将请求路径中的占位符 {\w+} 转换为正则表达式 (\\w+)
requestPath = StringUtil.replaceAll(requestPath, "\\{\\w+\\}", "(\\\\w+)");
// 将 Requestor 与 Handler 放入 Regexp Action Map 中
regexpActionMap.put(new Requestor(requestMethod, requestPath), new Handler(actionClass, actionMethod));
// 将 Requester 与 Handler 放入 Regexp Action Map 中
regexpActionMap.put(new Requester(requestMethod, requestPath), new Handler(actionClass, actionMethod));
} else {
// 将 Requestor 与 Handler 放入 Common Action Map 中
commonActionMap.put(new Requestor(requestMethod, requestPath), new Handler(actionClass, actionMethod));
// 将 Requester 与 Handler 放入 Common Action Map 中
commonActionMap.put(new Requester(requestMethod, requestPath), new Handler(actionClass, actionMethod));
}
}
/**
* 获取 Action Map
*/
public static Map<Requestor, Handler> getActionMap() {
public static Map<Requester, Handler> getActionMap() {
return actionMap;
}
}
......@@ -6,12 +6,12 @@ package org.smart4j.framework.mvc;
* @author huangyong
* @since 1.0
*/
public class Requestor {
public class Requester {
private String requestMethod;
private String requestPath;
public Requestor(String requestMethod, String requestPath) {
public Requester(String requestMethod, String requestPath) {
this.requestMethod = requestMethod;
this.requestPath = requestPath;
}
......
......@@ -6,7 +6,7 @@ import java.util.regex.Pattern;
import org.smart4j.framework.mvc.ActionHelper;
import org.smart4j.framework.mvc.Handler;
import org.smart4j.framework.mvc.HandlerMapping;
import org.smart4j.framework.mvc.Requestor;
import org.smart4j.framework.mvc.Requester;
/**
* 默认处理器映射
......@@ -21,12 +21,12 @@ public class DefaultHandlerMapping implements HandlerMapping {
// 定义一个 Handler
Handler handler = null;
// 获取并遍历 Action 映射
Map<Requestor, Handler> actionMap = ActionHelper.getActionMap();
for (Map.Entry<Requestor, Handler> actionEntry : actionMap.entrySet()) {
// 从 Requestor 中获取 Request 相关属性
Requestor requestor = actionEntry.getKey();
String requestMethod = requestor.getRequestMethod();
String requestPath = requestor.getRequestPath(); // 正则表达式
Map<Requester, Handler> actionMap = ActionHelper.getActionMap();
for (Map.Entry<Requester, Handler> actionEntry : actionMap.entrySet()) {
// 从 Requester 中获取 Request 相关属性
Requester requester = actionEntry.getKey();
String requestMethod = requester.getRequestMethod();
String requestPath = requester.getRequestPath(); // 正则表达式
// 获取请求路径匹配器(使用正则表达式匹配请求路径并从中获取相应的请求参数)
Matcher requestPathMatcher = Pattern.compile(requestPath).matcher(currentRequestPath);
// 判断请求方法与请求路径是否同时匹配
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册