From cfb31e91abad4242237d699fbaf81267873ade3e Mon Sep 17 00:00:00 2001 From: Go-Jaecheol Date: Tue, 26 Sep 2023 21:42:31 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20RedirectView=20=EC=B6=94=EC=83=81?= =?UTF-8?q?=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/servlet/view/JspView.java | 19 ++++------- .../web/servlet/view/RedirectView.java | 32 +++++++++++++++++++ 2 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 mvc/src/main/java/webmvc/org/springframework/web/servlet/view/RedirectView.java diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java index 03d535e5ce..7c3e3f4763 100644 --- a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/JspView.java @@ -1,32 +1,25 @@ package webmvc.org.springframework.web.servlet.view; +import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import webmvc.org.springframework.web.servlet.View; +import java.io.IOException; import java.util.Map; -public class JspView implements View { +public class JspView extends RedirectView { private static final Logger log = LoggerFactory.getLogger(JspView.class); - public static final String REDIRECT_PREFIX = "redirect:"; - - private final String viewName; - public JspView(final String viewName) { - this.viewName = viewName; + super(viewName); } @Override - public void render(final Map model, final HttpServletRequest request, final HttpServletResponse response) throws Exception { - if (viewName.startsWith(JspView.REDIRECT_PREFIX)) { - response.sendRedirect(viewName.substring(JspView.REDIRECT_PREFIX.length())); - return; - } - + protected void renderWithoutRedirect(final Map model, final HttpServletRequest request, + final HttpServletResponse response) throws ServletException, IOException { model.keySet().forEach(key -> { log.debug("attribute name : {}, value : {}", key, model.get(key)); request.setAttribute(key, model.get(key)); diff --git a/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/RedirectView.java b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/RedirectView.java new file mode 100644 index 0000000000..b2297e08c4 --- /dev/null +++ b/mvc/src/main/java/webmvc/org/springframework/web/servlet/view/RedirectView.java @@ -0,0 +1,32 @@ +package webmvc.org.springframework.web.servlet.view; + +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import webmvc.org.springframework.web.servlet.View; + +import java.io.IOException; +import java.util.Map; + +public abstract class RedirectView implements View { + + public static final String REDIRECT_PREFIX = "redirect:"; + + protected final String viewName; + + protected RedirectView(final String viewName) { + this.viewName = viewName; + } + + @Override + public void render(final Map model, final HttpServletRequest request, final HttpServletResponse response) throws Exception { + if (viewName.startsWith(REDIRECT_PREFIX)) { + response.sendRedirect(viewName.substring(REDIRECT_PREFIX.length())); + return; + } + renderWithoutRedirect(model, request, response); + } + + protected abstract void renderWithoutRedirect(final Map model, final HttpServletRequest request, + final HttpServletResponse response) throws ServletException, IOException; +}