Skip to content

Commit

Permalink
refactor: modelAndView 직접 render하도록 변경, 일부 메서드명 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
yoondgu committed Sep 25, 2023
1 parent 04c763e commit cc81b8d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package webmvc.org.springframework.web.servlet;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -14,6 +17,10 @@ public ModelAndView(final View view) {
this.model = new HashMap<>();
}

public void render(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
view.render(model, request, response);
}

public ModelAndView addObject(final String attributeName, final Object attributeValue) {
model.put(attributeName, attributeValue);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ protected void service(final HttpServletRequest request, final HttpServletRespon
throw new NoSuchElementException("Cannot find handler for request");
}

handlerExecutor.render(request, response, handler.get());
handlerExecutor.execute(request, response, handler.get());
} catch (Exception exception) {
log.error("Exception : {} {}", exception.getMessage(), exception.getStackTrace());

handlerExecutor.render(request, response, exception);
handlerExecutor.execute(request, response, exception);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ public HandlerExecutor(final HandlerAdapterRegistry handlerAdapterRegistry) {
this.handlerAdapterRegistry = handlerAdapterRegistry;
}

public void render(final HttpServletRequest request, HttpServletResponse response, Object handler) {
public void execute(final HttpServletRequest request, HttpServletResponse response, Object handler) {
try {
final var handlerAdapter = handlerAdapterRegistry.getHandlerAdapter(handler);
if (handlerAdapter.isPresent()) {
final var adapter = handlerAdapter.get();
final var modelAndView = adapter.handle(request, response, handler);
final var model = modelAndView.getModel();
final var view = modelAndView.getView();

view.render(model, request, response);
modelAndView.render(request, response);
return;
}

Expand All @@ -43,7 +41,7 @@ public void render(final HttpServletRequest request, HttpServletResponse respons

private void executeExceptionHandler(HttpServletRequest request, HttpServletResponse response, Throwable cause) {
handlerAdapterRegistry.getHandlerAdapter(cause)
.ifPresent(exceptionHandlerAdapter -> render(request, response, cause));
.ifPresent(exceptionHandlerAdapter -> execute(request, response, cause));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void execute_handlerExecution_JspView() throws Exception {
final var view = new JspView("redirect:/test.jsp");
when(handler.handle(request, response)).thenReturn(new ModelAndView(view));

handlerExecutor.render(request, response, handler);
handlerExecutor.execute(request, response, handler);

verify(handler).handle(request, response);
}
Expand All @@ -54,7 +54,7 @@ void execute_handlerExecution_JspView() throws Exception {
void execute_controller() throws Exception {
final var handler = new ForwardController("redirect:/test.jsp");

handlerExecutor.render(request, response, handler);
handlerExecutor.execute(request, response, handler);

verify(response).sendRedirect("/test.jsp");
}
Expand Down

0 comments on commit cc81b8d

Please sign in to comment.