Skip to content

Commit 905d8cf

Browse files
committed
Merge branch '2.0.x' of github.com:grails/grails-core into 2.0.x
2 parents c0c0b6a + 1f6f8f7 commit 905d8cf

File tree

7 files changed

+92
-16
lines changed

7 files changed

+92
-16
lines changed

grails-test-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/pages/GroovyPageRenderingTests.groovy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ class GroovyPageRenderingTests extends AbstractGrailsTagTests {
4343
def result = applyTemplate(template, [toplist: [[sublist:['a','b']],[sublist:['c','d']]]])
4444
assertEquals 'abcd', result
4545
}
46+
47+
void testForeachIteratingMap() {
48+
def template='<g:each var="k,v" in="[a:1,b:2,c:3]">${k}=${v},</g:each>'
49+
def result = applyTemplate(template, [:])
50+
assertEquals 'a=1,b=2,c=3,', result
51+
}
4652

4753
void testForeachRenaming() {
4854
def template='<g:each in="${list}"><g:each in="${list}">.</g:each></g:each>'

grails-test-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/taglib/ApplicationTagLibTests.groovy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ class ApplicationTagLibTests extends AbstractGrailsTagTests {
183183
assertOutputEquals('', template, [c:[:]])
184184
assertOutputEquals('foo', template, [c:[a:[b:'foo']]])
185185
}
186+
187+
void testSetTagWithScope() {
188+
def template = '<g:set var="var1" value="1"/>${var1}<g:set var="var1" value="2"/> ${var1}<g:set var="var2" value="3" scope="request"/> ${var2}<g:set var="var2" value="4" scope="request"/> ${var2}'
189+
assertOutputEquals('1 2 3 4', template)
190+
}
191+
186192

187193
void testIteration() {
188194
def template = '''<g:set var="counter" value="${1}" />

grails-web/src/main/groovy/org/codehaus/groovy/grails/web/pages/GroovyPageBinding.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public class GroovyPageBinding extends AbstractGroovyPageBinding {
3737
private Binding parent;
3838
private GroovyPage owner;
3939
private Set<String> cachedParentVariableNames=new HashSet<String>();
40+
private GroovyPageRequestBinding pageRequestBinding;
41+
private boolean pageRequestBindingInitialized=false;
4042
private boolean root;
4143

4244
public GroovyPageBinding() {
@@ -64,6 +66,24 @@ public GroovyPageBinding(String[] args) {
6466
public Object getProperty(String property) {
6567
return getVariable(property);
6668
}
69+
70+
private GroovyPageRequestBinding findPageRequestBinding() {
71+
if(!pageRequestBindingInitialized && parent != null) {
72+
Binding nextParent = parent;
73+
while(nextParent != null && pageRequestBinding==null) {
74+
if(nextParent instanceof GroovyPageRequestBinding) {
75+
pageRequestBinding = (GroovyPageRequestBinding)nextParent;
76+
}
77+
if (nextParent instanceof GroovyPageBinding) {
78+
nextParent = ((GroovyPageBinding)nextParent).parent;
79+
} else {
80+
nextParent = null;
81+
}
82+
}
83+
pageRequestBindingInitialized=true;
84+
}
85+
return pageRequestBinding;
86+
}
6787

6888
@SuppressWarnings("unchecked")
6989
@Override
@@ -76,9 +96,11 @@ public Object getVariable(String name) {
7696
if (parent != null) {
7797
val = parent.getVariable(name);
7898
if (val != null) {
79-
// cache variable in this context since parent context cannot change during usage of this context
80-
getVariablesMap().put(name, val);
81-
cachedParentVariableNames.add(name);
99+
if(findPageRequestBinding()==null || !findPageRequestBinding().isRequestAttributeVariable(name)) {
100+
// cache variable in this context since parent context cannot change during usage of this context
101+
getVariablesMap().put(name, val);
102+
cachedParentVariableNames.add(name);
103+
}
82104
}
83105
}
84106
}

grails-web/src/main/groovy/org/codehaus/groovy/grails/web/pages/GroovyPageRequestBinding.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class GroovyPageRequestBinding extends AbstractGroovyPageBinding {
3838
private GrailsWebRequest webRequest;
3939
private Map<String, Class<?>> cachedDomainsWithoutPackage;
4040
private boolean developmentMode = Environment.isDevelopmentMode();
41+
private Set<String> requestAttributeVariables=new HashSet<String>();
4142

4243
private static Map<String, LazyRequestBasedValue> lazyRequestBasedValuesMap = new HashMap<String, LazyRequestBasedValue>();
4344
static {
@@ -113,28 +114,33 @@ public GroovyPageRequestBinding(GrailsWebRequest webRequest) {
113114
}
114115
}
115116
}
117+
118+
public boolean isRequestAttributeVariable(String name) {
119+
return requestAttributeVariables.contains(name);
120+
}
116121

117122
@Override
118123
public Object getVariable(String name) {
119124
Object val = getVariablesMap().get(name);
120125
if (val == null && !getVariablesMap().containsKey(name) && webRequest != null) {
121126
val = webRequest.getCurrentRequest().getAttribute(name);
122-
123-
if (val == null) {
127+
if(val != null) {
128+
requestAttributeVariables.add(name);
129+
} else {
124130
LazyRequestBasedValue lazyValue = lazyRequestBasedValuesMap.get(name);
125131
if (lazyValue != null) {
126132
val = lazyValue.evaluate(webRequest);
127133
}
128-
}
129-
130-
if (val == null && cachedDomainsWithoutPackage != null) {
131-
val = cachedDomainsWithoutPackage.get(name);
132-
}
133-
134-
// warn about missing variables in development mode
135-
if (val == null && developmentMode) {
136-
if (log.isDebugEnabled()) {
137-
log.debug("Variable '" + name + "' not found in binding or the value is null.");
134+
135+
if (val == null && cachedDomainsWithoutPackage != null) {
136+
val = cachedDomainsWithoutPackage.get(name);
137+
}
138+
139+
// warn about missing variables in development mode
140+
if (val == null && developmentMode) {
141+
if (log.isDebugEnabled()) {
142+
log.debug("Variable '" + name + "' not found in binding or the value is null.");
143+
}
138144
}
139145
}
140146
}

grails-web/src/main/groovy/org/codehaus/groovy/grails/web/sitemesh/GrailsRoutablePrintWriter.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@
1414
*/
1515
package org.codehaus.groovy.grails.web.sitemesh;
1616

17+
import groovy.lang.Writable;
18+
1719
import java.io.IOException;
1820
import java.io.PrintWriter;
1921
import java.io.Writer;
2022

23+
import org.codehaus.groovy.grails.web.util.GrailsPrintWriter;
2124
import org.codehaus.groovy.grails.web.util.GrailsPrintWriterAdapter;
25+
import org.codehaus.groovy.grails.web.util.StreamCharBuffer;
2226

2327
public class GrailsRoutablePrintWriter extends GrailsPrintWriterAdapter {
2428

@@ -65,6 +69,7 @@ private PrintWriter getDestination() {
6569
setError();
6670
}
6771
super.out = destination;
72+
super.setTarget(destination);
6873
}
6974
return destination;
7075
}
@@ -279,4 +284,22 @@ public void blockFlushAndClose() {
279284
this.blockClose = true;
280285
this.blockFlush = true;
281286
}
287+
288+
@Override
289+
public GrailsPrintWriter leftShift(Object value) throws IOException {
290+
getDestination();
291+
return super.leftShift(value);
292+
}
293+
294+
@Override
295+
public GrailsPrintWriter leftShift(StreamCharBuffer otherBuffer) {
296+
getDestination();
297+
return super.leftShift(otherBuffer);
298+
}
299+
300+
@Override
301+
public GrailsPrintWriter leftShift(Writable writable) {
302+
getDestination();
303+
return super.leftShift(writable);
304+
}
282305
}

grails-web/src/main/groovy/org/codehaus/groovy/grails/web/taglib/GroovySyntaxTag.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,13 @@ protected void doEachMethod(String in) {
139139
var = "_it"+ Math.abs(System.identityHashCode(this));
140140
foreachRenamedIt = var;
141141
}
142-
142+
143+
String[] entryVars=null;
144+
if(hasVar && var.indexOf(',') > -1) {
145+
entryVars = var.split("\\s*,\\s*");
146+
var = "_entry" + Math.abs(System.identityHashCode(this));
147+
}
148+
143149
out.print("for( " + var);
144150
out.print(" in "); // dot de-reference
145151
out.print(parser != null ? parser.getExpressionText(in, false) : extractAttributeValue(in)); // object
@@ -148,6 +154,9 @@ protected void doEachMethod(String in) {
148154
out.println();
149155
if (!hasVar) {
150156
out.println("changeItVariable(" + foreachRenamedIt +")" );
157+
} else if (entryVars != null) {
158+
out.println("def " + entryVars[0].trim() + "=" + var + ".getKey()");
159+
out.println("def " + entryVars[1].trim() + "=" + var + ".getValue()");
151160
}
152161
}
153162

grails-web/src/main/groovy/org/codehaus/groovy/grails/web/util/GrailsPrintWriterAdapter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public class GrailsPrintWriterAdapter extends PrintWriter implements GrailsWrapp
3131

3232
public GrailsPrintWriterAdapter(Writer wrapped) {
3333
super(new NullWriter());
34+
setTarget(wrapped);
35+
}
36+
37+
public void setTarget(Writer wrapped) {
3438
if (wrapped instanceof GrailsPrintWriter) {
3539
this.target = ((GrailsPrintWriter)wrapped);
3640
}

0 commit comments

Comments
 (0)