Skip to content

Commit

Permalink
Fixes #9218 - Hyphenated URLs do not work when using the 'hyphenated'…
Browse files Browse the repository at this point in the history
… url converter strategy
  • Loading branch information
graemerocher committed Sep 7, 2015
1 parent d0ea359 commit 4ec4ac4
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 20 deletions.
10 changes: 10 additions & 0 deletions grails-core/src/main/groovy/grails/core/GrailsControllerClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package grails.core;

import grails.web.UrlConverter;

import java.util.Set;

/**
Expand Down Expand Up @@ -107,4 +109,12 @@ public interface GrailsControllerClass extends InjectableGrailsClass {
* @throws Throwable Thrown when an error occurs invoking the action
*/
Object invoke(Object controller, String action) throws Throwable;


/**
* Register a new {@link grails.web.UrlConverter} with the controller
*
* @param urlConverter The {@link grails.web.UrlConverter} to register
*/
void registerUrlConverter(UrlConverter urlConverter);
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public String toUrlElement(String propertyOrClassName) {
for (char c : charArray) {
if (Character.isUpperCase(c)) {
if (builder.length() > 0) {
builder.append("-");
builder.append('-');
}
builder.append(Character.toLowerCase(c));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import grails.core.GrailsControllerClass;
import grails.util.GrailsClassUtils;
import grails.web.Action;
import grails.web.UrlConverter;
import groovy.lang.GroovyObject;
import org.springframework.cglib.reflect.FastClass;
import org.springframework.cglib.reflect.FastMethod;
Expand Down Expand Up @@ -61,7 +62,9 @@ public DefaultGrailsControllerClass(Class<?> clazz) {
methodStrategy(actions);
}

public void initialize() {}
public void initialize() {
// no-op
}

@Override
public Set<String> getActions() {
Expand Down Expand Up @@ -130,6 +133,18 @@ public boolean mapsToURI(String uri) {
return false;
}

/**
* Register a new {@link grails.web.UrlConverter} with the controller
*
* @param urlConverter The {@link grails.web.UrlConverter} to register
*/
@Override
public void registerUrlConverter(UrlConverter urlConverter) {
for (String actionName : actions.keySet()) {
actions.put( urlConverter.toUrlElement(actionName), actions.get(actionName));
}
}

/**
* Invokes the controller action for the given name on the given controller instance
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,6 @@ class ControllersGrailsPlugin extends Plugin {
}
}
}
// now that we have a BeanBuilder calling registerBeans and passing the app ctx will
// register the necessary beans with the given app ctx
controllerClass.initialize()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class DefaultLinkGenerator implements LinkGenerator, org.codehaus.groovy.grails.
@Qualifier('grailsDomainClassMappingContext')
MappingContext mappingContext

@Autowired
@Autowired(required = false)
UrlConverter grailsUrlConverter = new CamelCaseUrlConverter()

DefaultLinkGenerator(String serverBaseURL, String contextPath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,6 @@ public static String forwardRequestForUrlMappingInfo(HttpServletRequest request,
return forwardUrl;
}

private static UrlConverter locateUrlConverter(final GrailsWebRequest webRequest) {
UrlConverter urlConverter = null;
try {
urlConverter = webRequest.getAttributes().getApplicationContext().getBean("grailsUrlConverter", UrlConverter.class);
} catch (NoSuchBeanDefinitionException e) {
urlConverter = new CamelCaseUrlConverter();
}
return urlConverter;
}

/**
* Include whatever the given UrlMappingInfo maps to within the current response
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,13 @@ abstract class AbstractGrailsControllerUrlMappings implements UrlMappings{


void registerController(GrailsControllerClass controller) {
def namespace = urlConverter ? urlConverter.toUrlElement( controller.namespace ) : controller.namespace
def plugin = urlConverter ? urlConverter.toUrlElement( controller.pluginName ) : controller.pluginName
def controllerName = urlConverter ? urlConverter.toUrlElement( controller.logicalPropertyName ) : controller.logicalPropertyName
boolean hasUrlConverter = urlConverter != null
if(hasUrlConverter) {
controller.registerUrlConverter(urlConverter)
}
def namespace = hasUrlConverter ? urlConverter.toUrlElement( controller.namespace ) : controller.namespace
def plugin = hasUrlConverter ? urlConverter.toUrlElement( controller.pluginName ) : controller.pluginName
def controllerName = hasUrlConverter ? urlConverter.toUrlElement( controller.logicalPropertyName ) : controller.logicalPropertyName

mappingsToGrailsControllerMap.put(new ControllerKey(namespace, controllerName, null, plugin), controller)
mappingsToGrailsControllerMap.put(new ControllerKey(null, controllerName, null, plugin), controller)
Expand All @@ -144,7 +148,7 @@ abstract class AbstractGrailsControllerUrlMappings implements UrlMappings{
}

for(action in controller.actions) {
action = urlConverter ? urlConverter.toUrlElement(action) : action
action = hasUrlConverter ? urlConverter.toUrlElement(action) : action
def withPluginKey = new ControllerKey(namespace, controllerName, action, pluginNameToRegister)
def withPluginKeyWithoutNamespaceKey = new ControllerKey(null, controllerName, action, pluginNameToRegister)
def withoutPluginKey = new ControllerKey(namespace, controllerName, action, null)
Expand Down

0 comments on commit 4ec4ac4

Please sign in to comment.