@EnableWebMvc
In spring-config.xml(your spring config file name) we can use <mvc:annotation-driven> to enable annotation driven in Spring MVC.
Using @EnableWebMvc we can create Spring MVC configuration in a class.
@Configuration
@EnableWebMvc
@ComponentScan("data.web")
public class AppConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver irvr = new InternalResourceViewResolver();
irvr.setPrefix("/WEB-INF/views/");
irvr.setSuffix(".jsp");
irvr.setExposeContextBeansAsAttributes(true);
return irvr;
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer config) {
config.enable();
}
}
@Configuration: it indicates this is a configuration class.
@EnableWebMvc: enables Spring MVC using a java class.
@ComponentScan("data.web"): It scan for the components in the data.web package.
viewResolver(): this method provides the InternalResourceViewResolver object which will be responsible for resloving the views based on the string returns from the controllers method and mapping the string with the jsp resides in the /WEB-INF/views/ dir.
configureDefaultServletHandling() method enables static content handling.