SlideShare a Scribd company logo
1 of 43
Download to read offline
Resource Handling 
in Spring MVC
박용권 
SK planet 
: 봄싹(SpringSprout) 
: 한국 스프링 사용자 모임(KSUG) 
: 라 스칼라 코딩단 
http://about.me/arawn 
: twitter / @arawnkr
Spring MVC에서 
정적 자원(css, js, etc)을 
처리해본 경험을 공유합니다.
Spring MVC : 리소스 제공(Serving) 
✔ ResourceHttpRequestHandler 
: URL 패턴에 따라 정적 자원 요청을 처리 
: org.springframework.core.io.Resource 인터페이스를 사용 
servletcontext, classpath, filesystem, etc 
: HTTP 캐시 설정 기능 제공 
expires 또는 cache-control를 사용한 캐시 설정 
last-modified 헤더 평가를 통해 304 상태코드 응답 
! 
✔ 설정 간소화 기능 제공 
: Java 기반 설정시 WebMvcConfigurer.addResourceHandlers(…) 사용 
: XML 기반 설정시 mvc:resources 태그 사용
리소스 제공 설정 
Java Config 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
registry.addResourceHandler("리소스에 접근할 URL 패턴") 
.addResourceLocations("리소스가 있는 위치"); 
} 
XML Config 
<mvc:resources mapping="리소스에 접근할 URL 패턴" 
location="리소스가 있는 위치"/>
리소스 제공 설정 
리소스에 접근할 URL 패턴 정의 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
registry.addResourceHandler("/resources/**") 
.addResourceLocations("/resources/"); 
} 
http://spring.io/resources/css/default.css 
http://spring.io/resources/js/spring-by-pivotal.png 
http://spring.io/resources/img/spring-by-pivotal.png
리소스 제공 설정 
리소스가 있는 위치 설정 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
registry.addResourceHandler("/resources/**") 
.addResourceLocations("/resources/"); 
} 
http://spring.io/resources/css/default.css 
. 
"## WEB-INF 
$ "## classes 
$ &## lib 
&## resources 
&## css 
&## default.css
DEMO 
(리소스 제공 설정)
Resource 인터페이스에 대한 다양한 구현체 
✔ ServletContextResource 
! 
! 
✔ ClassPathResource 
! 
! 
✔ FileSystemResource 
! 
! 
✔ UrlResource 
! 
! 
✔ etc 
registry.addResourceHandler("/resources/**") 
.addResourceLocations("/resources/"); 
registry.addResourceHandler("/resources/**") 
.addResourceLocations("classpath:/resources/"); 
registry.addResourceHandler("/resources/**") 
.addResourceLocations("file:/resources/“); 
registry.addResourceHandler("/resources/**") 
.addResourceLocations("http://spring.io");
리소스를 효율적으로 다루는 캐시 설정 
Java Config 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
registry.addResourceHandler("/resources/**") 
.addResourceLocations("/resources/") 
.setCachePeriod(31556926); 
} 
XML Config 
<mvc:resources mapping="/resources/**" 
location="/resources/" 
cache-period="31556926"/>
리소스를 효율적으로 다루는 캐시 설정 
Java Config 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
registry.addResourceHandler("/resources/**") 
.addResourceLocations("/resources/") 
.setCachePeriod(31556926); 
} 
Response headers : 
Cache-Control: "max-age=31556926, must-revalidate" 
Expires: "Sun, 16 Nov 2014 07:39:20 GMT" 
Last-Modified: "Thu, 20 Nov 2014 10:49:18 GMT"
리소스를 효율적으로 다루는 캐시 설정 
Java Config 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
registry.addResourceHandler("/resources/**") 
.addResourceLocations("/resources/") 
.setCachePeriod(0); 
} 
Response headers : 
Pragma: "no-cache" 
Cache-Control: "no-cache, no-store" 
Expires: "Thu, 01 Jan 1970 00:00:00 GMT”
DEMO 
(캐시 설정에 따른 동작)
Multi Module 
Web Application 
/ Backend) 
/ Server-side) 
(Frontend 
(Client-side
프로젝트 구조 
"## build.gradle - Gradle : 빌드 자동화 
"## gradle.properties 
"## settings.gradle 
"## backend - Spring IO Platform 사용해 개발된 Server-side 모듈 
$ &## src 
$ "## main 
$ $ "## java 
$ $ &## resources 
$ &## test 
&## frontend - 정적 자원(html, css, javascript)을 제공하는 Client-side 모듈 
"## package.json 
"## bower.json 
"## Gruntfile.js 
&## src 
"## assets 
"## helpers 
"## includes 
"## layouts 
"## libs 
&## pages
backend에 적용된 기술 훑어보기 
"## build.gradle - Gradle : 빌드 자동화 
"## gradle.properties 
"## settings.gradle 
"## backend - Spring IO Platform 사용해 개발된 Server-side 모듈 
$ &## src 
$ "## main 
$ $ "## java 
$ $ &## resources 
$ &## test 
&## frontend - 정적 자원(html, css, javascript)을 제공하는 Client-side 모듈 
"## package.json 
"## bower.json 
"## Gruntfile.js 
&## src 
"## assets 
"## helpers 
"## includes 
"## layouts 
"## libs 
&## pages 
✔ Spring Boot 
: Spring IO Platform 기반 개발을 빠르고 다양한 방법으로 시작 
: 애플리케이션 기능외 필요한 공통 컴포넌트를 제공 
✔ Thymeleaf 
: HTML 태그/속성 기반의 템플릿 엔진 
: Spring MVC & Security 와 통합을 위한 라이브러리 제공
frontend에 적용된 도구 훑어보기 
"## build.gradle - Gradle : 빌드 자동화 
"## gradle.properties 
"## settings.gradle 
"## backend - Spring IO Platform 사용해 개발된 Server-side 모듈 
$ &## src 
$ "## main 
$ $ "## java 
$ $ &## resources 
$ &## test 
&## frontend - 정적 자원(html, css, javascript)을 제공하는 Client-side 모듈 
"## package.json 
"## bower.json 
"## Gruntfile.js 
&## src 
"## assets 
"## helpers 
"## includes 
"## layouts 
"## libs 
&## pages 
✔ NPM(Node Package Manager) 
: 개발환경 및 의존성 관리 
✔ Bower 
: JavaScript Lib 의존성 관리(jquery, bootstrap, etc) 
✔ Grunt 
: Client-side 빌드 자동화 
: 다양한 Plugin 지원( jshint, usemin, filerev, assemble, etc )
frontend에 적용된 기술 훑어보기 (1/3) 
"## backend 
&## frontend 
"## package.json 
"## Gruntfile.js 
"## bower.json 
"## src 
$ "## assets 
$ $ "## css 
$ $ $ "## cover.css 
$ $ $ &## default.css 
$ $ &## js 
$ $ &## default.js 
$ "## libs 
$ $ "## bootstrap 
$ $ &## jquery 
$ "## includes 
$ $ "## common-css.hbs 
$ $ &## common-scripts.hbs 
$ "## layouts 
$ $ &## default.hbs 
$ &## pages 
$ &## about.hbs 
&## dist 
"## assets 
$ "## css 
$ $ &## style.min.99501602.css 
$ &## js 
$ &## app.min.264ed108.js 
&## pages 
&## about.html 
✔ CSS/JS 최적화(병합 및 압축) 
: grunt-usemin 
: grunt-contrib-concat 
: grunt-contrib-cssmin 
: grunt-contrib-uglify 
✔ Fingerprinting 
: grunt-filerev 
더 알아보려면 여기로! 
http://goo.gl/oGVCYT
frontend에 적용된 기술 훑어보기 (2/3) 
"## backend 
&## frontend 
"## package.json 
"## Gruntfile.js 
"## bower.json 
"## src 
$ "## assets 
$ $ "## css 
$ $ $ "## cover.css 
$ $ $ &## default.css 
$ $ &## js 
$ $ &## default.js 
$ "## libs 
$ $ "## bootstrap 
$ $ &## jquery 
$ "## includes 
$ $ "## common-css.hbs 
$ $ &## common-scripts.hbs 
$ "## layouts 
$ $ &## default.hbs 
$ &## pages 
$ &## about.hbs 
&## dist 
"## assets 
$ "## css 
$ $ &## style.min.99501602.css 
$ &## js 
$ &## app.min.264ed108.js 
&## pages 
&## about.html 
✔ 템플릿(HTML) 생성 
: assemble
frontend에 적용된 기술 훑어보기 (3/3) 
이제 
코드로 
만나보시죠! 
"## backend 
&## frontend 
"## package.json 
"## Gruntfile.js 
"## bower.json 
"## src 
$ "## assets 
$ $ "## css 
$ $ $ "## cover.css 
$ $ $ &## default.css 
$ $ &## js 
$ $ &## default.js 
$ "## libs 
$ $ "## bootstrap 
$ $ &## jquery 
$ "## includes 
$ $ "## common-css.hbs 
$ $ &## common-scripts.hbs 
$ "## layouts 
$ $ &## default.hbs 
$ &## pages 
$ &## about.hbs 
&## dist 
"## assets 
$ "## css 
$ $ &## style.min.99501602.css 
$ &## js 
$ &## app.min.264ed108.js 
&## pages 
&## about.html
frontend가 가출한 이유 
"## backend 
&## frontend 
"## package.json 
"## Gruntfile.js 
"## bower.json 
"## src 
$ "## assets 
$ $ "## css 
$ $ $ "## cover.css 
$ $ $ &## default.css 
$ $ &## js 
$ $ &## default.js 
$ "## libs 
$ $ "## bootstrap 
$ $ &## jquery 
$ "## includes 
$ $ "## common-css.hbs 
$ $ &## common-scripts.hbs 
$ "## layouts 
$ $ &## default.hbs 
$ &## pages 
$ &## about.hbs 
&## dist 
"## assets 
$ "## css 
$ $ &## style.min.99501602.css 
$ &## js 
$ &## app.min.264ed108.js 
&## pages 
&## about.html 
✔ dependency management 
✔ modularity 
✔ tests 
✔ build automation ( vs artifacts)
지금부터 Frontend의 
자원(html, css, javascript, image)을 
사용하는 방법을 살펴봅니다.
Frontend 의존성 다루기 
frontend 
backend
개발자의 친구 복붙- 
"## backend 
$ &## src 
$ "## main 
$ $ "## java 
$ $ "## resources 
$ $ &## webapp 
$ &## test 
$ "## java 
$ &## resources 
&## frontend 
&## src 
$ "## assets 
$ "## helpers 
$ "## includes 
$ "## layouts 
$ "## libs 
$ &## pages 
&## dist 
"## assets 
&## pages 
crtl + v 
crtl + c
Web Libraries in Jars 
WebJars 
✔ Client-side 웹 라이브러리를 JAR로 묶어서 제공하는 서비스 
✔ JVM 기반 빌드 도구(gradle, maven, sbt, etc)를 지원 (maven 저장소) 
bootstrap-3.3.1.jar 
&## META-INF 
&## resources 
&## webjars 
&## bootstrap 
&## 3.3.1 
"## css 
$ "## bootstrap.css 
$ &## bootstrap.min.css 
"## js 
$ "## bootstrap.js 
$ &## bootstrap.min.js 
"## fonts 
"## less 
&## webjars-requirejs.js 
<dependencies> 
<dependency> 
<groupId>org.webjars</groupId> 
<artifactId>bootstrap</artifactId> 
<version>3.3.1</version> 
</dependency> 
</dependencies>
DEMO 
(Servlet 3에서 webjars 사용)
Gradle로 통합하기 
"## build.gradle 
"## backend 
$ &## src 
$ "## main 
$ $ "## java 
$ $ "## resources 
$ $ &## webapp 
$ &## test 
&## frontend 
&## src 
$ "## assets 
$ "## helpers 
$ "## includes 
$ "## layouts 
$ "## libs 
$ &## pages 
&## dist 
"## assets 
&## pages 
frontend.jar 로 만든 후 
backend 모듈에 의존성을 추가!
frontend를 빌드 후 jar로 만들기 
"## build.gradle 
"## backend 
$ &## src 
$ "## main 
$ $ "## java 
$ $ "## resources 
$ $ &## webapp 
$ &## test 
&## frontend 
&## src 
$ "## assets 
$ "## helpers 
$ "## includes 
$ "## layouts 
$ "## libs 
$ &## pages 
&## dist 
"## assets 
&## pages 
project(':frontend') { 
apply plugin: 'java' 
! 
task npmInstall(type:Exec) { 
// do something 
} 
! 
task gruntBuild(type:Exec, dependsOn: [npmInstall]) { 
// do something 
} 
! 
jar { 
from 'dist' 
includeEmptyDirs = false 
} 
jar.dependsOn gruntBuild 
}
backend에 frontend 의존성 추가 
"## build.gradle 
"## backend 
$ &## src 
$ "## main 
$ $ "## java 
$ $ "## resources 
$ $ &## webapp 
$ &## test 
&## frontend 
&## src 
$ "## assets 
$ "## helpers 
$ "## includes 
$ "## layouts 
$ "## libs 
$ &## pages 
&## dist 
"## assets 
&## pages 
project(':frontend') { 
apply plugin: 'java' 
! 
task npmInstall(type:Exec) { 
// do something 
} 
! 
task gruntBuild(type:Exec, dependsOn: [npmInstall]) { 
// do something 
} 
! 
jar { 
from 'dist' 
includeEmptyDirs = false 
} 
jar.dependsOn gruntBuild 
} 
! 
project(':backend') { 
apply plugin: 'war' 
! 
dependencies { 
runtime project(':frontend') 
} 
}
DEMO 
(Gradle 통합과 자원 사용)
개발과 배포는 다르다
frontend: 배포시에는 최적화된 자원을 사용 
> grunt build:release 
"## backend 
&## frontend 
"## src 
$ "## assets 
$ $ "## css 
$ $ "## img 
$ $ &## js 
$ "## libs 
$ $ "## jquery 
$ $ &## bootstrap 
$ &## pages 
&## dist 
"## assets 
$ "## css 
$ $ &## style.min.6bde543a.css 
$ &## js 
$ &## app.min.142ca07c.js 
&## pages 
&## about.html 
<!DOCTYPE html> 
<html lang="ko"> 
<head> 
<link href='/assets/css/style.min.6bde543a.css' 
rel='stylesheet' type='text/css'/> 
</head> 
<body> 
<div class="site-wrapper"> 
// 생략 
</div> 
<script src='/assets/js/app.min.264ed108.js'></script> 
</body> 
</html>
frontend: 배포시에는 최적화된 자원을 사용 
> grunt build:release 
"## backend 
&## frontend 
"## src 
$ "## assets 
$ $ "## css 
$ $ "## img 
$ $ &## js 
$ "## libs 
$ $ "## jquery 
$ $ &## bootstrap 
$ &## pages 
&## dist 
"## assets 
$ "## css 
$ $ &## style.min.6bde543a.css 
$ &## js 
$ &## app.min.142ca07c.js 
&## pages 
&## about.html 
Classpath 위치한 자원을 사용 
! 
// build.gradle 
project(':frontend') { 
sourceSets.main.resources { srcDir 'dist' } 
} 
! 
// Java Config 
registry.addResourceHandler("/assets/**") 
.addResourceLocations("classpath:assets/");
frontend: 개발시에는 작성중인 css, js 사용 
> grunt build:develop 
<!DOCTYPE html> 
<html lang="ko"> 
<head> 
<link href="/libs/bootstrap/dist/css/bootstrap.css"/> 
<link href="/assets/css/default.css"/> 
</head> 
<body> 
<div class="site-wrapper"> 
// 생략 
</div> 
<script src="/libs/jquery/dist/jquery.js"/> 
<script src="/libs/bootstrap/dist/js/bootstrap.js"/> 
<script src="/assets/js/default.js"/> 
</body> 
</html> 
"## backend 
&## frontend 
"## src 
$ "## assets 
$ $ "## css 
$ $ $ &## default.css 
$ $ "## img 
$ $ &## js 
$ $ &## default.js 
$ "## libs 
$ $ "## jquery 
$ $ &## bootstrap 
$ &## pages 
&## dist 
&## pages 
&## about.html
backend: 개발시에는 어떻게 자원에 접근하지? 
> grunt build:develop 
“어라!? dist/assets이 없네!?” 
"## backend 
&## frontend 
"## src 
$ "## assets 
$ $ "## css 
$ $ $ &## default.css 
$ $ "## img 
$ $ &## js 
$ $ &## default.js 
$ "## libs 
$ $ "## jquery 
$ $ &## bootstrap 
$ &## pages 
&## dist 
&## pages 
&## about.html
backend: 개발시에는 어떻게 자원에 접근하지? 
> grunt build:develop 
src를 Classpath에 추가해볼까? 
! 
// build.gradle 
project(':frontend') { 
sourceSets.main.resources { srcDirs 'dist', 'src' } 
} 
! 
// console 
> gradle build 
"## backend 
&## frontend 
"## src 
$ "## assets 
$ $ "## css 
$ $ $ &## default.css 
$ $ "## img 
$ $ &## js 
$ $ &## default.js 
$ "## libs 
$ $ "## jquery 
$ $ &## bootstrap 
$ &## pages 
&## dist 
&## pages 
&## about.html
backend: 개발시에는 어떻게 자원에 접근하지? 
없는게 없는 frontend.jar 
! 
frontend.jar 
"## assets 
$ "## css 
$ $ &## default.css 
$ "## img 
$ &## js 
$ &## default.js 
"## libs 
$ "## jquery 
$ &## bootstrap 
&## pages 
> grunt build:develop 
"## backend 
&## frontend 
"## src 
$ "## assets 
$ $ "## css 
$ $ $ &## default.css 
$ $ "## img 
$ $ &## js 
$ $ &## default.js 
$ "## libs 
$ $ "## jquery 
$ $ &## bootstrap 
$ &## pages 
&## dist 
&## pages 
&## about.html 
이건 아니야… ;;;
backend: 환경에 따른 자원 접근 전략 변경 
"## backend 
&## frontend 
"## src 
$ "## assets 
$ $ "## css 
$ $ "## img 
$ $ &## js 
$ "## libs 
$ $ "## jquery 
$ $ &## bootstrap 
$ &## pages 
&## dist 
"## assets 
$ "## css 
$ "## img 
$ &## js 
&## templates 
&## about.html 
String locations; 
! 
if(개발) { 
locations = src/assets 사용 
} else { 
locations = dist/assets 사용 
} 
! 
registry.addResourceHandler("/assets/**") 
.addResourceLocations(locations);
DEMO 
(환경에 따른 자원 접근 전략 변경)
회고
더 하고 싶었던 
이야기가 있었지만…
묻고 답하는 시간 
https://github.com/arawn/resource-handling-in-springmvc
끝.

More Related Content

What's hot

[Pgday.Seoul 2017] 6. GIN vs GiST 인덱스 이야기 - 박진우
[Pgday.Seoul 2017] 6. GIN vs GiST 인덱스 이야기 - 박진우[Pgday.Seoul 2017] 6. GIN vs GiST 인덱스 이야기 - 박진우
[Pgday.Seoul 2017] 6. GIN vs GiST 인덱스 이야기 - 박진우PgDay.Seoul
 
Fun with click house window functions webinar slides 2021-08-19
Fun with click house window functions webinar slides  2021-08-19Fun with click house window functions webinar slides  2021-08-19
Fun with click house window functions webinar slides 2021-08-19Altinity Ltd
 
How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021
How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021
How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021Natan Silnitsky
 
공간정보 거점대학 - OpenLayers의 고급 기능 이해 및 실습
 공간정보 거점대학 - OpenLayers의 고급 기능 이해 및 실습 공간정보 거점대학 - OpenLayers의 고급 기능 이해 및 실습
공간정보 거점대학 - OpenLayers의 고급 기능 이해 및 실습HaNJiN Lee
 
State of OpenGXT: 오픈소스 공간분석엔진
State of OpenGXT: 오픈소스 공간분석엔진State of OpenGXT: 오픈소스 공간분석엔진
State of OpenGXT: 오픈소스 공간분석엔진MinPa Lee
 
스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration
스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration
스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration수홍 이
 
Tricks every ClickHouse designer should know, by Robert Hodges, Altinity CEO
Tricks every ClickHouse designer should know, by Robert Hodges, Altinity CEOTricks every ClickHouse designer should know, by Robert Hodges, Altinity CEO
Tricks every ClickHouse designer should know, by Robert Hodges, Altinity CEOAltinity Ltd
 
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오PgDay.Seoul
 
[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL
[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL
[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQLPgDay.Seoul
 
오픈소스GIS 개론 과정 - OpenLayers 기초
오픈소스GIS 개론 과정 - OpenLayers 기초오픈소스GIS 개론 과정 - OpenLayers 기초
오픈소스GIS 개론 과정 - OpenLayers 기초HaNJiN Lee
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Domenic Denicola
 
jQuery Fundamentals
jQuery FundamentalsjQuery Fundamentals
jQuery FundamentalsGil Fink
 
Introducing ASP.NET Core 2.0
Introducing ASP.NET Core 2.0Introducing ASP.NET Core 2.0
Introducing ASP.NET Core 2.0Steven Smith
 
Node.js middleware: Never again!
Node.js middleware: Never again!Node.js middleware: Never again!
Node.js middleware: Never again!Timur Shemsedinov
 
ClickHouse Materialized Views: The Magic Continues
ClickHouse Materialized Views: The Magic ContinuesClickHouse Materialized Views: The Magic Continues
ClickHouse Materialized Views: The Magic ContinuesAltinity Ltd
 
Fabric.js @ Falsy Values
Fabric.js @ Falsy ValuesFabric.js @ Falsy Values
Fabric.js @ Falsy ValuesJuriy Zaytsev
 

What's hot (20)

[Pgday.Seoul 2017] 6. GIN vs GiST 인덱스 이야기 - 박진우
[Pgday.Seoul 2017] 6. GIN vs GiST 인덱스 이야기 - 박진우[Pgday.Seoul 2017] 6. GIN vs GiST 인덱스 이야기 - 박진우
[Pgday.Seoul 2017] 6. GIN vs GiST 인덱스 이야기 - 박진우
 
Fun with click house window functions webinar slides 2021-08-19
Fun with click house window functions webinar slides  2021-08-19Fun with click house window functions webinar slides  2021-08-19
Fun with click house window functions webinar slides 2021-08-19
 
How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021
How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021
How to successfully manage a ZIO fiber’s lifecycle - Functional Scala 2021
 
QGIS 활용
QGIS 활용QGIS 활용
QGIS 활용
 
공간정보 거점대학 - OpenLayers의 고급 기능 이해 및 실습
 공간정보 거점대학 - OpenLayers의 고급 기능 이해 및 실습 공간정보 거점대학 - OpenLayers의 고급 기능 이해 및 실습
공간정보 거점대학 - OpenLayers의 고급 기능 이해 및 실습
 
State of OpenGXT: 오픈소스 공간분석엔진
State of OpenGXT: 오픈소스 공간분석엔진State of OpenGXT: 오픈소스 공간분석엔진
State of OpenGXT: 오픈소스 공간분석엔진
 
스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration
스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration
스프링캠프 2016 발표 - Deep dive into spring boot autoconfiguration
 
Tricks every ClickHouse designer should know, by Robert Hodges, Altinity CEO
Tricks every ClickHouse designer should know, by Robert Hodges, Altinity CEOTricks every ClickHouse designer should know, by Robert Hodges, Altinity CEO
Tricks every ClickHouse designer should know, by Robert Hodges, Altinity CEO
 
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오
 
[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL
[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL
[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL
 
오픈소스GIS 개론 과정 - OpenLayers 기초
오픈소스GIS 개론 과정 - OpenLayers 기초오픈소스GIS 개론 과정 - OpenLayers 기초
오픈소스GIS 개론 과정 - OpenLayers 기초
 
What Can Compilers Do for Us?
What Can Compilers Do for Us?What Can Compilers Do for Us?
What Can Compilers Do for Us?
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
 
Rapid json tutorial
Rapid json tutorialRapid json tutorial
Rapid json tutorial
 
jQuery Fundamentals
jQuery FundamentalsjQuery Fundamentals
jQuery Fundamentals
 
Introducing ASP.NET Core 2.0
Introducing ASP.NET Core 2.0Introducing ASP.NET Core 2.0
Introducing ASP.NET Core 2.0
 
Node.js middleware: Never again!
Node.js middleware: Never again!Node.js middleware: Never again!
Node.js middleware: Never again!
 
ClickHouse Materialized Views: The Magic Continues
ClickHouse Materialized Views: The Magic ContinuesClickHouse Materialized Views: The Magic Continues
ClickHouse Materialized Views: The Magic Continues
 
Flutter 3
Flutter 3Flutter 3
Flutter 3
 
Fabric.js @ Falsy Values
Fabric.js @ Falsy ValuesFabric.js @ Falsy Values
Fabric.js @ Falsy Values
 

Similar to Resource Handling in Spring MVC

Node.js
Node.jsNode.js
Node.jsymtech
 
Front-end Development Process - 어디까지 개선할 수 있나
Front-end Development Process - 어디까지 개선할 수 있나Front-end Development Process - 어디까지 개선할 수 있나
Front-end Development Process - 어디까지 개선할 수 있나JeongHun Byeon
 
스프링군살없이세팅하기(The way to setting the Spring framework for web.)
스프링군살없이세팅하기(The way to setting the Spring framework for web.)스프링군살없이세팅하기(The way to setting the Spring framework for web.)
스프링군살없이세팅하기(The way to setting the Spring framework for web.)EunChul Shin
 
Isomorphicspring Isomorphic - spring web seminar 2015
Isomorphicspring Isomorphic - spring web seminar 2015Isomorphicspring Isomorphic - spring web seminar 2015
Isomorphicspring Isomorphic - spring web seminar 2015sung yong jung
 
XE 오픈 세미나(2014-06-28) - (2/3) 레이아웃 제작 노하우
XE 오픈 세미나(2014-06-28) - (2/3) 레이아웃 제작 노하우XE 오픈 세미나(2014-06-28) - (2/3) 레이아웃 제작 노하우
XE 오픈 세미나(2014-06-28) - (2/3) 레이아웃 제작 노하우XpressEngine
 
Backend Master | 3.1.1 Build - JS build tools
Backend Master | 3.1.1 Build - JS build toolsBackend Master | 3.1.1 Build - JS build tools
Backend Master | 3.1.1 Build - JS build toolsKyunghun Jeon
 
자바스크립트 프레임워크 살펴보기
자바스크립트 프레임워크 살펴보기자바스크립트 프레임워크 살펴보기
자바스크립트 프레임워크 살펴보기Jeado Ko
 
React native 개발 및 javascript 기본
React native 개발 및 javascript 기본React native 개발 및 javascript 기본
React native 개발 및 javascript 기본Tj .
 
[아꿈사/111105] html5 9장 클라이언트측 데이터로 작업하기
[아꿈사/111105] html5 9장 클라이언트측 데이터로 작업하기[아꿈사/111105] html5 9장 클라이언트측 데이터로 작업하기
[아꿈사/111105] html5 9장 클라이언트측 데이터로 작업하기sung ki choi
 
Jenkins를 활용한 javascript 개발
Jenkins를 활용한 javascript 개발Jenkins를 활용한 javascript 개발
Jenkins를 활용한 javascript 개발지수 윤
 
캠프앱 개발 사례를 통해 본 하이브리드앱 어디까지 | Devon 2012
캠프앱 개발 사례를 통해 본 하이브리드앱 어디까지 | Devon 2012캠프앱 개발 사례를 통해 본 하이브리드앱 어디까지 | Devon 2012
캠프앱 개발 사례를 통해 본 하이브리드앱 어디까지 | Devon 2012Daum DNA
 

Similar to Resource Handling in Spring MVC (20)

4-3. jquery
4-3. jquery4-3. jquery
4-3. jquery
 
Node.js
Node.jsNode.js
Node.js
 
Front-end Development Process - 어디까지 개선할 수 있나
Front-end Development Process - 어디까지 개선할 수 있나Front-end Development Process - 어디까지 개선할 수 있나
Front-end Development Process - 어디까지 개선할 수 있나
 
스프링군살없이세팅하기(The way to setting the Spring framework for web.)
스프링군살없이세팅하기(The way to setting the Spring framework for web.)스프링군살없이세팅하기(The way to setting the Spring framework for web.)
스프링군살없이세팅하기(The way to setting the Spring framework for web.)
 
요즘웹개발
요즘웹개발요즘웹개발
요즘웹개발
 
Isomorphicspring Isomorphic - spring web seminar 2015
Isomorphicspring Isomorphic - spring web seminar 2015Isomorphicspring Isomorphic - spring web seminar 2015
Isomorphicspring Isomorphic - spring web seminar 2015
 
XE 오픈 세미나(2014-06-28) - (2/3) 레이아웃 제작 노하우
XE 오픈 세미나(2014-06-28) - (2/3) 레이아웃 제작 노하우XE 오픈 세미나(2014-06-28) - (2/3) 레이아웃 제작 노하우
XE 오픈 세미나(2014-06-28) - (2/3) 레이아웃 제작 노하우
 
Backend Master | 3.1.1 Build - JS build tools
Backend Master | 3.1.1 Build - JS build toolsBackend Master | 3.1.1 Build - JS build tools
Backend Master | 3.1.1 Build - JS build tools
 
자바스크립트 프레임워크 살펴보기
자바스크립트 프레임워크 살펴보기자바스크립트 프레임워크 살펴보기
자바스크립트 프레임워크 살펴보기
 
Hacosa jquery 1th
Hacosa jquery 1thHacosa jquery 1th
Hacosa jquery 1th
 
React native 개발 및 javascript 기본
React native 개발 및 javascript 기본React native 개발 및 javascript 기본
React native 개발 및 javascript 기본
 
Html5 performance
Html5 performanceHtml5 performance
Html5 performance
 
One-day-codelab
One-day-codelabOne-day-codelab
One-day-codelab
 
Nodejs express
Nodejs expressNodejs express
Nodejs express
 
okspring3x
okspring3xokspring3x
okspring3x
 
[아꿈사/111105] html5 9장 클라이언트측 데이터로 작업하기
[아꿈사/111105] html5 9장 클라이언트측 데이터로 작업하기[아꿈사/111105] html5 9장 클라이언트측 데이터로 작업하기
[아꿈사/111105] html5 9장 클라이언트측 데이터로 작업하기
 
Jenkins를 활용한 javascript 개발
Jenkins를 활용한 javascript 개발Jenkins를 활용한 javascript 개발
Jenkins를 활용한 javascript 개발
 
테스트
테스트테스트
테스트
 
Grunt
GruntGrunt
Grunt
 
캠프앱 개발 사례를 통해 본 하이브리드앱 어디까지 | Devon 2012
캠프앱 개발 사례를 통해 본 하이브리드앱 어디까지 | Devon 2012캠프앱 개발 사례를 통해 본 하이브리드앱 어디까지 | Devon 2012
캠프앱 개발 사례를 통해 본 하이브리드앱 어디까지 | Devon 2012
 

More from Arawn Park

우린 같은 곳을 바라 보고 있나요?
우린 같은 곳을 바라 보고 있나요?우린 같은 곳을 바라 보고 있나요?
우린 같은 곳을 바라 보고 있나요?Arawn Park
 
코틀린 멀티플랫폼, 미지와의 조우
코틀린 멀티플랫폼, 미지와의 조우코틀린 멀티플랫폼, 미지와의 조우
코틀린 멀티플랫폼, 미지와의 조우Arawn Park
 
kotlinx.serialization
kotlinx.serializationkotlinx.serialization
kotlinx.serializationArawn Park
 
#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기Arawn Park
 
우아한 모노리스
우아한 모노리스우아한 모노리스
우아한 모노리스Arawn Park
 
잘 키운 모노리스 하나 열 마이크로서비스 안 부럽다
잘 키운 모노리스 하나 열 마이크로서비스 안 부럽다잘 키운 모노리스 하나 열 마이크로서비스 안 부럽다
잘 키운 모노리스 하나 열 마이크로서비스 안 부럽다Arawn Park
 
점진적인 레거시 웹 애플리케이션 개선 과정
점진적인 레거시 웹 애플리케이션 개선 과정점진적인 레거시 웹 애플리케이션 개선 과정
점진적인 레거시 웹 애플리케이션 개선 과정Arawn Park
 
Introduction to Kotlin
Introduction to KotlinIntroduction to Kotlin
Introduction to KotlinArawn Park
 
Reactive Web - Servlet & Async, Non-blocking I/O
Reactive Web - Servlet & Async, Non-blocking I/OReactive Web - Servlet & Async, Non-blocking I/O
Reactive Web - Servlet & Async, Non-blocking I/OArawn Park
 
Spring framework 4.x
Spring framework 4.xSpring framework 4.x
Spring framework 4.xArawn Park
 
씹고 뜯고 맛보고 즐기는 스트림 API
씹고 뜯고 맛보고 즐기는 스트림 API씹고 뜯고 맛보고 즐기는 스트림 API
씹고 뜯고 맛보고 즐기는 스트림 APIArawn Park
 
Spring framework 3.2 > 4.0 — themes and trends
Spring framework 3.2 > 4.0 — themes and trendsSpring framework 3.2 > 4.0 — themes and trends
Spring framework 3.2 > 4.0 — themes and trendsArawn Park
 
overview of spring4
overview of spring4overview of spring4
overview of spring4Arawn Park
 
조금 더 좋은 개발자가 된다는 것( 부제: 컨퍼런스의 발표자가 된다는 것 )
조금 더 좋은 개발자가 된다는 것( 부제: 컨퍼런스의 발표자가 된다는 것 )조금 더 좋은 개발자가 된다는 것( 부제: 컨퍼런스의 발표자가 된다는 것 )
조금 더 좋은 개발자가 된다는 것( 부제: 컨퍼런스의 발표자가 된다는 것 )Arawn Park
 
[Spring Camp 2013] Java Configuration 없인 못살아!
[Spring Camp 2013] Java Configuration 없인 못살아![Spring Camp 2013] Java Configuration 없인 못살아!
[Spring Camp 2013] Java Configuration 없인 못살아!Arawn Park
 
Vagrant와 chef로 개발서버 구축 자동화하기
Vagrant와 chef로 개발서버 구축 자동화하기Vagrant와 chef로 개발서버 구축 자동화하기
Vagrant와 chef로 개발서버 구축 자동화하기Arawn Park
 

More from Arawn Park (16)

우린 같은 곳을 바라 보고 있나요?
우린 같은 곳을 바라 보고 있나요?우린 같은 곳을 바라 보고 있나요?
우린 같은 곳을 바라 보고 있나요?
 
코틀린 멀티플랫폼, 미지와의 조우
코틀린 멀티플랫폼, 미지와의 조우코틀린 멀티플랫폼, 미지와의 조우
코틀린 멀티플랫폼, 미지와의 조우
 
kotlinx.serialization
kotlinx.serializationkotlinx.serialization
kotlinx.serialization
 
#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기#살아있다 #자프링외길12년차 #코프링2개월생존기
#살아있다 #자프링외길12년차 #코프링2개월생존기
 
우아한 모노리스
우아한 모노리스우아한 모노리스
우아한 모노리스
 
잘 키운 모노리스 하나 열 마이크로서비스 안 부럽다
잘 키운 모노리스 하나 열 마이크로서비스 안 부럽다잘 키운 모노리스 하나 열 마이크로서비스 안 부럽다
잘 키운 모노리스 하나 열 마이크로서비스 안 부럽다
 
점진적인 레거시 웹 애플리케이션 개선 과정
점진적인 레거시 웹 애플리케이션 개선 과정점진적인 레거시 웹 애플리케이션 개선 과정
점진적인 레거시 웹 애플리케이션 개선 과정
 
Introduction to Kotlin
Introduction to KotlinIntroduction to Kotlin
Introduction to Kotlin
 
Reactive Web - Servlet & Async, Non-blocking I/O
Reactive Web - Servlet & Async, Non-blocking I/OReactive Web - Servlet & Async, Non-blocking I/O
Reactive Web - Servlet & Async, Non-blocking I/O
 
Spring framework 4.x
Spring framework 4.xSpring framework 4.x
Spring framework 4.x
 
씹고 뜯고 맛보고 즐기는 스트림 API
씹고 뜯고 맛보고 즐기는 스트림 API씹고 뜯고 맛보고 즐기는 스트림 API
씹고 뜯고 맛보고 즐기는 스트림 API
 
Spring framework 3.2 > 4.0 — themes and trends
Spring framework 3.2 > 4.0 — themes and trendsSpring framework 3.2 > 4.0 — themes and trends
Spring framework 3.2 > 4.0 — themes and trends
 
overview of spring4
overview of spring4overview of spring4
overview of spring4
 
조금 더 좋은 개발자가 된다는 것( 부제: 컨퍼런스의 발표자가 된다는 것 )
조금 더 좋은 개발자가 된다는 것( 부제: 컨퍼런스의 발표자가 된다는 것 )조금 더 좋은 개발자가 된다는 것( 부제: 컨퍼런스의 발표자가 된다는 것 )
조금 더 좋은 개발자가 된다는 것( 부제: 컨퍼런스의 발표자가 된다는 것 )
 
[Spring Camp 2013] Java Configuration 없인 못살아!
[Spring Camp 2013] Java Configuration 없인 못살아![Spring Camp 2013] Java Configuration 없인 못살아!
[Spring Camp 2013] Java Configuration 없인 못살아!
 
Vagrant와 chef로 개발서버 구축 자동화하기
Vagrant와 chef로 개발서버 구축 자동화하기Vagrant와 chef로 개발서버 구축 자동화하기
Vagrant와 chef로 개발서버 구축 자동화하기
 

Resource Handling in Spring MVC

  • 1. Resource Handling in Spring MVC
  • 2. 박용권 SK planet : 봄싹(SpringSprout) : 한국 스프링 사용자 모임(KSUG) : 라 스칼라 코딩단 http://about.me/arawn : twitter / @arawnkr
  • 3. Spring MVC에서 정적 자원(css, js, etc)을 처리해본 경험을 공유합니다.
  • 4. Spring MVC : 리소스 제공(Serving) ✔ ResourceHttpRequestHandler : URL 패턴에 따라 정적 자원 요청을 처리 : org.springframework.core.io.Resource 인터페이스를 사용 servletcontext, classpath, filesystem, etc : HTTP 캐시 설정 기능 제공 expires 또는 cache-control를 사용한 캐시 설정 last-modified 헤더 평가를 통해 304 상태코드 응답 ! ✔ 설정 간소화 기능 제공 : Java 기반 설정시 WebMvcConfigurer.addResourceHandlers(…) 사용 : XML 기반 설정시 mvc:resources 태그 사용
  • 5. 리소스 제공 설정 Java Config public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("리소스에 접근할 URL 패턴") .addResourceLocations("리소스가 있는 위치"); } XML Config <mvc:resources mapping="리소스에 접근할 URL 패턴" location="리소스가 있는 위치"/>
  • 6. 리소스 제공 설정 리소스에 접근할 URL 패턴 정의 public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**") .addResourceLocations("/resources/"); } http://spring.io/resources/css/default.css http://spring.io/resources/js/spring-by-pivotal.png http://spring.io/resources/img/spring-by-pivotal.png
  • 7. 리소스 제공 설정 리소스가 있는 위치 설정 public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**") .addResourceLocations("/resources/"); } http://spring.io/resources/css/default.css . "## WEB-INF $ "## classes $ &## lib &## resources &## css &## default.css
  • 9. Resource 인터페이스에 대한 다양한 구현체 ✔ ServletContextResource ! ! ✔ ClassPathResource ! ! ✔ FileSystemResource ! ! ✔ UrlResource ! ! ✔ etc registry.addResourceHandler("/resources/**") .addResourceLocations("/resources/"); registry.addResourceHandler("/resources/**") .addResourceLocations("classpath:/resources/"); registry.addResourceHandler("/resources/**") .addResourceLocations("file:/resources/“); registry.addResourceHandler("/resources/**") .addResourceLocations("http://spring.io");
  • 10. 리소스를 효율적으로 다루는 캐시 설정 Java Config public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**") .addResourceLocations("/resources/") .setCachePeriod(31556926); } XML Config <mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/>
  • 11. 리소스를 효율적으로 다루는 캐시 설정 Java Config public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**") .addResourceLocations("/resources/") .setCachePeriod(31556926); } Response headers : Cache-Control: "max-age=31556926, must-revalidate" Expires: "Sun, 16 Nov 2014 07:39:20 GMT" Last-Modified: "Thu, 20 Nov 2014 10:49:18 GMT"
  • 12. 리소스를 효율적으로 다루는 캐시 설정 Java Config public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**") .addResourceLocations("/resources/") .setCachePeriod(0); } Response headers : Pragma: "no-cache" Cache-Control: "no-cache, no-store" Expires: "Thu, 01 Jan 1970 00:00:00 GMT”
  • 13. DEMO (캐시 설정에 따른 동작)
  • 14. Multi Module Web Application / Backend) / Server-side) (Frontend (Client-side
  • 15. 프로젝트 구조 "## build.gradle - Gradle : 빌드 자동화 "## gradle.properties "## settings.gradle "## backend - Spring IO Platform 사용해 개발된 Server-side 모듈 $ &## src $ "## main $ $ "## java $ $ &## resources $ &## test &## frontend - 정적 자원(html, css, javascript)을 제공하는 Client-side 모듈 "## package.json "## bower.json "## Gruntfile.js &## src "## assets "## helpers "## includes "## layouts "## libs &## pages
  • 16. backend에 적용된 기술 훑어보기 "## build.gradle - Gradle : 빌드 자동화 "## gradle.properties "## settings.gradle "## backend - Spring IO Platform 사용해 개발된 Server-side 모듈 $ &## src $ "## main $ $ "## java $ $ &## resources $ &## test &## frontend - 정적 자원(html, css, javascript)을 제공하는 Client-side 모듈 "## package.json "## bower.json "## Gruntfile.js &## src "## assets "## helpers "## includes "## layouts "## libs &## pages ✔ Spring Boot : Spring IO Platform 기반 개발을 빠르고 다양한 방법으로 시작 : 애플리케이션 기능외 필요한 공통 컴포넌트를 제공 ✔ Thymeleaf : HTML 태그/속성 기반의 템플릿 엔진 : Spring MVC & Security 와 통합을 위한 라이브러리 제공
  • 17. frontend에 적용된 도구 훑어보기 "## build.gradle - Gradle : 빌드 자동화 "## gradle.properties "## settings.gradle "## backend - Spring IO Platform 사용해 개발된 Server-side 모듈 $ &## src $ "## main $ $ "## java $ $ &## resources $ &## test &## frontend - 정적 자원(html, css, javascript)을 제공하는 Client-side 모듈 "## package.json "## bower.json "## Gruntfile.js &## src "## assets "## helpers "## includes "## layouts "## libs &## pages ✔ NPM(Node Package Manager) : 개발환경 및 의존성 관리 ✔ Bower : JavaScript Lib 의존성 관리(jquery, bootstrap, etc) ✔ Grunt : Client-side 빌드 자동화 : 다양한 Plugin 지원( jshint, usemin, filerev, assemble, etc )
  • 18. frontend에 적용된 기술 훑어보기 (1/3) "## backend &## frontend "## package.json "## Gruntfile.js "## bower.json "## src $ "## assets $ $ "## css $ $ $ "## cover.css $ $ $ &## default.css $ $ &## js $ $ &## default.js $ "## libs $ $ "## bootstrap $ $ &## jquery $ "## includes $ $ "## common-css.hbs $ $ &## common-scripts.hbs $ "## layouts $ $ &## default.hbs $ &## pages $ &## about.hbs &## dist "## assets $ "## css $ $ &## style.min.99501602.css $ &## js $ &## app.min.264ed108.js &## pages &## about.html ✔ CSS/JS 최적화(병합 및 압축) : grunt-usemin : grunt-contrib-concat : grunt-contrib-cssmin : grunt-contrib-uglify ✔ Fingerprinting : grunt-filerev 더 알아보려면 여기로! http://goo.gl/oGVCYT
  • 19. frontend에 적용된 기술 훑어보기 (2/3) "## backend &## frontend "## package.json "## Gruntfile.js "## bower.json "## src $ "## assets $ $ "## css $ $ $ "## cover.css $ $ $ &## default.css $ $ &## js $ $ &## default.js $ "## libs $ $ "## bootstrap $ $ &## jquery $ "## includes $ $ "## common-css.hbs $ $ &## common-scripts.hbs $ "## layouts $ $ &## default.hbs $ &## pages $ &## about.hbs &## dist "## assets $ "## css $ $ &## style.min.99501602.css $ &## js $ &## app.min.264ed108.js &## pages &## about.html ✔ 템플릿(HTML) 생성 : assemble
  • 20. frontend에 적용된 기술 훑어보기 (3/3) 이제 코드로 만나보시죠! "## backend &## frontend "## package.json "## Gruntfile.js "## bower.json "## src $ "## assets $ $ "## css $ $ $ "## cover.css $ $ $ &## default.css $ $ &## js $ $ &## default.js $ "## libs $ $ "## bootstrap $ $ &## jquery $ "## includes $ $ "## common-css.hbs $ $ &## common-scripts.hbs $ "## layouts $ $ &## default.hbs $ &## pages $ &## about.hbs &## dist "## assets $ "## css $ $ &## style.min.99501602.css $ &## js $ &## app.min.264ed108.js &## pages &## about.html
  • 21. frontend가 가출한 이유 "## backend &## frontend "## package.json "## Gruntfile.js "## bower.json "## src $ "## assets $ $ "## css $ $ $ "## cover.css $ $ $ &## default.css $ $ &## js $ $ &## default.js $ "## libs $ $ "## bootstrap $ $ &## jquery $ "## includes $ $ "## common-css.hbs $ $ &## common-scripts.hbs $ "## layouts $ $ &## default.hbs $ &## pages $ &## about.hbs &## dist "## assets $ "## css $ $ &## style.min.99501602.css $ &## js $ &## app.min.264ed108.js &## pages &## about.html ✔ dependency management ✔ modularity ✔ tests ✔ build automation ( vs artifacts)
  • 22. 지금부터 Frontend의 자원(html, css, javascript, image)을 사용하는 방법을 살펴봅니다.
  • 23. Frontend 의존성 다루기 frontend backend
  • 24. 개발자의 친구 복붙- "## backend $ &## src $ "## main $ $ "## java $ $ "## resources $ $ &## webapp $ &## test $ "## java $ &## resources &## frontend &## src $ "## assets $ "## helpers $ "## includes $ "## layouts $ "## libs $ &## pages &## dist "## assets &## pages crtl + v crtl + c
  • 25. Web Libraries in Jars WebJars ✔ Client-side 웹 라이브러리를 JAR로 묶어서 제공하는 서비스 ✔ JVM 기반 빌드 도구(gradle, maven, sbt, etc)를 지원 (maven 저장소) bootstrap-3.3.1.jar &## META-INF &## resources &## webjars &## bootstrap &## 3.3.1 "## css $ "## bootstrap.css $ &## bootstrap.min.css "## js $ "## bootstrap.js $ &## bootstrap.min.js "## fonts "## less &## webjars-requirejs.js <dependencies> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>3.3.1</version> </dependency> </dependencies>
  • 26. DEMO (Servlet 3에서 webjars 사용)
  • 27. Gradle로 통합하기 "## build.gradle "## backend $ &## src $ "## main $ $ "## java $ $ "## resources $ $ &## webapp $ &## test &## frontend &## src $ "## assets $ "## helpers $ "## includes $ "## layouts $ "## libs $ &## pages &## dist "## assets &## pages frontend.jar 로 만든 후 backend 모듈에 의존성을 추가!
  • 28. frontend를 빌드 후 jar로 만들기 "## build.gradle "## backend $ &## src $ "## main $ $ "## java $ $ "## resources $ $ &## webapp $ &## test &## frontend &## src $ "## assets $ "## helpers $ "## includes $ "## layouts $ "## libs $ &## pages &## dist "## assets &## pages project(':frontend') { apply plugin: 'java' ! task npmInstall(type:Exec) { // do something } ! task gruntBuild(type:Exec, dependsOn: [npmInstall]) { // do something } ! jar { from 'dist' includeEmptyDirs = false } jar.dependsOn gruntBuild }
  • 29. backend에 frontend 의존성 추가 "## build.gradle "## backend $ &## src $ "## main $ $ "## java $ $ "## resources $ $ &## webapp $ &## test &## frontend &## src $ "## assets $ "## helpers $ "## includes $ "## layouts $ "## libs $ &## pages &## dist "## assets &## pages project(':frontend') { apply plugin: 'java' ! task npmInstall(type:Exec) { // do something } ! task gruntBuild(type:Exec, dependsOn: [npmInstall]) { // do something } ! jar { from 'dist' includeEmptyDirs = false } jar.dependsOn gruntBuild } ! project(':backend') { apply plugin: 'war' ! dependencies { runtime project(':frontend') } }
  • 30. DEMO (Gradle 통합과 자원 사용)
  • 32. frontend: 배포시에는 최적화된 자원을 사용 > grunt build:release "## backend &## frontend "## src $ "## assets $ $ "## css $ $ "## img $ $ &## js $ "## libs $ $ "## jquery $ $ &## bootstrap $ &## pages &## dist "## assets $ "## css $ $ &## style.min.6bde543a.css $ &## js $ &## app.min.142ca07c.js &## pages &## about.html <!DOCTYPE html> <html lang="ko"> <head> <link href='/assets/css/style.min.6bde543a.css' rel='stylesheet' type='text/css'/> </head> <body> <div class="site-wrapper"> // 생략 </div> <script src='/assets/js/app.min.264ed108.js'></script> </body> </html>
  • 33. frontend: 배포시에는 최적화된 자원을 사용 > grunt build:release "## backend &## frontend "## src $ "## assets $ $ "## css $ $ "## img $ $ &## js $ "## libs $ $ "## jquery $ $ &## bootstrap $ &## pages &## dist "## assets $ "## css $ $ &## style.min.6bde543a.css $ &## js $ &## app.min.142ca07c.js &## pages &## about.html Classpath 위치한 자원을 사용 ! // build.gradle project(':frontend') { sourceSets.main.resources { srcDir 'dist' } } ! // Java Config registry.addResourceHandler("/assets/**") .addResourceLocations("classpath:assets/");
  • 34. frontend: 개발시에는 작성중인 css, js 사용 > grunt build:develop <!DOCTYPE html> <html lang="ko"> <head> <link href="/libs/bootstrap/dist/css/bootstrap.css"/> <link href="/assets/css/default.css"/> </head> <body> <div class="site-wrapper"> // 생략 </div> <script src="/libs/jquery/dist/jquery.js"/> <script src="/libs/bootstrap/dist/js/bootstrap.js"/> <script src="/assets/js/default.js"/> </body> </html> "## backend &## frontend "## src $ "## assets $ $ "## css $ $ $ &## default.css $ $ "## img $ $ &## js $ $ &## default.js $ "## libs $ $ "## jquery $ $ &## bootstrap $ &## pages &## dist &## pages &## about.html
  • 35. backend: 개발시에는 어떻게 자원에 접근하지? > grunt build:develop “어라!? dist/assets이 없네!?” "## backend &## frontend "## src $ "## assets $ $ "## css $ $ $ &## default.css $ $ "## img $ $ &## js $ $ &## default.js $ "## libs $ $ "## jquery $ $ &## bootstrap $ &## pages &## dist &## pages &## about.html
  • 36. backend: 개발시에는 어떻게 자원에 접근하지? > grunt build:develop src를 Classpath에 추가해볼까? ! // build.gradle project(':frontend') { sourceSets.main.resources { srcDirs 'dist', 'src' } } ! // console > gradle build "## backend &## frontend "## src $ "## assets $ $ "## css $ $ $ &## default.css $ $ "## img $ $ &## js $ $ &## default.js $ "## libs $ $ "## jquery $ $ &## bootstrap $ &## pages &## dist &## pages &## about.html
  • 37. backend: 개발시에는 어떻게 자원에 접근하지? 없는게 없는 frontend.jar ! frontend.jar "## assets $ "## css $ $ &## default.css $ "## img $ &## js $ &## default.js "## libs $ "## jquery $ &## bootstrap &## pages > grunt build:develop "## backend &## frontend "## src $ "## assets $ $ "## css $ $ $ &## default.css $ $ "## img $ $ &## js $ $ &## default.js $ "## libs $ $ "## jquery $ $ &## bootstrap $ &## pages &## dist &## pages &## about.html 이건 아니야… ;;;
  • 38. backend: 환경에 따른 자원 접근 전략 변경 "## backend &## frontend "## src $ "## assets $ $ "## css $ $ "## img $ $ &## js $ "## libs $ $ "## jquery $ $ &## bootstrap $ &## pages &## dist "## assets $ "## css $ "## img $ &## js &## templates &## about.html String locations; ! if(개발) { locations = src/assets 사용 } else { locations = dist/assets 사용 } ! registry.addResourceHandler("/assets/**") .addResourceLocations(locations);
  • 39. DEMO (환경에 따른 자원 접근 전략 변경)
  • 41. 더 하고 싶었던 이야기가 있었지만…
  • 42. 묻고 답하는 시간 https://github.com/arawn/resource-handling-in-springmvc
  • 43. 끝.