Spring MVC에서 정적 파일 서비스하기


Spring의 Dispatcher는 기본적으로 정적인 파일(html,css,image)을 서비스 할 수 없도록 막아놓고 있다

풀어주기 위해서는 dispatcher-servlet에 mvc스키마를 추가하고 리소스를 허용해줘야 한다 

[dispatcher-servlet.xml]
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">


xmlns:mvc="http://www.springframework.org/schema/mvc"
-mvc 처리기를 사용할때 mvc: 를 사용하겠다 선언

xsi:schemaLocation="
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd"
-mvc 스키마 파일(.xsd) 경로 설정


스키마 설정 후 다음 코드를 추가
<mvc:resources location="/static/" mapping="/**"></mvc:resources>
-사용자가 요청하는(/** : /이후의 모든 폴더나 파일)것은 (절대경로 /static/)에서 찾아라
(/static/ 이 마치 root 폴더처럼 바뀐다. localhost:5000/images)

설정이 끝나면 static 내부에 있는 정적 파일들을 서비스할 수 있다
(html,css,js,img 등 정적 파일별로 개별적으로 설정하면 페이지를 요청할 때 귀찮으니 위처럼 폴더 하나 정해서 한곳에 몰아놓는다)

http, https 언제고 못찾아서 오류나겠네

 

'Development > Java' 카테고리의 다른 글

데이터 서비스 클레스 분리, DB 연결 정보 분리 #6  (0) 2022.08.29
Tiles #5  (0) 2022.08.28
Spring MVC, 웹 개발환경 설정 #3  (0) 2022.08.26
DI, AOP #2  (0) 2022.08.25
Maven 사용법  (0) 2022.08.24

+ Recent posts