Skip to content

Commit

Permalink
refactor: RedirectView 추상 클래스 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Go-Jaecheol committed Sep 26, 2023
1 parent c539134 commit cfb31e9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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<String, ?> 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<String, ?> 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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, ?> 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<String, ?> model, final HttpServletRequest request,
final HttpServletResponse response) throws ServletException, IOException;
}

0 comments on commit cfb31e9

Please sign in to comment.