Skip to content

Commit 88df6de

Browse files
author
graeme
committed
fix for GRAILS-1061 and GRAILS-1060
git-svn-id: https://svn.codehaus.org/grails/trunk@3960 1cfb16fd-6d17-0410-8ff1-b7e8e1e2867d
1 parent 474060e commit 88df6de

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

src/groovy/org/codehaus/groovy/grails/plugins/web/mapping/UrlMappingsGrailsPlugin.groovy

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ class UrlMappingsGrailsPlugin {
3131
def version = GrailsUtil.getGrailsVersion()
3232
def dependsOn = [core:version]
3333

34-
34+
35+
def doWithSpring = {
36+
grailsUrlMappingsHolder(UrlMappingsHolderFactoryBean) {
37+
grailsApplication = ref("grailsApplication", true)
38+
}
39+
}
40+
3541
def doWithApplicationContext = { ctx ->
3642
def beans = beans {
3743
grailsUrlMappingsHolder(UrlMappingsHolderFactoryBean) {

src/web/org/codehaus/groovy/grails/web/mapping/DefaultUrlMappingsHolder.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ public class DefaultUrlMappingsHolder implements UrlMappingsHolder {
3636
private List urlMappings = new ArrayList();
3737
private UrlMapping[] mappings;
3838
private Map mappingsLookup = new HashMap();
39+
private UrlMappingKey DEFAULT_CONTROLLER_KEY = new UrlMappingKey(null, null, new HashSet() {{
40+
add(UrlMapping.CONTROLLER);
41+
add(UrlMapping.ACTION);
42+
}});
43+
private UrlMappingKey DEFAULT_ACTION_KEY = new UrlMappingKey(null, null, new HashSet() {{
44+
add(UrlMapping.ACTION);
45+
}});
3946

4047

4148
public DefaultUrlMappingsHolder(List mappings) {
@@ -92,18 +99,9 @@ public UrlMapping getReverseMapping(final String controller, final String action
9299
UrlMappingKey key = new UrlMappingKey(controller, action, params.keySet());
93100
UrlMapping mapping = (UrlMapping)mappingsLookup.get(key);
94101
if(mapping == null) {
95-
Map lookup = new HashMap() {{
96-
put(UrlMapping.CONTROLLER,controller);
97-
put(UrlMapping.ACTION,action);
98-
}};
99-
100-
key = new UrlMappingKey(null,null,lookup.keySet());
101-
mapping = (UrlMapping)mappingsLookup.get(key);
102-
102+
mapping = (UrlMapping)mappingsLookup.get(DEFAULT_CONTROLLER_KEY);
103103
if(mapping == null) {
104-
lookup.remove(UrlMapping.ACTION);
105-
key = new UrlMappingKey(null,null,lookup.keySet());
106-
return (UrlMapping)mappingsLookup.get(key);
104+
return (UrlMapping)mappingsLookup.get(DEFAULT_ACTION_KEY);
107105
}
108106
}
109107
return mapping;

src/web/org/codehaus/groovy/grails/web/mapping/RegexUrlMapping.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
package org.codehaus.groovy.grails.web.mapping;
1616

1717
import groovy.lang.Closure;
18-
18+
import org.apache.commons.logging.Log;
19+
import org.apache.commons.logging.LogFactory;
1920
import org.codehaus.groovy.grails.commons.GrailsControllerClass;
2021
import org.codehaus.groovy.grails.validation.ConstrainedProperty;
2122
import org.codehaus.groovy.grails.web.mapping.exceptions.UrlMappingException;
@@ -24,6 +25,7 @@
2425
import org.springframework.validation.MapBindingResult;
2526
import org.springframework.web.context.request.RequestContextHolder;
2627

28+
import java.util.Collections;
2729
import java.util.HashMap;
2830
import java.util.Map;
2931
import java.util.regex.Matcher;
@@ -63,6 +65,7 @@ public class RegexUrlMapping implements UrlMapping {
6365
private static final String WILDCARD = "*";
6466
private static final String CAPTURED_WILDCARD = "(*)";
6567
private static final String SLASH = "/";
68+
private static final Log LOG = LogFactory.getLog(RegexUrlMapping.class);
6669

6770

6871
public ConstrainedProperty[] getConstraints() {
@@ -166,7 +169,8 @@ public UrlMappingInfo match(String uri) {
166169
* @see org.codehaus.groovy.grails.web.mapping.UrlMapping
167170
*/
168171
public String createURL(Map parameterValues) {
169-
StringBuffer uri = new StringBuffer();
172+
if(parameterValues==null)parameterValues=Collections.EMPTY_MAP;
173+
StringBuffer uri = new StringBuffer();
170174

171175
String[] tokens = urlData.getTokens();
172176
int paramIndex = 0;
@@ -187,6 +191,9 @@ else if(value == null)
187191
}
188192

189193
}
194+
if(LOG.isDebugEnabled()) {
195+
LOG.debug("Created reverse URL mapping ["+uri.toString()+"] for parameters ["+parameterValues+"]");
196+
}
190197
return uri.toString();
191198
}
192199

src/web/org/codehaus/groovy/grails/web/metaclass/RedirectDynamicMethod.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public Object invoke(Object target, String methodName, Object[] arguments) {
9090

9191
Object actionRef = argMap.get(ARGUMENT_ACTION);
9292
String controllerName = argMap.containsKey(ARGUMENT_CONTROLLER) ? argMap.get(ARGUMENT_CONTROLLER).toString() : null;
93+
9394
Object id = argMap.get(ARGUMENT_ID);
9495
Object uri = argMap.get(ARGUMENT_URI);
9596
String url = argMap.containsKey(ARGUMENT_URL) ? argMap.get(ARGUMENT_URL).toString() : null;
@@ -122,17 +123,32 @@ else if(url != null) {
122123
}
123124
else {
124125
String actionName = establishActionName(actionRef, target, webRequest);
126+
controllerName = controllerName != null ? controllerName : webRequest.getControllerName();
127+
128+
if(LOG.isDebugEnabled()) {
129+
LOG.debug("Dynamic method [redirect] looking up URL mapping for controller ["+controllerName+"] and action ["+actionName+"] and params ["+params+"] with ["+urlMappingsHolder+"]");
130+
}
125131

126132
UrlMapping urlMapping = null;
127133
if(urlMappingsHolder!=null)
128134
urlMapping = urlMappingsHolder.getReverseMapping(controllerName, actionName, params);
129-
135+
136+
if(LOG.isDebugEnabled() && urlMapping == null) {
137+
LOG.debug("Dynamic method [redirect] no URL mapping found for params ["+params +"]");
138+
}
139+
140+
130141
if(urlMapping != null) {
131142

132143
try {
133144
params.put(ARGUMENT_CONTROLLER, controllerName);
134-
params.put(ARGUMENT_ACTION, actionName);
145+
params.put(ARGUMENT_ACTION, actionName != null ? actionName : webRequest.getActionName());
135146
String mappedUrl = urlMapping.createURL(params);
147+
148+
if(LOG.isDebugEnabled()) {
149+
LOG.debug("Dynamic method [redirect] mapped to URL ["+mappedUrl +"]");
150+
}
151+
136152
actualUriBuf.append(mappedUrl);
137153
}
138154
finally {

0 commit comments

Comments
 (0)