hvgotcodes
hvgotcodes

Reputation: 120138

url mappings grails

right, basically we have a link into our system that is all lowercase but that needs to be camel cased. rather than get the other developers to fix this, I want to just make the problem go away by url magic

"/myobj/$action?/$id?"{ 
    controller: "myObj"        
}

what i am trying to do is map anything that references controller myobj to controller myObj instead, and keep the action and everything else the same. The above code gives an exception, saying the

2010-07-11 18:14:50,021 ERROR [default] - Servlet.service() for servlet default threw exception
java.lang.IllegalArgumentException: URL mapping must either provide a controller or view name to map to!

however the following works

 "/myobj/$action?/$id?"(controller: "myObj")

I dont understand, given the documentation. Why didnt the closure work? This is grails 1.2.0...

Upvotes: 3

Views: 2788

Answers (1)

ig0774
ig0774

Reputation: 41247

You should use

'/myobj/$action?/$id?' {
    controller = 'myObj'
}

Edit As a bit of explanation: the colon in the working example creates a map as described here (the working case is a Groovy function invocation with named parameters). Within the closure, you need to set the value of the controller property directly, i.e., using the equals sign.

This difference is demonstrated not specifically highlighted in the Grails URL mapping documentation.

Edit Here's an untested method that may yield case insensitive controller mappings... maybe.

'$myController/$action?/$id?' {
    grailsApplication.getArtefacts('Controller').find { it.name.toLowerCase() == myController.name.toLowerCase }.with { controller = it.name }
}

I'm assuming here (which I haven't verified) that the Grails application is available within the MappingCapturingClosure in the DefaultMappingUrlEvaluator.

Upvotes: 8

Related Questions