Resource Handling in Spring MVC

  • Published on
    14-Jul-2015

  • View
    1.924

  • Download
    8

Embed Size (px)

Transcript

<ul><li><p>Resource Handlingin Spring MVC</p></li><li><p>: (SpringSprout): (KSUG) : </p><p>http://about.me/arawn</p><p>SK planet</p><p>: twitter / @arawnkr</p></li><li><p>Spring MVC (css, js, etc) .</p></li><li><p>Spring MVC : (Serving)</p><p> ResourceHttpRequestHandler : URL : org.springframework.core.io.Resource servletcontext, classpath, filesystem, etc : HTTP expires cache-control last-modified 304 ! : Java WebMvcConfigurer.addResourceHandlers() : XML mvc:resources </p></li><li><p>Java Config</p><p>XML Config</p><p>public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(" URL ") .addResourceLocations(" ");}</p></li><li><p> URL public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**") .addResourceLocations("/resources/");}</p><p>http://spring.io/resources/css/default.csshttp://spring.io/resources/js/spring-by-pivotal.pnghttp://spring.io/resources/img/spring-by-pivotal.png</p></li><li><p> public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**") .addResourceLocations("/resources/");}</p><p>."## WEB-INF$ "## classes$ # lib# resources # css # default.css</p><p>http://spring.io/resources/css/default.css</p></li><li><p>DEMO( )</p></li><li><p>Resource </p><p> ServletContextResource !! ClassPathResource !! FileSystemResource !! UrlResource !! etc</p><p> registry.addResourceHandler("/resources/**") .addResourceLocations("/resources/");</p><p> registry.addResourceHandler("/resources/**") .addResourceLocations("classpath:/resources/");</p><p> registry.addResourceHandler("/resources/**") .addResourceLocations("file:/resources/);</p><p> registry.addResourceHandler("/resources/**") .addResourceLocations("http://spring.io");</p></li><li><p>Java Config</p><p>XML Config</p><p>public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**") .addResourceLocations("/resources/") .setCachePeriod(31556926);}</p></li><li><p>Java Configpublic void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**") .addResourceLocations("/resources/") .setCachePeriod(31556926);}</p><p>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"</p></li><li><p>Java Configpublic void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**") .addResourceLocations("/resources/") .setCachePeriod(0);}</p><p>Response headers : Pragma: "no-cache" Cache-Control: "no-cache, no-store" Expires: "Thu, 01 Jan 1970 00:00:00 GMT</p></li><li><p>DEMO( )</p></li><li><p>Multi Module Web Application</p><p>/ Backend)/ Server-side)</p><p>(Frontend (Client-side </p></li><li><p>"## 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</p></li><li><p>"## 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</p><p>backend </p><p> Spring Boot : Spring IO Platform : Thymeleaf : HTML / : Spring MVC &amp; Security </p></li><li><p>"## 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</p><p>frontend </p><p> NPM(Node Package Manager) : Bower : JavaScript Lib (jquery, bootstrap, etc) Grunt : Client-side : Plugin ( jshint, usemin, filerev, assemble, etc )</p></li><li><p>"## 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</p><p>frontend (1/3)</p><p>: grunt-usemin : grunt-contrib-concat : grunt-contrib-cssmin : grunt-contrib-uglify</p><p> CSS/JS ( )</p><p>: grunt-filerev</p><p> Fingerprinting</p><p> !http://goo.gl/oGVCYT</p></li><li><p>"## 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</p><p>frontend (2/3)</p><p>: assemble</p><p> (HTML) </p></li><li><p>frontend (3/3)</p><p> !</p><p>"## 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</p></li><li><p>"## 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</p><p>frontend </p><p> dependency management modularity tests build automation ( vs artifacts)</p></li><li><p> Frontend (html, css, javascript, image) .</p></li><li><p>Frontend </p><p>frontend</p><p>backend</p></li><li><p> -</p><p>"## backend$ # src$ "## main$ $ "## java$ $ "## resources$ $ # webapp$ # test$ "## java$ # resources# frontend # src $ "## assets $ "## helpers $ "## includes $ "## layouts $ "## libs $ # pages # dist "## assets # pages</p><p>crtl + c</p><p>crtl + v</p></li><li><p>Web Libraries in Jars</p><p> Client-side JAR JVM (gradle, maven, sbt, etc) (maven ) </p><p>WebJars</p><p>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</p><p> org.webjars bootstrap 3.3.1 </p></li><li><p>DEMO(Servlet 3 webjars )</p></li><li><p>Gradle </p><p>"## build.gradle"## backend$ # src$ "## main$ $ "## java$ $ "## resources$ $ # webapp$ # test# frontend # src $ "## assets $ "## helpers $ "## includes $ "## layouts $ "## libs $ # pages # dist "## assets # pages</p><p> frontend.jar backend !</p></li><li><p> frontend jar </p><p>"## build.gradle"## backend$ # src$ "## main$ $ "## java$ $ "## resources$ $ # webapp$ # test# frontend # src $ "## assets $ "## helpers $ "## includes $ "## layouts $ "## libs $ # pages # dist "## assets # pages</p><p>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}</p></li><li><p>backend frontend </p><p>"## build.gradle"## backend$ # src$ "## main$ $ "## java$ $ "## resources$ $ # webapp$ # test# frontend # src $ "## assets $ "## helpers $ "## includes $ "## layouts $ "## libs $ # pages # dist "## assets # pages</p><p>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') }}</p></li><li><p>DEMO(Gradle )</p></li><li><p>frontend: </p><p>"## 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</p><p>&gt; grunt build:release</p><p> // </p></li><li><p>frontend: </p><p>"## 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</p><p>&gt; grunt build:release</p><p>Classpath !// build.gradleproject(':frontend') { sourceSets.main.resources { srcDir 'dist' }}!// Java Configregistry.addResourceHandler("/assets/**") .addResourceLocations("classpath:assets/");</p></li><li><p>frontend: css, js </p><p>&gt; grunt build:develop</p><p> // </p><p>"## backend# frontend "## src $ "## assets $ $ "## css $ $ $ # default.css $ $ "## img $ $ # js $ $ # default.js $ "## libs $ $ "## jquery $ $ # bootstrap $ # pages # dist # pages # about.html</p></li><li><p>backend: ?</p><p>&gt; grunt build:develop</p><p>!? dist/assets !?</p><p>"## backend# frontend "## src $ "## assets $ $ "## css $ $ $ # default.css $ $ "## img $ $ # js $ $ # default.js $ "## libs $ $ "## jquery $ $ # bootstrap $ # pages # dist # pages # about.html</p></li><li><p>backend: ?</p><p>&gt; grunt build:develop</p><p>src Classpath ? !// build.gradleproject(':frontend') { sourceSets.main.resources { srcDirs 'dist', 'src' }}!// console&gt; gradle build</p><p>"## backend# frontend "## src $ "## assets $ $ "## css $ $ $ # default.css $ $ "## img $ $ # js $ $ # default.js $ "## libs $ $ "## jquery $ $ # bootstrap $ # pages # dist # pages # about.html</p></li><li><p> frontend.jar ! frontend.jar "## assets $ "## css $ $ # default.css$ "## img$ # js$ # default.js"## libs$ "## jquery$ # bootstrap# pages</p><p>backend: ?</p><p>&gt; grunt build:develop"## backend# frontend "## src $ "## assets $ $ "## css $ $ $ # default.css $ $ "## img $ $ # js $ $ # default.js $ "## libs $ $ "## jquery $ $ # bootstrap $ # pages # dist # pages # about.html</p><p> ;;;</p></li><li><p>backend: </p><p>"## backend# frontend "## src $ "## assets $ $ "## css $ $ "## img $ $ # js $ "## libs $ $ "## jquery $ $ # bootstrap $ # pages # dist "## assets $ "## css $ "## img $ # js # templates # about.html</p><p>String locations;!if() { locations = src/assets } else { locations = dist/assets }!registry.addResourceHandler("/assets/**") .addResourceLocations(locations);</p></li><li><p>DEMO( )</p></li><li><p>https://github.com/arawn/resource-handling-in-springmvc</p></li><li><p>.</p></li></ul>