grails 2+ url mapping specific rules
-
21-12-2019 - |
Вопрос
EDIT
I had forget a "/" so it is actually working. I don't remove this post as it might be useful for others
I have read many SO answer about grails urlMapping but didn't find any answer that fit my needs.
Let say I have two Domain: Project
and Contributor
class Project{
String name;
String urlName; //is the name converted to url
static belongsTo = [contributor: Contributor]
}
//a contributor has many Projects, then in the `Project`
//table I have `contributor_id`
class Contributor{
String userName;
static hasMany = [projets: Project]
}
My urls looks like this:
www.mysite.com/frank/frank-s-nice-project/comments
www.mysite.com/bill/bill-s-great-project/comments
www.mysite.com/lucie/lucie-s-awesome-project/comments
Which is corresponding to : www.mysite.com/username/urlName/comments
username
comes from Contributor Domain.
urlName
comes from Project Domain
How can I set urlMappgings.groovy
to always redirect to the same controller/action
when url matches "/$username/$urlName/comments"
I have tried like this but with no success.
static mappings = {
"/$username/$urlName/comments"(controller: 'comment', action: 'index)
}
Thanks for your help.
Решение 2
I answer my question to make it as accepted answer,
This actually works
static mappings = {
"/$username/$urlName/comments"(controller: 'comment', action: 'index)
}
Другие советы
static mappings = {
"/comments/$username/$urlName"(controller: 'comment', action: 'index)
}
What happens if you replace comments to the first value and make comments then parse username/url ?
The problem I can see with your initial mapping is that username would clash with actual real actions of your project