Search code examples
grailsgrails-2.0url-mapping

Mapping URL in main menu to every page


I am using in my grails 2.3.4 project with spring security and spring securit ui.

I am having scaffolde my domain contact to the view. I have also a .gsp page which is not scarfolded.

My links from my main menue looks like that:

<li><a href="pricing">Pricing</a></li>
<li><a href="contact/create">Contact</a></li>

Thats my URLMappings

class UrlMappings {

    static mappings = {

        "/$controller/$action?/$id?(.${format})?"{
            constraints {
                // apply constraints here
            }
        }

        "/private/$controller/$action?/$id?(.${format})?"{
            constraints {
                // apply constraints here
            }
        }

        "/"(view:"/index")
        "/pricing"(view:"/pricing")
        "/private/dashboard"(view:"/private/dashboard")
        "/contact/create"(view:"/contact/create")
        "500"(view:'/error')

    }
}

My problem is when I am using this two links from my mainpage / then everythings works fine. However using them from 5432:localhost/TestApp/pricing I am getting the link 5432:localhost/TestApp/pricing/contact/create which goes is not available. If I am using <li><a href="../contact/create">Contact</a></li>, I am going to 5432:localhost/contact/create, which is also not available. How to go to contact/create from every page?

I appreciate your reply!


Solution

  • The simplest and safest approach would be

    <li><g:link uri="/contact/create">Contact</g:link></li>
    

    Other attributes on the g:link tag will pass through to the generated a tag, with the exception of id - you need to use elementId instead, as id is treated as a parameter to the link generation (controller/action/id)

    <li><g:link uri="/contact/create" class="nav" elementId="contactlink">Contact</g:link></li>
    

    would become

    <li><a href="/TestApp/contact/create" class="nav" id="contactlink">Contact</a></li>
    

    (where /TestApp is the application context path - if you deploy the app at a different context path then then link will change to match).