@@ -21,6 +21,7 @@ import grails.core.support.GrailsApplicationAware
2121import grails.plugins.Plugin
2222import grails.util.Environment
2323import grails.util.GrailsUtil
24+ import groovy.transform.CompileStatic
2425import groovy.util.logging.Commons
2526import org.grails.core.artefact.ControllerArtefactHandler
2627import org.grails.plugins.web.servlet.context.BootStrapClassRunner
@@ -38,6 +39,8 @@ import org.springframework.core.Ordered
3839import org.springframework.util.ClassUtils
3940import org.springframework.web.filter.CharacterEncodingFilter
4041import org.springframework.web.multipart.support.StandardServletMultipartResolver
42+ import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry
43+ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
4144import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
4245import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
4346import 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