둘 중 하나를 골라서 설정해야 한다. 나는 java 클래스를 이용해 설정했다.
선택한 두 개의 root-context.xml과 web.xml을 삭제한다.
해당 경로의 servlet.xml까지 삭제한다.
그 다음 삭제한 xml들을 대체할 자바 클래스를 생성한다
RootConfig.java
package com.coco.config;
@Configuration
//참조할 패키지
@ComponentScan(basePackages = {"com.coco.sample"})
public class RootConfig {
}
ServletConfig.java
@EnableWebMvc
@ComponentScan(basePackages = {"com.coco.controller"})
public class ServletConfig implements WebMvcConfigurer {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/views");
bean.setSuffix(".jsp");
registry.viewResolver(bean);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources");
}
}
WebConfig.java
package com.coco.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {RootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] {ServletConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}