spring mvc architectureStep1 HTTP Request 가 들어오면, 앞단에서 DispatcherSevlet이 해당 요청에 맞는 Controller 정보를 HandlerMapping 정보를 통해 가져온다. Step2 HandlerMapping에서 찾은 Controller를 호출한다. 이때, Handler의 추가 파라미터와 모델 정보와 함께 호출할 수 있다. Step3 요청을 받은 Controller는 요청에 응답으로 생성할 View에 담을 Model 정보를 생성한다. 이때 필요하다면, Service 단에서 비즈니스 로직을 처리할 수 있고, DAO를 통해 DB에 엑세스할 수도 있다. 이렇게 생성한 Model과, 리턴할 View 정보를 ViewResolver에 전달한다. Step4View Resolver에서는 컨트롤러가 전달한 View정보와 Model data로 화면을 구성하여 응답한다.
SpringFramework가 제공하는 웹 MVC 를 사용하려면, 의존성 추가 뿐만 아니라,
web.xml 를 통해, SUN에서 규정해 놓은 방식에 맞게 웹 애플리케이션 배포 시 Servlet 정보, tomcat 등 정보, 등 Deployment Descriptor를 구성해야한다.
또한, dispatcher-servlet.xml 파일을 통해 Controller, ViewResolver, Spring MVC view의 prefix/suffix, static contents 등에 대한 정보도 세팅해주어야 한다.
Spring Boot Web MVC
하지만, 스프링 부트를 이용하면, 스프링 부트 자동 설정 기능으로 간편하게 애플리케이션을 구성할 수 있다.
org.springframework.boot.autoconfigure 의 spring.factories 파일
org.springframework.boot.autoconfigure 의 spring.factories 파일에 WebMvcAutoConfiguration 설정을 Import 해오는 것을 확인할 수 있다.
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,
ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
@Bean
@ConditionalOnMissingBean(HiddenHttpMethodFilter.class)
@ConditionalOnProperty(prefix = "spring.mvc.hiddenmethod.filter", name = "enabled", matchIfMissing = false)
public OrderedHiddenHttpMethodFilter hiddenHttpMethodFilter() {
return new OrderedHiddenHttpMethodFilter();
}
@Bean
@ConditionalOnMissingBean(FormContentFilter.class)
@ConditionalOnProperty(prefix = "spring.mvc.formcontent.filter", name = "enabled", matchIfMissing = true)
public OrderedFormContentFilter formContentFilter() {
return new OrderedFormContentFilter();
}
static String[] getResourceLocations(String[] staticLocations) {
String[] locations = new String[staticLocations.length + SERVLET_LOCATIONS.length];
System.arraycopy(staticLocations, 0, locations, 0, staticLocations.length);
System.arraycopy(SERVLET_LOCATIONS, 0, locations, staticLocations.length, SERVLET_LOCATIONS.length);
return locations;
}
// Defined as a nested config to ensure WebMvcConfigurer is not read when not
// on the classpath
@SuppressWarnings("deprecation")
@Configuration(proxyBeanMethods = false)
@Import(EnableWebMvcConfiguration.class)
@EnableConfigurationProperties({ WebMvcProperties.class,
org.springframework.boot.autoconfigure.web.ResourceProperties.class, WebProperties.class })
@Order(0)
public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {
// ....
}
HiddenHttpMethodFilter
RESTful HTTP 메소드 ( GET, POST, PUT, PATCH, DELETE ) 중 브라우저에 따라서 PUT, PATCH, DELETE 방식을 지원하지 않는 경우 (HiddenHttpMethod) 가 있는데, 이때 HiddenHttpMethodFilter는 요청을 request parameter의 "_method"를 키로 메소드 정보를 가져와 HttpServletRequestWrapper.getMethod() 반환 값을 변경해 요청된 HTTP 메소드의 타입을 HiddenHttpMethod (PUT, DELETE, PATCH) 로 변경해준다.
SpringFramework 에서 사용하려면, web.xml에 추가 세팅해주면 된다.
FormContentFilter
기본 Servlet 스펙에는 HTTP POST 에서만 Form Data를 전달할 수 있도록 정의되어있다. FormContentFilter는 PUT과 PATCH, DELETE 요청에서도 Form Data를 사용할 수 있도록 Wrapping 해준다.
SpringFrameWork 5.1 부터 추가되었다. 기존 HttpPutFormContentFilter 클래스가 Deprecated 되었고 이를 대체할FormContentFilter클래스가 추가 되었다. 또한 이와 관련된spring.mvc.formcontent.putfilter.enabled프로퍼티는spring.mvc.formcontent.filter.enabled프로퍼티로 변경 해야 한다.
WebMvcConfigurer
WebMvcConfigurer는 스프링 부트가 제공하는 MVC 기능을 모두 사용하면서 컨버터 추가, 인터셉터 추가와 같은 추가적인 설정할 수 있는 기능을 제공한다. 이를 통해 스프링 웹 MVC가 제공해주는 기능을 커스터마이징 할 수 있다.
WebMvcProperties.class, org.springframework.boot.autoconfigure.web.ResourceProperties.class, WebProperties.class 세 가지
프로퍼티 를 가지고 있는데,
각 spring.mvc.* , spring.resources.*, spring.web.*인 프로퍼티에 대해 바인딩한다.