Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch '2.1.x'

  • Loading branch information...
commit d0d96136f94b3a5ddebfba7b3e989eaebb65e468 2 parents f9b368a + 632480e
@graemerocher graemerocher authored
View
15 ...rollers/src/main/groovy/org/codehaus/groovy/grails/web/metaclass/RenderDynamicMethod.java
@@ -49,12 +49,14 @@
import org.codehaus.groovy.grails.web.pages.GSPResponseWriter;
import org.codehaus.groovy.grails.web.pages.GroovyPageTemplate;
import org.codehaus.groovy.grails.web.pages.GroovyPagesTemplateEngine;
+import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes;
import org.codehaus.groovy.grails.web.servlet.HttpHeaders;
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest;
import org.codehaus.groovy.grails.web.servlet.mvc.exceptions.ControllerExecutionException;
import org.codehaus.groovy.grails.web.sitemesh.GrailsLayoutDecoratorMapper;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.springframework.context.ApplicationContext;
+import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.servlet.ModelAndView;
@@ -341,7 +343,7 @@ private boolean renderTemplate(Object target, GroovyObject controller, GrailsWeb
if (argMap.containsKey(ARGUMENT_MODEL)) {
Object modelObject = argMap.get(ARGUMENT_MODEL);
if (modelObject instanceof Map) {
- binding.putAll((Map) modelObject);
+ setTemplateModel(webRequest, binding, (Map) modelObject);
}
}
renderTemplateForBean(t, binding, bean, var, out);
@@ -351,13 +353,16 @@ else if (argMap.containsKey(ARGUMENT_COLLECTION)) {
if (argMap.containsKey(ARGUMENT_MODEL)) {
Object modelObject = argMap.get(ARGUMENT_MODEL);
if (modelObject instanceof Map) {
- binding.putAll((Map) modelObject);
+ setTemplateModel(webRequest, binding, (Map)modelObject);
}
}
renderTemplateForCollection(t, binding, colObject, var, out);
}
else if (argMap.containsKey(ARGUMENT_MODEL)) {
Object modelObject = argMap.get(ARGUMENT_MODEL);
+ if(modelObject instanceof Map) {
+ setTemplateModel(webRequest, binding, (Map)modelObject);
+ }
renderTemplateForModel(t, modelObject, target, out);
}
else {
@@ -375,6 +380,12 @@ else if (argMap.containsKey(ARGUMENT_MODEL)) {
return renderView;
}
+ private void setTemplateModel(GrailsWebRequest webRequest, Map binding, Map modelObject) {
+ Map modelMap = (Map) modelObject;
+ webRequest.setAttribute(GrailsApplicationAttributes.TEMPLATE_MODEL, modelMap, RequestAttributes.SCOPE_REQUEST);
+ binding.putAll(modelMap);
+ }
+
private String getContextPath(GrailsWebRequest webRequest, Map argMap) {
Object cp = argMap.get(ARGUMENT_CONTEXTPATH);
String contextPath = (cp != null ? cp.toString() : "");
View
9 .../main/groovy/org/codehaus/groovy/grails/plugins/web/filters/FilterToHandlerAdapter.groovy
@@ -147,7 +147,14 @@ class FilterToHandlerAdapter implements HandlerInterceptor, InitializingBean, Gr
if (!accept(controllerName, actionName, uri)) return
def callable = filterConfig.after.clone()
- def result = callable.call(modelAndView?.model)
+ def currentModel = modelAndView?.model
+ if(currentModel == null) {
+ final templateModel = request.getAttribute(GrailsApplicationAttributes.TEMPLATE_MODEL)
+ if(templateModel != null) {
+ currentModel = templateModel
+ }
+ }
+ def result = callable.call(currentModel)
if (result instanceof Boolean) {
// if false is returned don't render a view
if (!result) {
View
51 ...ite-web/src/test/groovy/org/codehaus/groovy/grails/web/filters/FiltersUnitTestSpec.groovy
@@ -32,6 +32,38 @@ class FiltersUnitTestSpec extends Specification{
e.message == "bad things happened"
}
+ void "Test view model is passed in after filter"() {
+ when:"A filter is used around a controller action that renders a view and model"
+ withFilters(action: "model1") {
+ controller.model1()
+ }
+
+ then:"The model is correctly passed to the after filter"
+ request.testModel == [foo:'bar']
+ }
+
+
+ void "Test returned model is passed in after filter"() {
+ when:"A filter is used around a controller action that returns a model"
+ withFilters(action: "model2") {
+ controller.model2()
+ }
+
+ then:"The model is correctly passed to the after filter"
+ request.testModel == [foo:'bar']
+ }
+
+
+ void "Test template model is passed in after filter"() {
+ when:"A filter is used around a controller action that returns a model"
+ views['/user/_foo.gsp'] = 'blah'
+ withFilters(action: "model3") {
+ controller.model3()
+ }
+
+ then:"The model is correctly passed to the after filter"
+ request.testModel == [foo:'bar']
+ }
}
@Artefact("Controller")
@@ -39,6 +71,17 @@ class UserController {
def create() {}
def update() {}
+ def model1() {
+ render view:"test", model:[foo:'bar']
+ }
+
+ def model2() {
+ [foo:'bar']
+ }
+
+ def model3() {
+ render template:"foo", model: [foo:'bar']
+ }
}
@Artefact("Filters")
@@ -51,8 +94,9 @@ class AuthenticationFilters {
return false
}
}
+
}
- create(controller: 'user', action: 'update') {
+ update(controller: 'user', action: 'update') {
before = {
if (params.username == '') {
throw new RuntimeException("bad things happened")
@@ -60,5 +104,10 @@ class AuthenticationFilters {
}
}
}
+ model1(controller: 'user', action: 'model*') {
+ after = { model ->
+ request.testModel = model
+ }
+ }
}
}
View
1  ...b/src/main/groovy/org/codehaus/groovy/grails/web/servlet/GrailsApplicationAttributes.java
@@ -47,6 +47,7 @@
String PROPERTY_REGISTRY = "org.codehaus.groovy.grails.PROPERTY_REGISTRY";
String ERRORS = "org.codehaus.groovy.grails.ERRORS";
String MODEL_AND_VIEW = "org.codehaus.groovy.grails.MODEL_AND_VIEW";
+ String TEMPLATE_MODEL = "org.codehaus.groovy.grails.TEMPLATE_MODEL";
String OUT = "org.codehaus.groovy.grails.RESPONSE_OUT";
String TAG_CACHE = "org.codehaus.groovy.grails.TAG_CACHE";
String ID_PARAM = "id";
Please sign in to comment.
Something went wrong with that request. Please try again.