├── .gitignore ├── .travis.yml ├── README.md ├── build.gradle ├── client ├── .bowerrc ├── .gitignore ├── build.gradle ├── gulpfile.js ├── package.json ├── pom.xml └── src │ ├── app │ ├── app.js │ ├── model.js │ └── ui.js │ ├── cache.manifest │ ├── config.js │ ├── css │ └── main.css │ ├── img │ └── spring-logo.png │ └── less │ └── style.less ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── pom.xml ├── server ├── build.gradle ├── pom.xml └── src │ └── main │ ├── java │ └── org │ │ └── springframework │ │ └── samples │ │ └── resources │ │ ├── Application.java │ │ ├── ApplicationProperties.java │ │ ├── WebConfig.java │ │ └── support │ │ ├── GroovyMarkupViewResolverCustomizer.java │ │ └── MustacheViewResolverCustomizer.java │ ├── resources │ ├── application-development.properties │ ├── application-production.properties │ ├── application.properties │ ├── groovy │ │ └── hello.tpl │ ├── logback.xml │ └── mustache │ │ ├── app.html │ │ ├── index.html │ │ └── less.html │ └── webapp │ └── WEB-INF │ └── jsp │ └── hellojsp.jsp └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.sw[op] 2 | .DS_Store 3 | 4 | # IDEA 5 | .idea 6 | *.iml 7 | *.ipr 8 | *.iws 9 | out 10 | 11 | # Gradle 12 | .gradle 13 | build 14 | 15 | # Maven 16 | target 17 | dist 18 | node 19 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Configuration for CI build at https://travis-ci.org/bclozel/spring-resource-handling 2 | 3 | language: java 4 | 5 | jdk: 6 | - oraclejdk8 7 | 8 | sudo: false 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Spring Resource Handling 2 | ======================== 3 | 4 | [![Build Status](https://travis-ci.org/bclozel/spring-resource-handling.svg?branch=master)](https://travis-ci.org/bclozel/spring-resource-handling) 5 | 6 | This application demonstrates new resource handling features in Spring Framework 4.1. 7 | It was originally developed for the talk [Resource Handling in Spring MVC 4.1](https://2014.event.springone2gx.com/schedule/sessions/resource_handling_in_spring_mvc_4_1.html) talk at SpringOne2GX 2014. 8 | 9 | 10 | This projects requires a local install of node+npm (see [nvm](https://github.com/creationix/nvm)). 11 | 12 | The easiest way to get started - from the project root - development version: 13 | 14 | SPRING_PROFILES_ACTIVE=development ./gradlew :server:bootRun 15 | 16 | Or the production version (more optimizations): 17 | 18 | SPRING_PROFILES_ACTIVE=production ./gradlew :server:bootRun 19 | 20 | Then go to: 21 | 22 | * http://localhost:8080/ for an example with JMustache templating 23 | * http://localhost:8080/groovy for an example with Groovy Template Engine 24 | * http://localhost:8080/app for an example with an [HTML5 AppCache Manifest](http://www.html5rocks.com/en/tutorials/appcache/beginner/) 25 | (you can check this in Chrome with chrome://appcache-internals/ ) 26 | * http://localhost:8080/less for an example with a LESS stylesheet; this page uses less files and the LESS JS transpiler 27 | in development mode, and a transpiled version in production 28 | * http://localhost:8080/jsp for a JSP example 29 | * http://localhost:8080/velocity for a Velocity example 30 | 31 | Interesting parts of the application: 32 | 33 | * [configuring resource handlers with resource resolvers and resource transformers](https://github.com/bclozel/spring-resource-handling/blob/master/server/src/main/resources/application-production.properties) 34 | * [a sample template file using JMustache](https://github.com/bclozel/spring-resource-handling/blob/master/server/src/main/resources/mustache/index.html) 35 | and a [custom Mustache lambdas](https://github.com/bclozel/spring-resource-handling/blob/master/server/src/main/java/org/springframework/samples/resources/support/MustacheViewResolverCustomizer.java) to resolve URLs to static resources 36 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | task wrapper(type: Wrapper) { 2 | gradleVersion = '3.2' 3 | } 4 | -------------------------------------------------------------------------------- /client/.bowerrc: -------------------------------------------------------------------------------- 1 | { 2 | "directory": "src/lib" 3 | } -------------------------------------------------------------------------------- /client/.gitignore: -------------------------------------------------------------------------------- 1 | src/lib 2 | node_modules 3 | npm-debug.log 4 | .cram/ 5 | dist/ -------------------------------------------------------------------------------- /client/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id "com.moowork.node" version "0.13" 3 | } 4 | 5 | apply plugin: 'java' 6 | 7 | node { 8 | version = '6.9.1' 9 | download = true 10 | } 11 | 12 | def githubAuthToken = "$System.env.JSPM_GITHUB_AUTH_TOKEN" 13 | 14 | jar { 15 | from 'dist' 16 | eachFile { details -> 17 | details.path = details.path.startsWith('META-INF') ?: 'static/'+details.path 18 | } 19 | // Jar has duplicate empty folders (see http://issues.gradle.org/browse/GRADLE-1830) 20 | // So we need to set includeEmptyDirs to false 21 | includeEmptyDirs = false 22 | } 23 | 24 | // Avoid Github rate limiting on CI instances 25 | // See https://github.com/jspm/jspm-cli/wiki/Registries#travis-ci 26 | task jspm_config(type: NpmTask, dependsOn: npm_install) { 27 | args = ['run', 'jspm-config', '--', "$githubAuthToken"] 28 | } 29 | 30 | task jspm_install(type: NpmTask, dependsOn: npm_install) { 31 | args = ['run', 'jspm-install'] 32 | } 33 | 34 | task npm_build(type: NpmTask, dependsOn: jspm_install) { 35 | args = ['run', 'build'] 36 | } 37 | 38 | if (githubAuthToken != "null") { 39 | jspm_install.setDependsOn([jspm_config]) 40 | } 41 | 42 | jar.dependsOn npm_build -------------------------------------------------------------------------------- /client/gulpfile.js: -------------------------------------------------------------------------------- 1 | 2 | var path = require('path'), 3 | cssMinify = require('gulp-minify-css'), 4 | less = require('gulp-less'), 5 | gulp = require('gulp'), 6 | Builder = require('systemjs-builder'); 7 | 8 | var paths = { 9 | baseUrl: 'file:' + process.cwd() + '/src/', 10 | config: ['src/config.js'], 11 | jspmLibs: ['src/lib/**', '!src/lib/*/test/*'], 12 | css: { 13 | files: ['src/css/*.css'] 14 | }, 15 | less: ['src/less/*'], 16 | assets: ["src/cache.manifest"], 17 | images: ["src/img/*"], 18 | destination: './dist' 19 | }; 20 | 21 | // Optimize application CSS files and copy to "dist" folder 22 | gulp.task('optimize-and-copy-css', function() { 23 | return gulp.src(paths.css.files) 24 | .pipe(cssMinify()) 25 | .pipe(gulp.dest(paths.destination + '/css')); 26 | }); 27 | 28 | // Optimize application JavaScript files and copy to "dist" folder 29 | gulp.task('optimize-and-copy-js', function(cb) { 30 | var builder = new Builder(); 31 | builder.loadConfig('./src/config.js') 32 | .then(function() { 33 | builder.config({ baseURL: paths.baseUrl }); 34 | builder.build('app/app', paths.destination + '/app/app.js', { minify: true, sourceMaps: true }); 35 | cb(); 36 | }) 37 | .catch(function(err) { 38 | cb(err); 39 | }); 40 | }); 41 | 42 | // Copy jspm-managed JavaScript dependencies to "dist" folder 43 | gulp.task('copy-lib', function() { 44 | return gulp.src(paths.jspmLibs) 45 | .pipe(gulp.dest(paths.destination + '/lib')); 46 | }); 47 | 48 | gulp.task('copy-config', function() { 49 | return gulp.src(paths.config) 50 | .pipe(gulp.dest(paths.destination)); 51 | }); 52 | 53 | gulp.task('copy-images', function() { 54 | return gulp.src(paths.images) 55 | .pipe(gulp.dest(paths.destination + '/img')); 56 | }); 57 | 58 | gulp.task('copy-assets', function() { 59 | return gulp.src(paths.assets) 60 | .pipe(gulp.dest(paths.destination)) 61 | }); 62 | 63 | gulp.task('less', function () { 64 | return gulp.src(paths.less) 65 | .pipe(less()) 66 | .pipe(cssMinify({noRebase: true})) 67 | .pipe(gulp.dest(paths.destination + '/css')); 68 | }); 69 | 70 | gulp.task('build', ['optimize-and-copy-css', 'optimize-and-copy-js', 'copy-lib', 71 | 'copy-config', 'copy-images', 'less', 'copy-assets'], function(){}); -------------------------------------------------------------------------------- /client/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "spring-resource-handling", 3 | "version": "0.0.1", 4 | "description": "Spring Resources Handling sample", 5 | "dependencies": { 6 | "gulp": "^3.8.11", 7 | "gulp-less": "^3.0.3", 8 | "gulp-minify-css": "^1.1.1", 9 | "gulp-util": "^3.0.4", 10 | "jspm": "^0.15.6", 11 | "systemjs-builder": "^0.10.6" 12 | }, 13 | "scripts": { 14 | "build": "gulp build", 15 | "jspm-install": "jspm install", 16 | "jspm-config": "jspm config registries.github.auth" 17 | }, 18 | "engines": { 19 | "node": ">=0.10.0" 20 | }, 21 | "jspm": { 22 | "directories": { 23 | "baseURL": "src", 24 | "packages": "src/lib" 25 | }, 26 | "dependencies": { 27 | "bootstrap": "github:twbs/bootstrap@^3.3.4", 28 | "jquery": "github:components/jquery@^2.1.3", 29 | "less": "github:aaike/jspm-less-plugin@^0.0.5" 30 | }, 31 | "devDependencies": { 32 | "traceur": "github:jmcriffey/bower-traceur@0.0.88", 33 | "traceur-runtime": "github:jmcriffey/bower-traceur-runtime@0.0.88" 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /client/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.samples 7 | spring-resource-handling-client 8 | 0.1.0.BUILD-SNAPSHOT 9 | jar 10 | 11 | Spring Resource Handling Client module 12 | 13 | 14 | org.springframework.samples 15 | spring-resource-handling 16 | 0.1.0.BUILD-SNAPSHOT 17 | ../ 18 | 19 | 20 | 21 | 22 | 23 | com.github.eirslett 24 | frontend-maven-plugin 25 | 1.2 26 | 27 | 28 | install node and npm 29 | install-node-and-npm 30 | 31 | v6.9.1 32 | 3.10.8 33 | 34 | 35 | 36 | npm install 37 | npm 38 | 39 | 40 | jspm-install 41 | npm 42 | 43 | run jspm-install 44 | 45 | 46 | 47 | npm build 48 | npm 49 | 50 | run build 51 | 52 | 53 | 54 | 55 | 56 | maven-resources-plugin 57 | 3.0.1 58 | 59 | 60 | copy-resources 61 | prepare-package 62 | 63 | copy-resources 64 | 65 | 66 | ${project.build.directory}/classes/static 67 | 68 | 69 | ${basedir}/dist 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /client/src/app/app.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var ui = require('./ui'); 4 | var $ = require('jquery'); 5 | 6 | ui.update(); -------------------------------------------------------------------------------- /client/src/app/model.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | define([], function() { 4 | 5 | return { 6 | getMessage: function() { 7 | return "Hello world!"; 8 | } 9 | } 10 | }); -------------------------------------------------------------------------------- /client/src/app/ui.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | define(["jquery", "app/model"], function($, ui) { 4 | 5 | return { 6 | update: function() { 7 | $("#greeting").html(ui.getMessage()); 8 | } 9 | } 10 | }); -------------------------------------------------------------------------------- /client/src/cache.manifest: -------------------------------------------------------------------------------- 1 | CACHE MANIFEST 2 | 3 | # this is a comment 4 | CACHE: 5 | lib/curl/src/curl.js 6 | run.js 7 | 8 | NETWORK: 9 | * 10 | 11 | CACHE: 12 | css/main.css 13 | img/spring-logo.png 14 | https://fonts.googleapis.com/css?family=Varela+Round|Montserrat:400,700 -------------------------------------------------------------------------------- /client/src/config.js: -------------------------------------------------------------------------------- 1 | System.config({ 2 | "baseURL": "/", 3 | "transpiler": "traceur", 4 | "paths": { 5 | "*": "*.js", 6 | "github:*": "lib/github/*.js", 7 | "npm:*": "lib/npm/*.js" 8 | } 9 | }); 10 | 11 | System.config({ 12 | "map": { 13 | "bootstrap": "github:twbs/bootstrap@3.3.4", 14 | "jquery": "github:components/jquery@2.1.3", 15 | "less": "github:aaike/jspm-less-plugin@0.0.5", 16 | "traceur": "github:jmcriffey/bower-traceur@0.0.88", 17 | "traceur-runtime": "github:jmcriffey/bower-traceur-runtime@0.0.88", 18 | "github:aaike/jspm-less-plugin@0.0.5": { 19 | "less.js": "github:distros/less@2.4.0" 20 | }, 21 | "github:twbs/bootstrap@3.3.4": { 22 | "jquery": "github:components/jquery@2.1.3" 23 | } 24 | } 25 | }); 26 | 27 | -------------------------------------------------------------------------------- /client/src/css/main.css: -------------------------------------------------------------------------------- 1 | @import "../lib/github/twbs/bootstrap@3.3.4/css/bootstrap.css"; 2 | #logo { 3 | width: 100%; 4 | height: 200px; 5 | background: url('/img/spring-logo.png') no-repeat 50% 50%; 6 | background-size: 450px; 7 | color:white; 8 | } -------------------------------------------------------------------------------- /client/src/img/spring-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bclozel/spring-resource-handling/686e7bb72f8db0164e72901fcf6050cdc5216286/client/src/img/spring-logo.png -------------------------------------------------------------------------------- /client/src/less/style.less: -------------------------------------------------------------------------------- 1 | @import "../lib/github/twbs/bootstrap@3.3.4/css/bootstrap.css"; 2 | 3 | @images: "../img"; 4 | 5 | #logo { 6 | width: 100%; 7 | height: 200px; 8 | background: url("@{images}/spring-logo.png") no-repeat 50% 50%; 9 | background-size: 450px; 10 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | version=0.1-SNAPSHOT 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bclozel/spring-resource-handling/686e7bb72f8db0164e72901fcf6050cdc5216286/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Nov 16 14:51:01 CET 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.2-bin.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn ( ) { 37 | echo "$*" 38 | } 39 | 40 | die ( ) { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | for s in "${@}" ; do 159 | s=\"$s\" 160 | APP_ARGS=$APP_ARGS" "$s 161 | done 162 | 163 | # Collect all arguments for the java command, following the shell quoting and substitution rules 164 | eval set -- "$DEFAULT_JVM_OPTS" "$JAVA_OPTS" "$GRADLE_OPTS" "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 165 | 166 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 167 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 168 | cd "$(dirname "$0")" 169 | fi 170 | 171 | exec "$JAVACMD" "$@" 172 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.samples 7 | spring-resource-handling 8 | 0.1.0.BUILD-SNAPSHOT 9 | pom 10 | 11 | Spring Resource Handling 12 | 13 | 14 | org.springframework.boot 15 | spring-boot-starter-parent 16 | 1.4.2.RELEASE 17 | 18 | 19 | 20 | client 21 | server 22 | 23 | 24 | 25 | UTF-8 26 | 1.8 27 | 28 | 29 | 30 | 31 | spring-snapshots 32 | spring-snapshots 33 | 34 | http://repo.spring.io/libs-snapshot 35 | 36 | 37 | 38 | 39 | spring-snapshots 40 | spring-snapshots 41 | 42 | http://repo.spring.io/libs-snapshot 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /server/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | springBootVersion = '1.4.2.RELEASE' 4 | } 5 | repositories { 6 | mavenCentral() 7 | maven { url 'http://repo.spring.io/libs-snapshot' } 8 | maven { url 'https://repo.spring.io/plugins-release' } 9 | } 10 | dependencies { 11 | classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 12 | classpath("io.spring.gradle:dependency-management-plugin:0.6.1.RELEASE") 13 | classpath("org.springframework.build.gradle:propdeps-plugin:0.0.7") 14 | } 15 | } 16 | 17 | apply plugin: 'java' 18 | apply plugin: 'org.springframework.boot' 19 | apply plugin: 'io.spring.dependency-management' 20 | apply plugin: "propdeps" 21 | 22 | repositories { 23 | mavenCentral() 24 | maven { 25 | url 'http://repo.spring.io/libs-snapshot' 26 | 27 | } 28 | } 29 | 30 | ext { 31 | javaVersion = '1.8'; 32 | } 33 | 34 | sourceCompatibility = javaVersion 35 | targetCompatibility = javaVersion 36 | 37 | jar { 38 | baseName = 'spring-resource-handling' 39 | version = '0.1.0.BUILD-SNAPSHOT' 40 | } 41 | 42 | // war build 43 | /* 44 | apply plugin: 'war' 45 | war { 46 | baseName = 'spring-resource-handling' 47 | version = '0.1.0.BUILD-SNAPSHOT' 48 | } 49 | */ 50 | 51 | configurations { 52 | providedRuntime 53 | } 54 | 55 | dependencies { 56 | compile project(':client') 57 | compile("org.springframework.boot:spring-boot-starter-web") 58 | compile("org.springframework.boot:spring-boot-starter-mustache") 59 | optional("org.springframework.boot:spring-boot-configuration-processor") 60 | optional("org.springframework.boot:spring-boot-devtools") 61 | 62 | compile("org.apache.tomcat.embed:tomcat-embed-jasper") 63 | compile("javax.servlet:jstl") 64 | compile("org.codehaus.groovy:groovy-all") 65 | 66 | testCompile("org.springframework.boot:spring-boot-starter-test") 67 | testCompile("org.testng:testng") 68 | } 69 | 70 | compileJava.dependsOn(processResources) -------------------------------------------------------------------------------- /server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.samples 7 | spring-resource-handling-server 8 | 0.1.0.BUILD-SNAPSHOT 9 | war 10 | 11 | Spring Resource Handling Server Module 12 | 13 | 14 | org.springframework.samples 15 | spring-resource-handling 16 | 0.1.0.BUILD-SNAPSHOT 17 | ../ 18 | 19 | 20 | 21 | org.springframework.samples.resources.Application 22 | 23 | 24 | 25 | 26 | org.springframework.samples 27 | spring-resource-handling-client 28 | 0.1.0.BUILD-SNAPSHOT 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter-web 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-mustache 37 | 38 | 39 | org.springframework.boot 40 | spring-boot-configuration-processor 41 | true 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-devtools 46 | true 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-starter-tomcat 51 | provided 52 | 53 | 54 | org.codehaus.groovy 55 | groovy-all 56 | 57 | 58 | javax.servlet 59 | javax.servlet-api 60 | 3.0.1 61 | provided 62 | 63 | 64 | javax.servlet 65 | jstl 66 | 67 | 68 | org.apache.tomcat.embed 69 | tomcat-embed-jasper 70 | provided 71 | 72 | 73 | 74 | 75 | 76 | 77 | org.springframework.boot 78 | spring-boot-maven-plugin 79 | 80 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /server/src/main/java/org/springframework/samples/resources/Application.java: -------------------------------------------------------------------------------- 1 | package org.springframework.samples.resources; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.boot.context.properties.EnableConfigurationProperties; 6 | 7 | @SpringBootApplication 8 | @EnableConfigurationProperties(ApplicationProperties.class) 9 | public class Application { // extends SpringBootServletInitializer 10 | 11 | 12 | public static void main(String[] args) { 13 | SpringApplication.run(Application.class, args); 14 | } 15 | 16 | /* 17 | @Override 18 | protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 19 | return application.sources(Application.class); 20 | } 21 | */ 22 | } -------------------------------------------------------------------------------- /server/src/main/java/org/springframework/samples/resources/ApplicationProperties.java: -------------------------------------------------------------------------------- 1 | package org.springframework.samples.resources; 2 | 3 | 4 | import org.springframework.boot.context.properties.ConfigurationProperties; 5 | 6 | @ConfigurationProperties("application") 7 | public class ApplicationProperties { 8 | 9 | private String version; 10 | 11 | public String getVersion() { 12 | return version; 13 | } 14 | 15 | public void setVersion(String version) { 16 | this.version = version; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /server/src/main/java/org/springframework/samples/resources/WebConfig.java: -------------------------------------------------------------------------------- 1 | package org.springframework.samples.resources; 2 | 3 | import org.springframework.context.annotation.Configuration; 4 | import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; 5 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 6 | 7 | @Configuration 8 | public class WebConfig extends WebMvcConfigurerAdapter { 9 | 10 | @Override 11 | public void addViewControllers(ViewControllerRegistry registry) { 12 | registry.addViewController("/").setViewName("index"); 13 | registry.addViewController("/groovy").setViewName("hello"); 14 | registry.addViewController("/app").setViewName("app"); 15 | registry.addViewController("/less").setViewName("less"); 16 | registry.addViewController("/jsp").setViewName("hellojsp"); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /server/src/main/java/org/springframework/samples/resources/support/GroovyMarkupViewResolverCustomizer.java: -------------------------------------------------------------------------------- 1 | package org.springframework.samples.resources.support; 2 | 3 | 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | import java.util.function.Function; 7 | 8 | import javax.annotation.PostConstruct; 9 | 10 | import org.springframework.beans.factory.annotation.Autowired; 11 | import org.springframework.samples.resources.ApplicationProperties; 12 | import org.springframework.stereotype.Component; 13 | import org.springframework.web.servlet.resource.ResourceUrlProvider; 14 | import org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver; 15 | 16 | @Component 17 | public class GroovyMarkupViewResolverCustomizer { 18 | 19 | private final GroovyMarkupViewResolver groovyMarkupViewResolver; 20 | 21 | private final ResourceUrlProvider urlProvider; 22 | 23 | private final ApplicationProperties applicationProperties; 24 | 25 | @Autowired 26 | public GroovyMarkupViewResolverCustomizer(GroovyMarkupViewResolver groovyMarkupViewResolver, 27 | ResourceUrlProvider urlProvider, ApplicationProperties applicationProperties) { 28 | this.groovyMarkupViewResolver = groovyMarkupViewResolver; 29 | this.urlProvider = urlProvider; 30 | this.applicationProperties = applicationProperties; 31 | } 32 | 33 | @PostConstruct 34 | public void customizeViewResolver() { 35 | 36 | Map groovyTemplateHelpers = new HashMap<>(); 37 | groovyTemplateHelpers.put("linkTo", s -> this.urlProvider.getForLookupPath((String) s)); 38 | groovyTemplateHelpers.put("appVersion", s -> applicationProperties.getVersion()); 39 | this.groovyMarkupViewResolver.setAttributesMap(groovyTemplateHelpers); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /server/src/main/java/org/springframework/samples/resources/support/MustacheViewResolverCustomizer.java: -------------------------------------------------------------------------------- 1 | package org.springframework.samples.resources.support; 2 | 3 | import java.io.IOException; 4 | import java.io.Writer; 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | import javax.annotation.PostConstruct; 9 | 10 | import com.samskivert.mustache.Mustache; 11 | import com.samskivert.mustache.Template; 12 | 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.boot.autoconfigure.mustache.web.MustacheViewResolver; 15 | import org.springframework.core.env.Environment; 16 | import org.springframework.samples.resources.ApplicationProperties; 17 | import org.springframework.stereotype.Component; 18 | import org.springframework.util.StringUtils; 19 | import org.springframework.web.servlet.resource.ResourceUrlProvider; 20 | 21 | @Component 22 | public class MustacheViewResolverCustomizer { 23 | 24 | private final MustacheViewResolver mustacheViewResolver; 25 | 26 | private final ResourceUrlProvider urlProvider; 27 | 28 | private final ApplicationProperties applicationProperties; 29 | 30 | private final Environment environment; 31 | 32 | @Autowired 33 | public MustacheViewResolverCustomizer(MustacheViewResolver mustacheViewResolver, 34 | ResourceUrlProvider urlProvider, ApplicationProperties applicationProperties, 35 | Environment environment) { 36 | this.mustacheViewResolver = mustacheViewResolver; 37 | this.urlProvider = urlProvider; 38 | this.applicationProperties = applicationProperties; 39 | this.environment = environment; 40 | } 41 | 42 | @PostConstruct 43 | public void customizeViewResolver() { 44 | boolean isDevMode = this.environment.acceptsProfiles("development"); 45 | Map attributesMap = new HashMap<>(); 46 | attributesMap.put("src", (Mustache.Lambda) (frag, out) -> { 47 | String url = frag.execute(); 48 | String resourceUrl = urlProvider.getForLookupPath(frag.execute()); 49 | if(StringUtils.hasLength(resourceUrl)) { 50 | out.write(resourceUrl); 51 | } 52 | else { 53 | out.write(url); 54 | } 55 | }); 56 | attributesMap.put("isDevMode", new Mustache.InvertibleLambda() { 57 | 58 | @Override 59 | public void execute(Template.Fragment frag, Writer out) throws IOException { 60 | if (isDevMode) { 61 | out.write(frag.execute()); 62 | } 63 | } 64 | 65 | @Override 66 | public void executeInverse(Template.Fragment frag, Writer out) throws IOException { 67 | if (!isDevMode) { 68 | out.write(frag.execute()); 69 | } 70 | } 71 | 72 | }); 73 | attributesMap.put("applicationVersion", this.applicationProperties.getVersion()); 74 | this.mustacheViewResolver.setAttributesMap(attributesMap); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /server/src/main/resources/application-development.properties: -------------------------------------------------------------------------------- 1 | spring.resources.static-locations=classpath:/static/,file:../client/src/ 2 | spring.devtools.restart.additional-paths=../client/src/ 3 | spring.devtools.restart.additional-exclude=**/*.js,**/*.css 4 | 5 | application.version=de4db33f 6 | spring.resources.chain.cache=true 7 | spring.resources.cache-period=60 8 | spring.resources.chain.strategy.fixed.enabled=true 9 | spring.resources.chain.strategy.fixed.paths=/** 10 | spring.resources.chain.strategy.fixed.version=${application.version} 11 | spring.resources.chain.strategy.content.enabled=true -------------------------------------------------------------------------------- /server/src/main/resources/application-production.properties: -------------------------------------------------------------------------------- 1 | application.version=de4db33f 2 | spring.resources.chain.cache=true 3 | spring.resources.cache-period=60 4 | spring.resources.chain.strategy.fixed.enabled=true 5 | spring.resources.chain.strategy.fixed.paths=/**/*.js,/**/*.map 6 | spring.resources.chain.strategy.fixed.version=${application.version} 7 | spring.resources.chain.strategy.content.enabled=true 8 | spring.resources.chain.html-application-cache=true -------------------------------------------------------------------------------- /server/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.resources.chain.enabled=true 2 | # JSP 3 | spring.mvc.view.prefix=/WEB-INF/jsp/ 4 | spring.mvc.view.suffix=.jsp 5 | # Groovy 6 | spring.groovy.template.resource-loader-path=classpath:/groovy/ 7 | # Mustache 8 | spring.mustache.prefix=classpath:/mustache/ 9 | # Velocity 10 | spring.velocity.enabled=true 11 | spring.velocity.resource-loader-path=classpath:/velocity/ 12 | -------------------------------------------------------------------------------- /server/src/main/resources/groovy/hello.tpl: -------------------------------------------------------------------------------- 1 | yieldUnescaped '' 2 | html { 3 | head { 4 | meta(charset:"utf-8") 5 | meta('http-equiv':"X-UA-Compatible", content:"IE=edge") 6 | meta(name:"viewport", content:"width=device-width") 7 | title('Spring resource handling') 8 | link(href: linkTo.apply('/css/main.css'), type: 'text/css', rel: 'stylesheet') 9 | script(src: linkTo.apply('/lib/system.js'), "") 10 | script(src: linkTo.apply('/config.js'), "") 11 | script(""" 12 | System.config({baseURL: "/${appVersion.apply()}"}); 13 | System.import('app/app'); 14 | """) 15 | } 16 | body { 17 | div(class:'container') { 18 | div(class:'jumbotron') { 19 | h1(id:"greeting", "{insert greeting here}") 20 | } 21 | div(id:"logo", "") 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /server/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | %d{HH:mm:ss} [%thread] %-5level %logger{26} - %msg%n%rEx 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /server/src/main/resources/mustache/app.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Spring resource handling 7 | 8 | 9 | 10 | 11 | 12 | 13 | 17 | 18 | 19 | 20 |
21 |
22 |

