电话号码的 Grails UrlMappings
我想对其进行设置,以便当用户使用 URL 中的电话号码访问应用程序时,Grails 会将用户定向到指定的配置文件,例如:
localhost:8080/Application/profile/07871216969 -> /user/show/User.findByUserTelephone(07871216969)
或最好
localhost:8080/Application/07871216969 -> /user/show/User.findByUserTelephone(07871216969)
但是,我不知道在调用 findByUserTelephone 闭包时如何引用 URL 中的电话号码;我注意到类似的内容:
/profile/$telephoneNumber
但我不完全确定这是如何实现的。
另外,由于 /user/show 操作需要参数中的 id,我不确定 findUserByTelephone 闭包是否有任何用途,因为它返回 User 作为对象,而且我似乎无法 getId() 或 .id检索 id 的对象。
已解决/解决方案:
我通过在控制器中创建另一个名为profile
的操作解决了这个问题,该操作的代码类似于以下内容:
def profile = {
if(User.findByUserTelephone(params.userTelephone)) {
def userInstance = User.findByUserTelephone(params.userTelephone)
if (!userInstance) {
flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
redirect(action: "list")
}
else {
[userInstance: userInstance]
}
} else {
render("Sorry, that user wasn't found in our database.")
}
}
然后我创建了以下UrlMapping条目:
`"/$userTelephone"(controller: "user", action: "profile")`
因此,当用户在系统中输入电话号码(或 /
之后的任何字符串)时,系统会将用户路由到用户/个人资料操作,该操作将尝试通过电话号码查找用户。如果成功,将显示用户详细信息,否则将显示错误消息。
I'd like to set it so when a user visits the application with a telephone number in the URL Grails directs the user to the specified profile, for example:
localhost:8080/Application/profile/07871216969 -> /user/show/User.findByUserTelephone(07871216969)
or preferably
localhost:8080/Application/07871216969 -> /user/show/User.findByUserTelephone(07871216969)
However, I have no idea how to reference the telephone number in the URL when calling the findByUserTelephone closure; I've noticed something like:
/profile/$telephoneNumber
however I'm not entirely sure how that works out.
Also, as the /user/show action requires an id in the parameters I am not sure if the findUserByTelephone closure is any use as it returns the User as an object, and I can't seem to either getId() or .id the object to retrieve the id.
SOLVED/SOLUTION:
I solved this by creating another action in the Controller called profile
, this action then had code similar to the following:
def profile = {
if(User.findByUserTelephone(params.userTelephone)) {
def userInstance = User.findByUserTelephone(params.userTelephone)
if (!userInstance) {
flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
redirect(action: "list")
}
else {
[userInstance: userInstance]
}
} else {
render("Sorry, that user wasn't found in our database.")
}
}
I then created the following UrlMapping entry:
`"/$userTelephone"(controller: "user", action: "profile")`
Thus, when a user enters a telephone number into the system (or any string after the /
) it will route the user to the user/profile action, which will attempt to find a user by their telephone number. If successful, will show the users details otherwise will show an error message.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用与控制器名称不同的 url 路径,以后可能会为您省去一些麻烦。因此,如果您有类似的情况:
您将不会有任何与多个 URLMapping 条目匹配的 URL 模式,这可能会有点令人困惑。我猜想 just:
会起作用,但我希望最终会遇到这样的情况:Grails 会被匹配相同请求路径的多个 URLMapping 搞糊涂,除非你非常小心。
It will probably save you some headaches later if you use a url path which is not the same as a controller name. So if you have something like:
you won't have any URL patterns that match multiple URLMapping entries which can get a little confusing. I would guess that just:
would work, but I would expect to end up in situations where Grails gets confused by multiple URLMappings matching the same request path unless you're very careful.