Search code examples
grailsgrails-controller

Use controllers globally in Grails


I'm new to Grails and am trying to build a CMS with it. I want the navigation menu to read from the database so a new page will automatically get a link in the navigation. I've been reading Grails: use controller from index.gsp and related questions, but the answers don't seem to work for me. :(

I've created a domain class named Navigation and a template called _header.

In the "Navigation/list" namespace everything works fine, but outside I can't get to the Navigation data.

I've setup url mapping like so:

class UrlMappings {
    static mappings = {
        "/$controller/$action?/$id?"{
            constraints {
                // apply constraints here
            }
        }
        "/"(controller : "Navigation", action : "list")
        "/"(view:"/index")
        "500"(view:'/error')
    }
}

But that doesn't seem to work. Any clues on what could be the problem?


Solution

  • I was looking at the problem all wrong, urlmapping only made the index.gsp redirect to navigation/list. What I was looking for was the

    DomainClass.findAll( String query )
    

    propertie to use in the g:each tag

    <g:each in="${Navigation.findAll('from Navigation as n where n.css=?', ['ctBoven'])}" var="oNavigation" status="i">
    

    This allows me to read any database from any page.