리주의 프로그래밍 공부

정적파일 서비스 본문

스프링(Spring) 공부

정적파일 서비스

Leezu_ 2020. 12. 31. 15:36

정적인 리소스 : 이미지, html, js, css 등

  <servlet>
  	<servlet-name>dispatcher</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>dispatcher</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>

위의 코드에서 url-pattern에 있는 /가 jsp만 통과시키고 정적인 파일은 제한

 

정적인 파일을 서비스하기 위해서 -servlet.xml 파일을 다음과 같이 수정

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    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">

    <bean id="/index" class="com.newlecture.web.controller.IndexController"/>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="prefix" value="/WEB-INF/view/"></property>
    	<property name="suffix" value=".jsp"></property>
    </bean>
    
</beans>

위의 코드(기존 -servlet.xml)에서 3줄 추가(17번째 줄은 예시)

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <bean id="/index" class="com.newlecture.web.controller.IndexController"/>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="prefix" value="/WEB-INF/view/"></property>
    	<property name="suffix" value=".jsp"></property>
    </bean>
    // location : 물리적 폴더 경로     mapping : 접속 url 패턴
    <mvc:resources location="/static/" mapping="/**"></mvc:resources>
</beans>

3번째줄 : "http://www.springframework.org/schema/mvc" 를 mvc로 치환한다고 생각하면 편함(namespace)

 

17번째줄 : 리소스 요청이 들어오는 url을 /static/으로 보냄

'스프링(Spring) 공부' 카테고리의 다른 글

DI를 Annotation으로 변경  (0) 2021.01.06
설정파일 분리하기  (0) 2021.01.05
연결정보 분리  (0) 2021.01.05
Wildcard  (0) 2021.01.04
ViewResolver  (0) 2020.12.31