Skip to content

Commit 4989a87

Browse files
committed
fix NPE when applyLayout is used. Fixes #9600
1 parent 3f101c9 commit 4989a87

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

grails-web-sitemesh/src/main/groovy/org/grails/web/sitemesh/GroovyPageLayoutFinder.java

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.grails.web.sitemesh;
1717

1818
import grails.util.Environment;
19+
import grails.util.GrailsNameUtils;
1920
import groovy.lang.GroovyObject;
2021

2122
import java.util.Map;
@@ -27,8 +28,10 @@
2728
import org.apache.commons.logging.LogFactory;
2829
import grails.util.GrailsClassUtils;
2930
import grails.util.GrailsStringUtils;
31+
import org.grails.core.artefact.ControllerArtefactHandler;
3032
import org.grails.io.support.GrailsResourceUtils;
3133
import org.codehaus.groovy.grails.web.metaclass.ControllerDynamicMethods;
34+
import org.grails.web.servlet.mvc.GrailsWebRequest;
3235
import org.grails.web.util.GrailsApplicationAttributes;
3336
import org.grails.web.servlet.view.AbstractGrailsView;
3437
import org.grails.web.servlet.view.GrailsViewResolver;
@@ -110,33 +113,40 @@ public Decorator findLayout(HttpServletRequest request, Page page) {
110113

111114
if (GrailsStringUtils.isBlank(layoutName)) {
112115
GroovyObject controller = (GroovyObject)request.getAttribute(GrailsApplicationAttributes.CONTROLLER);
113-
if (controller != null) {
114-
String controllerName = (String)controller.getProperty(ControllerDynamicMethods.CONTROLLER_NAME_PROPERTY);
115-
String actionUri = (String)controller.getProperty(ControllerDynamicMethods.ACTION_URI_PROPERTY);
116-
117-
if (LOG.isDebugEnabled()) {
118-
LOG.debug("Found controller in request, location layout for controller [" + controllerName
119-
+ "] and action [" + actionUri + "]");
116+
if (controller != null ) {
117+
GrailsWebRequest webRequest = GrailsWebRequest.lookup(request);
118+
String controllerName = webRequest.getControllerName();
119+
if(controllerName == null) {
120+
controllerName = GrailsNameUtils.getLogicalPropertyName(controller.getClass().getName(), ControllerArtefactHandler.TYPE);
120121
}
122+
String actionUri = webRequest.getAttributes().getControllerActionUri(request);
121123

122-
LayoutCacheKey cacheKey = null;
123-
boolean cachedIsNull = false;
124+
if(controllerName != null && actionUri != null) {
124125

125-
if (cacheEnabled) {
126-
cacheKey = new LayoutCacheKey(controllerName, actionUri);
127-
DecoratorCacheValue cacheValue = layoutDecoratorCache.get(cacheKey);
128-
if (cacheValue != null && (!gspReloadEnabled || !cacheValue.isExpired())) {
129-
d = cacheValue.getDecorator();
130-
if (d == null) {
131-
cachedIsNull = true;
132-
}
126+
if (LOG.isDebugEnabled()) {
127+
LOG.debug("Found controller in request, location layout for controller [" + controllerName
128+
+ "] and action [" + actionUri + "]");
133129
}
134-
}
135130

136-
if (d == null && !cachedIsNull) {
137-
d = resolveDecorator(request, controller, controllerName, actionUri);
131+
LayoutCacheKey cacheKey = null;
132+
boolean cachedIsNull = false;
133+
138134
if (cacheEnabled) {
139-
layoutDecoratorCache.put(cacheKey, new DecoratorCacheValue(d));
135+
cacheKey = new LayoutCacheKey(controllerName, actionUri);
136+
DecoratorCacheValue cacheValue = layoutDecoratorCache.get(cacheKey);
137+
if (cacheValue != null && (!gspReloadEnabled || !cacheValue.isExpired())) {
138+
d = cacheValue.getDecorator();
139+
if (d == null) {
140+
cachedIsNull = true;
141+
}
142+
}
143+
}
144+
145+
if (d == null && !cachedIsNull) {
146+
d = resolveDecorator(request, controller, controllerName, actionUri);
147+
if (cacheEnabled) {
148+
layoutDecoratorCache.put(cacheKey, new DecoratorCacheValue(d));
149+
}
140150
}
141151
}
142152
}

0 commit comments

Comments
 (0)