Search code examples
grailsgsp

How to link from a page to another in Grails


I know that in HTML, if we have a button we want to link it to another page we use the tags below: <a href="#"> button </a>

However I tried this for grail it did not work, it is taking me to a blank page:

in views/admin/admin.gsp I have a button that I am trying to link to views/admin/gm.gsp

<li><a href="Users\mzein\file_down\grails-app\views\admin\gm.gsp">GM</a></li>

I also tried the <g:link> tag:

<g:link resource="gm">

but it did not work and took me to error no path page. What is the problem am I specifying the wrong path ? or is that not how it works in grails ?

I know how to use controllers but is that the only to go from one page to another ?

I would like to use URL mapping for such a task

    class UrlMappings {

    static mappings = {
        "/$controller/$action?/$id?(.$format)?"{

        }
        "/"(view:"/index")
        "500"(view:'/error')
        "/views/admin/gm"(resources:'gm')
    }
}

<g:link resource="gm">GM</g:link>

Solution

  • You can use createLink tag with uri attribute like:

    <li><a href="${createLink(uri:'/somepath/gm.gsp')}">GM</a></li>

    Edit: ok then you need to do the request mapping in UrlMappings.groovy file like

    "/foo/bar"(view: "path/test")
    

    and make a corresponding request via g:link as

    <g:link controller="foo" action="bar" name="someName">GM</g:link> and path is the path of directory in views folder in which test.gsp is present.