Search code examples
grailsviewurl-mapping

UrlMapping :: View attribute in matched block ignored


Given URI /admin/article/index, why would this url mapping not work?

"/admin/$controller/$action?/$id?"{ 
        view = "/admin/index" // no dice, ignored 
        //action = "foo" // uncommented, this is picked up 
} 

I'd like for all admin controllers to use the admin view by default (and not have to render the view in each action of each controller). Same goes for "/account/$controller/..." and any other site module that should use a common view.

Perhaps there's another way to achieve this, but assumed that UrlMappings is the place to do it...


Solution

  • Looks like you are trying to do something very different than what you wrote.

    You already have the action mapped in the base URL mapping, and the view is automatically selected based on the controller, so you need to define different mappings for those views that don't have a controller, and yet another mapping for items with a default action of foo. The default action on controllers is index, though, so there is usually no need to supply a default action without also specifying a controller.

    I think you are, in general, misunderstanding how an MVC framework works. The controller should not be rendering anything, and the views should be specific to the controller/action. If multiple controllers are rendering the exact same view, I'd be willing to bet that either the controller is rendering HTML or the view is overly complicated.

    You should look into Layouts with SiteMesh, which allows you to create default template structures, then just have the specific content change through views.