简体   繁体   中英

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...

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, ie, 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM