tags:

views:

58

answers:

1

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

+1  A: 

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.

ig0774
ahh -- ok, whats wrong with the working solution i gave earlier, using () instead of {}. also, no matter what I try, before links were showing up as myObj, and now they are all myobj. whats the deal with that?
hvgotcodes
Nothing's wrong with the parentheses. It just works differently (am I missing something?).Also, the change in the way the links are showing is probably just due to the URL mapping taking effect (by default Grails link to myController is '/myController' whereas your mapping tells Grails to use '/mycontroller' instead). If you're trying to make your links case insensitive you may need to do something a little fancier.
ig0774
is there a way to make all links case in-sensitve? That would be grand. I appreciate your time on this one...
hvgotcodes
@hvgotcodes: see my edit above... I'm less than certain it will work, but it's the easiest way that occurs to me to do if it works. In a production scenario, it's probably a good idea to memoize the results. The only alternative I can think of is to for the DefaultMappingUrlEvaluator to support case-insensitive mappings directly and inject your version into the Spring context, but that seems like a lot of work for very little gain.
ig0774
@ig0774, ok thanx for all the help. gave you the checkmark even though I haven't tried your solution, for sticking with my follow ups and of course helping with the first answer.... Just wanted to be sure it wasn't something trivial that I missed in the documentation.
hvgotcodes
although this seems like a nifty feature that some people might want...
hvgotcodes