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; +}