[Spring boot & React] 개발 과정

·2025년 5월 22일

just공부

목록 보기
24/47

Error 1.

로그인 화면에서 로그인 성공 시 /dashboard 페이지로 넘어가줘야 하는데 No static resource dashboard 오류가 발생한다.

원인

/dashboard 경로에 해당하는 정적 파일이나 템플릿 파일 또는 컨트롤러가 없을 때 발생

해결방법

  1. HTML 제공 방식인 경우
  • src/main/resources/static/dashboard.html 파일 생성해서 해당 경로에서 접근할 수 있도록 하기
  • React인 경우, index.html에서 모든 경로를 처리할 수 있게 프론트엔드 라우팅을 구성한 후, Spring Boot의 resource handler를 수정해 모든 경로를 index.html로 포워딩하게끔 설정
  1. 템플릿 엔진 방식인 경우
  • src/main/resources/templates/dashboard.html 파일 생성 (Thymeleaf 등의 템플릿 엔진 사용 시)
  • 해당 경로를 반환하는 컨트롤러 작성
    @Controller
    public class DashboardController {
    	@GetMapping("/dashboard")
      public String dashboard(){
      	return "dashboard"; // dashboard.html 렌더링
      }
    }
  1. REST API 방식
  • 프론트엔드 분리
  • /dashboard 경로를 프론트엔드에서 처리하게끔 하고, Spring Boot에서는 해당 경로를 모두 index.html로 포워딩하는 리소스 핸들러를 추가
    @Configuration
    public class WebConfig implements WebMvcConfigurer {
    	@Override
      public void addResourceHandlers(ResourceHandlerRegistry registry){
      	registry.addResourceHandler("/**")
          		.addResourceLocations("classpath:/static/");
      }
      @Override
      public void addViewControllers(ViewControllerRegistry registry){
      	registry.addViewController("/{spring:\\w+}")
          		.setViewName("forward:/index.html");
      }
    }
profile
Whatever I want | Interested in DFIR, Security, Infra, Cloud

0개의 댓글