#22 mvc: HandlerKey
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package org.example.mvc;
|
||||
|
||||
import org.example.mvc.controller.Controller;
|
||||
import org.example.mvc.controller.HandlerKey;
|
||||
import org.example.mvc.controller.RequestMethod;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -30,7 +32,11 @@ public class DispatcherServlet extends HttpServlet {
|
||||
log.info("[DispatcherServlet] service started.");
|
||||
|
||||
try {
|
||||
Controller handler = requestMappingHandlerMapping.findHandler(req.getRequestURI());
|
||||
Controller handler =
|
||||
requestMappingHandlerMapping.findHandler(new HandlerKey(RequestMethod.valueOf(req.getMethod()), req.getRequestURI()));
|
||||
|
||||
if (handler == null) return;
|
||||
|
||||
String viewName = handler.handleRequest(req, resp);
|
||||
|
||||
RequestDispatcher requestDispatcher = req.getRequestDispatcher(viewName);
|
||||
|
||||
@@ -1,22 +1,21 @@
|
||||
package org.example.mvc;
|
||||
|
||||
import org.example.mvc.controller.Controller;
|
||||
import org.example.mvc.controller.HomeController;
|
||||
import org.example.mvc.controller.UserListController;
|
||||
import org.example.mvc.controller.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class RequestMappingHandlerMapping {
|
||||
|
||||
private Map<String, Controller> mappings = new HashMap<>();
|
||||
private Map<HandlerKey, Controller> mappings = new HashMap<>();
|
||||
|
||||
void init() {
|
||||
mappings.put("/", new HomeController());
|
||||
mappings.put("/users", new UserListController());
|
||||
mappings.put(new HandlerKey(RequestMethod.GET, "/"), new HomeController());
|
||||
mappings.put(new HandlerKey(RequestMethod.GET, "/users"), new UserListController());
|
||||
mappings.put(new HandlerKey(RequestMethod.POST, "/users"), new UserListController());
|
||||
}
|
||||
|
||||
public Controller findHandler(String uriPath) {
|
||||
return mappings.get(uriPath);
|
||||
public Controller findHandler(HandlerKey handlerKey) {
|
||||
return mappings.get(handlerKey);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.example.mvc.controller;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class HandlerKey {
|
||||
|
||||
private final RequestMethod requestMethod;
|
||||
private final String uriPath;
|
||||
|
||||
public HandlerKey(RequestMethod requestMethod, String uriPath) {
|
||||
this.requestMethod = requestMethod;
|
||||
this.uriPath = uriPath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (!(o instanceof HandlerKey)) return false;
|
||||
HandlerKey that = (HandlerKey) o;
|
||||
return requestMethod == that.requestMethod && Objects.equals(uriPath, that.uriPath);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(requestMethod, uriPath);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.example.mvc.controller;
|
||||
|
||||
public enum RequestMethod {
|
||||
GET, POST, PUT, DELETE
|
||||
}
|
||||
Reference in New Issue
Block a user