Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 |
Tags
- Stack
- JSON
- insertion sort
- 이것이 자바다
- 메모리구조
- buffer
- s
- 혼자 공부하는 C언어
- stream
- Graph
- C programming
- 윤성우의 열혈 자료구조
- Algorithm
- list 컬렉션
- Serialization
- datastructure
- C 언어 코딩 도장
- R
- 이스케이프 문자
- coding test
- 윤성우 열혈자료구조
- Selection Sorting
- 알기쉬운 알고리즘
- ㅅ
Archives
- Today
- Total
Engineering Note
[Spring] 스프링에서 정적파일 처리하기 본문
문제 상황
스프링 환경을 세팅하고 테스트를 위해 sample.htlm 정적파일(파일경로:resources/static/sample.html)을 브라우저에서 요청했을 때 에러가 발생했다.

에러 원인
Thymeleaf가 없는 환경에서 스프링 MVC의 모든 경로를 스프링에서 처리하려고 시도하기 때문
해결방법
리소스 핸들러로 경로를 지정해주면 된다.
package org.zerock.api01.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebMvc
public class CustomServletConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/");
}
}
"domain"에서 "/"(루트)경로 이하로 요는 URL요청은 classpath:/static/ 경로의 파일로 처리한다는 뜻이다. 기본 루트경로 하위는 컨트롤러나 타임리프 파일을 처리하기 위해, "/files" 이하로 수정해주었다.
package org.zerock.api01.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebMvc
public class CustomServletConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
registry.addResourceHandler("/files/**")
.addResourceLocations("classpath:/static/");
}
}
참고자료: 자바웹개발 워크북(구멍가게 코딩단, p777)
'Server > Spring' 카테고리의 다른 글
| [Spring] @RequiredArgsContructor, 생성자 주입 (0) | 2025.12.27 |
|---|---|
| [Spring] Spring AOP (0) | 2025.12.25 |
| [Spring] CommandLineRunner (0) | 2025.10.22 |
| [Spring] Spring Boot 프로젝트 생성시 사용 되는 개념 (0) | 2025.10.08 |
| [Spring] @Transactional 어노테이션과 Spring AOP의 관계 (0) | 2025.10.07 |
Comments