如何使用 Scala 重定向到 playframework 中的相同控制器方法?
打开 url 时,
/users/{id}/foo
我将通过调用来显示视图
html.foo(userWithId)
在控制器方法 foo 中,我想确保 id 与登录用户的 id 相同,如果不是,则应将用户重定向到
/users/{theLoggedInUsersId}/foo
using
html.foo(loggedInUser)
工作正常,但这不是重定向,所以浏览器中的 url 仍然是
/users/{id}/foo
我想要真正重定向,以便 url 显示正确的 id。我不知道该怎么做。像这样使用“Action”:
Action(foo(loggedInUsersId))
如果使用递归而不提供返回类型,将会出现错误。将返回类型 play.template.Html 添加到控制器方法 foo 将得到编译器错误,因为 Action 返回 ScalaAction 而不是 Html。
我该怎么做呢。我是否应该这样做,我是否以错误的方式思考它?
编辑
重定向(...)工作正常。但没有它可以吗? 在伪 Scala 中:
def list(id: Long) = {
if (some_criteria)
html.list(user_with_id_equal_to_id)
else if(another_criteria)
list(another_user_id)
else
Action(Application.login)
}
list(another_user_id) 不起作用,因为它是递归调用,我必须在 list 方法上提供返回类型。添加返回类型 play.templates.Html 将不起作用,因为它不是 Action 的返回类型。
您明白我的意思了吗?如果我不使用对 list 的调用,而是使用 html.list(user_with_another_id) 那么它不会是重定向,浏览器中的 url 仍将是 /users/id/foo 而不是 /user /another_id/foo.
When opening the url
/users/{id}/foo
I will display the view with a call to
html.foo(userWithId)
In the controller method foo I want to make sure that id is the same as the loggedin user's id and if not should redirect the user to
/users/{theLoggedInUsersId}/foo
using
html.foo(loggedInUser)
works fine but this is not a redirect, so the url in the browser still is
/users/{id}/foo
I want to really redirect so that the url is showing the correct id. I'm not sure how to do this. Using "Action" like this:
Action(foo(loggedInUsersId))
will get an error for using recursion without supplying a return type. Adding the return type play.template.Html to the controller method foo, will get a compiler error since Action returns a ScalaAction and not Html.
How should I do this. Should I even do it, am I stuck thinking about it in the wrong way?
Edit
The Redirect(...) works fine. But is it possible to do without it?
In pseudo-Scala:
def list(id: Long) = {
if (some_criteria)
html.list(user_with_id_equal_to_id)
else if(another_criteria)
list(another_user_id)
else
Action(Application.login)
}
The list(another_user_id) won't work since it's a recursive call and I have to supply a return type on the list method. Adding the return type play.templates.Html won't work since that is not the return type of Action
Do you see what I'm getting at? If I instead of using a call to list uses html.list(user_with_another_id) then it won't be a redirect and the url in the browser will still be /users/id/foo instead of /user/another_id/foo.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有两种方法可以做到这一点。第一种是使用重定向返回类型。
您想要的方式是像您建议的那样使用操作。
这应该可以正常工作。 Play 实际上将此调用解析为 URL 并调用重定向。所以我向您展示的第一个例子几乎是一样的。该操作效果更好,因为它可以防止您在更改 url 时更改代码。我们可能需要查看您的更多代码才能了解发生了什么情况。
这是一个更清楚的例子。
参考: http://scala.playframework.org/documentation/scala-0.9 .1/controllers#Returntypeinference
There are two ways to go about doing this. The first is to use the Redirect return type.
The way you want is to use the Action like you suggested.
This should work fine. Play actually resolves this call as URL and calls a redirect. So the first example I showed you is pretty much the same thing. The Action works better because it prevents you from having to change code if you change the url. We may need to see more of your code to see what's going on.
Here is a more clear example.
Reference: http://scala.playframework.org/documentation/scala-0.9.1/controllers#Returntypeinference