I have a URL that I'd like to handle with different controllers depending on the method. Is there a way to do this via UrlMappings?

Using two different mappings for the same URL doesn't work (the second overwrites the first)...

有帮助吗?

解决方案

Untested, but can you try with the below mapping:

"/myurl" {
    if(params.method == "doThis"){
        controller = "doThis"
        action = "doThisAction"
    } else if(params.method == "doThat"){
        controller = "doThat"
        action = "doThatAction"
    }
}

Assuming,

http://<appserver>/myurl?method=doThis
http://<appserver>/myurl?method=doThat

UPDATE
When referring to HTTP Methods you can use filter (where we have request available) as below:

class RoutingFilters{
    def filters = {
        routingCheck(uri: '/myurl/**' ) {
            before = {
                if(request.method == 'GET'){
                    redirect(controller: 'doThis', action: 'doThis')
                }

                if(request.method == 'POST'){
                    redirect(controller: 'doThat', action: 'doThat') 
                }

                //So on and so forth for PUT and DELET

                return false
            }
        }
    }
}

provided the url mapping would look something like:

//defaulting to "doThis" or any other "valid" controller as dummy
"/myurl/$id?"(controller: 'doThis') 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top