@EnableWebMvc 애노테이션이란?
@Configuration 애노테이션과 함께 쓰여 애노테이션 클래스가 Spring MVC 의 구성 클래스로 작동해야 한다는 신호를 보낸다.
- 이 클래스는 많은 필수 MVC 관련 컴포넌트를 설정하는
WebMvcConfigurationSupport 에서 Spring MVC 구성을 가져온다.
WebMvcConfigurationSupport 에는 Spring Web Mvc 를 구성하기 위한 다양한 기본 설정들이 들어있다.
@EnableWebMvc 애노테이션을 사용하고 WebMvcConfigurer 인터페이스 를 구현하여 사용자 정의 구성을 활성화할 수도 있다.
용례
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/home").setViewName("home");
}
// other customization methods ...
}
XML 로 구성하는 방법
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.example" />
<mvc:annotation-driven />
<!-- Additional configuration, such as view resolvers, etc. -->
</beans>