Grails “loses” custom URL mapping when following a

2019-04-16 10:37发布

问题:

I have an application where users can browse maps in two ways (like thumbnails and in a list)

/map/browse
/map/list

Now, I would like to restrict these views to just show maps of a specific user, for example through

/user/3/browse
/user/3/list

So I created the mapping:

"/user/$userId/browse" {
  controller = "map"
  action = "browse"
}

"/user/$userId/list" {
  controller = "map"
  action = "list"
}

Now, I can go to /user/3/browse, but as soon as I click on a pagination link or change the pagination filters, the URL goes back to /map/browse.

Also, if I set the userId to null in the controller, I get the error:

Error 500: Error processing GroovyPageView: Error executing tag : Unable to create URL for mapping [/user/(*)/list] and parameters [["action":"list", "controller":"map", "max":20, "offset":0, "sort":"uploadDate", "order":"desc", "userId":null, "totalMaps":30]]. Parameter [userId] is required, but was not specified! at /views/map/browse.gsp:26

The pagination works as follows:

<div class="paginateButtons">
    <g:paginate controller="map" action="browse" total="${mapInstanceTotal}"></g:paginate>
</div>

What can I do against that or what would be the correct way of implementing what I want?

I don't necessarily need to have that URL mapping, I only need a nice way of saying: "Display maps of only one user"

回答1:

It seems that the problem is not at your URL mapping configuration ,but in your way to create link. I think it's better if you use Named URL Mapping : it's clearer than your approach now, and when create link for pagination you only need to specify the url name. For example:

In UrlMappings.groovy:

static mappings = {

    name accountDetails: "/details/$acctNumber" {
        controller = 'product'
        action = 'accountDetails'
    }
}

In view - gsp page:

<g:link mapping="accountDetails" params="[acctNumber:'8675309']">
    Show Account
</g:link>