Skip to content

Commit c96f824

Browse files
committed
Support serving static assets from src/main/resources/public (GRAILS-12096)
1 parent e947133 commit c96f824

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

grails-core/src/main/groovy/grails/config/Settings.groovy

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,14 @@ interface Settings {
137137
* The suffix used during scaffolding for the domain
138138
*/
139139
String SCAFFOLDING_DOMAIN_SUFFIX = 'grails.scaffolding.templates.domainSuffix'
140+
141+
/**
142+
* The amount of time to cache static resource requests
143+
*/
144+
String RESOURCES_CACHE_PERIOD = 'grails.resources.cachePeriod'
145+
146+
/**
147+
* Whether serving static HTML pages from src/main/resources/public is enabled
148+
*/
149+
String RESOURCES_ENABLED = 'grails.resources.enabled'
140150
}

grails-plugin-controllers/src/main/groovy/org/grails/plugins/web/controllers/ControllersGrailsPlugin.groovy

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import grails.core.support.GrailsApplicationAware
2121
import grails.plugins.Plugin
2222
import grails.util.Environment
2323
import grails.util.GrailsUtil
24+
import groovy.transform.CompileStatic
2425
import groovy.util.logging.Commons
2526
import org.grails.core.artefact.ControllerArtefactHandler
2627
import org.grails.plugins.web.servlet.context.BootStrapClassRunner
@@ -38,6 +39,8 @@ import org.springframework.core.Ordered
3839
import org.springframework.util.ClassUtils
3940
import org.springframework.web.filter.CharacterEncodingFilter
4041
import org.springframework.web.multipart.support.StandardServletMultipartResolver
42+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
43+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
4144
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
4245
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
4346
import org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator
@@ -73,6 +76,8 @@ class ControllersGrailsPlugin extends Plugin {
7376
def filtersEncoding = config.getProperty(Settings.FILTER_ENCODING, 'utf-8')
7477
boolean dbConsoleEnabled = config.getProperty(Settings.DBCONSOLE_ENABLED, Boolean, Environment.current == Environment.DEVELOPMENT)
7578

79+
int resourcesCachePeriod = config.getProperty(Settings.RESOURCES_CACHE_PERIOD, Integer, 0)
80+
boolean resourcesEnabled = config.getProperty(Settings.RESOURCES_ENABLED, Boolean, true)
7681

7782
bootStrapClassRunner(BootStrapClassRunner)
7883
tokenResponseActionResultTransformer(TokenResponseActionResultTransformer)
@@ -126,6 +131,9 @@ class ControllersGrailsPlugin extends Plugin {
126131
annotationHandlerMapping(RequestMappingHandlerMapping, interceptorsClosure)
127132
annotationHandlerAdapter(RequestMappingHandlerAdapter)
128133

134+
// add Grails webmvc config
135+
webMvcConfig(GrailsWebMvcConfigurer, resourcesCachePeriod, resourcesEnabled)
136+
129137
// add the dispatcher servlet
130138
dispatcherServlet(GrailsDispatcherServlet)
131139
dispatcherServletRegistration(ServletRegistrationBean, ref("dispatcherServlet"), "/*") {
@@ -191,5 +199,51 @@ class ControllersGrailsPlugin extends Plugin {
191199
}
192200

193201

202+
@CompileStatic
203+
static class GrailsWebMvcConfigurer extends WebMvcConfigurerAdapter {
204+
205+
private static final String[] SERVLET_RESOURCE_LOCATIONS = [ "/" ]
206+
207+
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = [
208+
"classpath:/META-INF/resources/", "classpath:/resources/",
209+
"classpath:/static/", "classpath:/public/" ]
210+
211+
private static final String[] RESOURCE_LOCATIONS
212+
static {
213+
RESOURCE_LOCATIONS = new String[CLASSPATH_RESOURCE_LOCATIONS.length
214+
+ SERVLET_RESOURCE_LOCATIONS.length]
215+
System.arraycopy(SERVLET_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, 0,
216+
SERVLET_RESOURCE_LOCATIONS.length)
217+
System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS,
218+
SERVLET_RESOURCE_LOCATIONS.length, CLASSPATH_RESOURCE_LOCATIONS.length);
219+
}
220+
221+
boolean addMappings = true
222+
Integer cachePeriod
223+
224+
GrailsWebMvcConfigurer(Integer cachePeriod, boolean addMappings = true) {
225+
this.addMappings = addMappings
226+
this.cachePeriod = cachePeriod
227+
}
228+
229+
@Override
230+
@Override
231+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
232+
if (!addMappings) {
233+
return
234+
}
235+
236+
if (!registry.hasMappingForPattern("/webjars/**")) {
237+
registry.addResourceHandler("/webjars/**")
238+
.addResourceLocations("classpath:/META-INF/resources/webjars/")
239+
.setCachePeriod(cachePeriod)
240+
}
241+
if (!registry.hasMappingForPattern("/**")) {
242+
registry.addResourceHandler("/**")
243+
.addResourceLocations(RESOURCE_LOCATIONS)
244+
.setCachePeriod(cachePeriod)
245+
}
246+
}
247+
}
194248

195249
}

0 commit comments

Comments
 (0)