Skip to content

Commit

Permalink
feat: 404 페이지 반환 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
kevstevie committed Sep 25, 2023
1 parent 93a3693 commit b6809b2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class DispatcherServletInitializer implements WebApplicationInitializer {

@Override
public void onStartup(final ServletContext servletContext) {
final var dispatcherServlet = new DispatcherServlet();
final var dispatcherServlet = new DispatcherServlet("com.techcourse.controller", "/404.jsp");

final var registration = servletContext.addServlet(DEFAULT_SERVLET_NAME, dispatcherServlet);
if (registration == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import webmvc.org.springframework.web.servlet.mvc.tobe.AnnotationHandlerMapping;
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerAdapterRegistry;
import webmvc.org.springframework.web.servlet.mvc.tobe.HandlerMappingRegistry;
import webmvc.org.springframework.web.servlet.mvc.tobe.NotFoundHandlerAdapter;

public class DispatcherServlet extends HttpServlet {

Expand All @@ -19,9 +20,9 @@ public class DispatcherServlet extends HttpServlet {
private final HandlerMappingRegistry handlerMappingRegistry;
private final HandlerAdapterRegistry handlerAdapterRegistry;

public DispatcherServlet() {
handlerAdapterRegistry = new HandlerAdapterRegistry(new AnnotationHandlerAdapter());
handlerMappingRegistry = new HandlerMappingRegistry(new AnnotationHandlerMapping("com.techcourse.controller"));
public DispatcherServlet(String basePackage, String notFoundViewName) {
handlerAdapterRegistry = new HandlerAdapterRegistry(new AnnotationHandlerAdapter(), new NotFoundHandlerAdapter(notFoundViewName));
handlerMappingRegistry = new HandlerMappingRegistry(new AnnotationHandlerMapping(basePackage));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public Object getHandler(HttpServletRequest request) {
.map(handler -> handler.getHandler(request))
.filter(Objects::nonNull)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 URL입니다."));
.orElse(new NotFoundHandler());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package webmvc.org.springframework.web.servlet.mvc.tobe;

import webmvc.org.springframework.web.servlet.ModelAndView;
import webmvc.org.springframework.web.servlet.view.JspView;

public class NotFoundHandler {

public ModelAndView handle(String viewName) {
return new ModelAndView(new JspView(viewName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package webmvc.org.springframework.web.servlet.mvc.tobe;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import webmvc.org.springframework.web.servlet.ModelAndView;

public class NotFoundHandlerAdapter implements HandlerAdapter {

private final String viewName;

public NotFoundHandlerAdapter(String viewName) {
this.viewName = viewName;
}

@Override
public boolean supports(Object handler) {
return handler instanceof NotFoundHandler;
}

@Override
public ModelAndView handle(Object handler, HttpServletRequest request, HttpServletResponse response) throws Exception {
return ((NotFoundHandler) handler).handle(viewName);
}
}

0 comments on commit b6809b2

Please sign in to comment.