Resource Handling in Spring MVC

Embed Size (px)

Text of Resource Handling in Spring MVC

  • Resource Handlingin Spring MVC

  • : (SpringSprout): (KSUG) :

    http://about.me/arawn

    SK planet

    : 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

    XML Config

    public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(" URL ") .addResourceLocations(" ");}

  • URL public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**") .addResourceLocations("/resources/");}

    http://spring.io/resources/css/default.csshttp://spring.io/resources/js/spring-by-pivotal.pnghttp://spring.io/resources/img/spring-by-pivotal.png

  • public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**") .addResourceLocations("/resources/");}

    ."## WEB-INF$ "## classes$ # lib# resources # css # default.css

    http://spring.io/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

    XML Config

    public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**") .addResourceLocations("/resources/") .setCachePeriod(31556926);}

  • Java Configpublic 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 Configpublic 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

  • "## 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

    Spring Boot : Spring IO Platform : Thymeleaf : HTML / : Spring MVC & Security

  • "## 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

    frontend

    NPM(Node Package Manager) : Bower : JavaScript Lib (jquery, bootstrap, etc) Grunt : Client-side : Plugin ( jshint, usemin, filerev, assemble, etc )

  • "## 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 (1/3)

    : grunt-usemin : grunt-contrib-concat : grunt-contrib-cssmin : grunt-contrib-uglify

    CSS/JS ( )

    : grunt-filerev

    Fingerprinting

    !http://goo.gl/oGVCYT

  • "## 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 (2/3)

    : assemble

    (HTML)

  • 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

  • "## 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

    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 + c

    crtl + v

  • Web Libraries in Jars

    Client-side JAR JVM (gradle, maven, sbt, etc) (maven )

    WebJars

    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

    org.webjars bootstrap 3.3.1

  • 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(t