我想,当用户在URL中的Grails的电话号码访问的应用程序来设置它,以便将用户指引到指定的配置文件,例如:Grails的UrlMappings的电话号码
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闭包是否有任何用处,因为它将用户作为对象返回,并且我看起来似乎都不是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")`
因此,当用户将电话号码输入到系统em(或在/
之后的任何字符串),它会将用户路由到用户/配置文件操作,该操作将尝试通过其电话号码查找用户。如果成功,将显示用户的详细信息,否则会显示错误消息。
是的,编码上面的UrlMapping并做了类似的事情后,我遇到了问题不久。谢谢。 :-) – krslynx