Grails URL mapping cause error on GSP
Asked Answered
S

2

1

I have a site that have URL similar to this:

/mysite/admin/controller/action/id
/mysite/search/controller/action/id
/mysite/user/controller/action/id

I have my URL mapping like this

"/$prefix/$controller/$action?/$id?"{
    constraints {}
}

I am able to get to the controller correctly.

But on the GSP side

<g:link controller="controller">abc</g:link> ==> <a href="/mysite/controller/...">abc</a>

Notice how I lose the prefix between mysite and the controller.

Swanner answered 13/2, 2012 at 15:14 Comment(0)
E
3

You can use named url mappings and then pass the prefix as part of the params:

URLMappings:

name prefix: "/$prefix/$controller/$action?/$id?"{
    constraints {}
}

GSP:

<g:link mapping="prefix" params="[prefix:$prefix, controller:...]">abc</g:link>

To use sortableColumn, just put all of the URLMapping parameters in the params property:

<g:sortableColumn property="col" title="title" params="[ prefix: 'prefix', controller:'controller', action:'action']" />
Evangelista answered 13/2, 2012 at 15:47 Comment(2)
That works for <g:link... Excellent But now I cant get this to work for other Linking tags such as sortableColumn or paginate. Is there some link that you can point me to for further reading? for my sortableColumn I did something like this: <g:sortableColumn mapping="prefix" params="[prefix:prefix]" and it just add a &prefix=xxx vs mapping it as a prefix the URLSwanner
My previous comment was incorrect. I deleted it and updated my answer.Evangelista
C
0

It works when you hit the URL in browser, because prefix is available in URL. It does not work when you use link tag to create url, because grails does not have information about which prefix should be used for this controller. You will need to provide the value for prefix to link tag.

Try this

<g:link controller="controller" params="[prefix:'admin']">abc</g:link>

in-short - You have to pass those dynamic variables as params if you want link re-writing to consider them. Read more docs here

Canuck answered 13/2, 2012 at 16:26 Comment(3)
I understand that I need to pass the params="[prefix:'admin']", what I am try to say is. It works only for <g:link tags, but for other such as <g:sortableColumn or <g:paginate, even if I pass the params so that it will look like this: <g:sortableColumn property="${key}" title="${message}" mapping="prefix" params="${params}" /> the URL for this translates to /mysite/controller/action/id?prefix=admin, and my prefix is gone in the URL.Swanner
In that case you will have to change url mappings to explicitly specify the controller name "/admin/$controller/$action?/$id?"(controller:"Admin") "/search/$controller/$action?/$id?"(controller:"Search")Canuck
Yes, I attempted that, that actually was my first attempt and it still does not retain the prefix.Swanner

© 2022 - 2024 — McMap. All rights reserved.