[spring] 프로젝트 설정 시 오류 해결(css,js 경로 설정)

2021. 3. 2. 01:28프론트엔드 개발

반응형

ERR_ABORTED 404와 같은 에러가 뜨게 되었는데 이는 경로문제로 인하여 발생하는 오류였다.

 

구글링을 통하여 검색한 결과,

package config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="controller")
public class MvcConfig implements WebMvcConfigurer{
	@Override
	// dispatcherservlet의 매핑 경로를 /로 줄 때 jsp/html/css 등 올바르게 처리하기 위함
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}
	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
		registry.jsp("/WEB-INF/views/",".jsp");
	}
	
}

Mvcconfig라는 자바 파일에서 매핑 시켜줄 때 configureViewResolvers만 override하여 설정하였기 때문에 css,js와 같은 모든 파일들의 경로 설정이 잘못 설정된 것이었다.

 

이를 configureDefaultServletHandling이라는 모듈을 import하여 설정해주게 되면 위 에러를 해결할 수 있다.

반응형