Playframework路线问题
我的应用程序路由文件中有这个:
GET /new Tweets.create
POST /new Tweets.save
在我看来,我正在创建一个这样的表单:
#{form @save()}
...
#{/form}
但是一旦提交表单,它就会将我发送到 /tweets/save
而不是 /新的。我有什么想法可以解决这个问题吗?谢谢!
I have this in my applcation routes file:
GET /new Tweets.create
POST /new Tweets.save
And in my view I'm creating a form like this:
#{form @save()}
...
#{/form}
But once is submit the form it's sending me to /tweets/save
and not to /new
. Any ideas how I can fix this? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您已经尝试过下面的路线(这是使用路线的正确方法)
并且这不起作用,我认为您可能将路线放在了错误的位置。确保它位于路由文件的顶部,而不是在全部路由之后。路由文件是按顺序处理的,因此如果找到了包罗万象的文件,则首先使用它,而忽略其他路由。包罗万象的看起来像
If you have already tried the route below (which is the correct way to use routes)
and this did not work, I think you may have put your route in the wrong place. Make sure it is at the top of the routes file, and not after the catch-all route. The routes file is processed in order, so if the catch-all is found, this is used first and your other route is ignored. The catch-all looks like
尝试使用
我认为建议使用类名和方法名。
编辑:
播放框架路由的工作方式是定义一些路由,
如果遇到带有 URI
/clients
的请求,那么它将被拦截到Clients.index( )
。如果您有另一个路由,则框架会忽略此路由,因为
/clients
已经有了映射。 (很可能它在控制台或日志流中给出了一些错误,请检查您的日志。)因此您不能让它像那样工作。我明白了,您请求的反向映射将为不同的方法返回相同的 URI。然而,该框架的目的是拦截请求,以便它会简单地忽略您的第二个路由。
尝试分开页面。您最有可能想要的是为两个函数呈现相同的视图。您无需将它们重定向到相同的 URI 即可完成此操作。
Try using
I think it is suggested to use class names with method names.
EDIT:
The way the play framework routing works is you define some route as
If a request encountered with URI
/clients
then it will be intercepted toClients.index()
. If you have another routing such thatThen the framework ignores this routing because
/clients
aready has a mapping. (Most probably it is giving some error in the console or logging stream, check your logs.)Therefore you can't make it work like that. I see, you request a reverse mapping that will return the same URI for different methods. However the framework aims to intercept requests so that it will simply ignore your second routing.
Try to separate pages. Most probably what you want is to render the same views for two functions. You can do that without redirecting them to the same URI.
我认为(如果我没有误读的话)问题在于你期待错误的行为。
据我了解,您期望提交将转到 Tweet.save() (POST 方法),然后返回 Tweet.create() (GET 方法),因为两者共享相同的路径(/new)。
实际上,Play 正在调用 Tweet.save(),并且它期望在 Tweet.save() 末尾进行渲染以显示一些结果。如果您想重定向到 Tweet.create(),您可以在 Tweet.save() 实现结束时调用该方法,使用:
或 ,
并且应该重定向(通过 302)到 GET 版本。
I think (if I did not misread) that the issue is you expecting the wrong behavior.
As I understand you expect that the submit will go to Tweet.save() (POST method) and then back to Tweet.create() (GET method) as both share the same path (/new).
In reality Play is calling Tweet.save() and it expects a render at the end of Tweet.save() to display some result. If you want to redirect to Tweet.create() you can do a call to that method at the end of the implementation of Tweet.save(), with either:
or
and that should redirect (via 302) to the GET version.