{insert greeting here}

23 |
24 |

Example of HTML5 AppCache manifest

25 | 26 |
27 | 28 | -------------------------------------------------------------------------------- /server/src/main/resources/mustache/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Spring resource handling 7 | 8 | 9 | 10 | 11 | 12 | 13 | 17 | 18 | 19 |
20 |
21 |

{insert greeting here}

22 |
23 | 24 |
25 | 26 | -------------------------------------------------------------------------------- /server/src/main/resources/mustache/less.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Spring resource handling 7 | 8 | 9 | 10 | 11 | 12 | {{#isDevMode}} 13 | 14 | 17 | {{/isDevMode}} 18 | {{^isDevMode}} 19 | 20 | {{/isDevMode}} 21 | 25 | 26 | 27 | 28 |
29 |
30 |

{insert greeting here}

31 |
32 | {{#isDevMode}} 33 |

In dev mode a LESS stylesheet is used (see HTML source of this page)

34 | {{/isDevMode}} 35 | {{^isDevMode}} 36 |

In production mode a transpiled version of that stylesheet is used (see HTML source of this page)

37 | {{/isDevMode}} 38 | 39 |
40 | 41 | -------------------------------------------------------------------------------- /server/src/main/webapp/WEB-INF/jsp/hellojsp.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 | <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> 4 | <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 5 | 6 | 7 | 8 | 9 | 10 | Spring resource handling 11 | 12 | 13 | 14 | "> 15 | 16 | 17 | 20 | 21 | 22 | 23 | 24 | 25 |
26 |
27 |

{insert greeting here}

28 |
29 |

Spring URL: ${springUrl}

30 |

JSTL URL: ${jstlUrl}

31 | 32 |
33 | 34 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include 'client', 'server' 2 | --------------------------------------------------------------------------------