Playframework路线问题

发布于 2024-11-08 08:00:30 字数 305 浏览 0 评论 0原文

我的应用程序路由文件中有这个:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掩于岁月 2024-11-15 08:00:30

如果您已经尝试过下面的路线(这是使用路线的正确方法)

#{form @Tweets.save()}

并且这不起作用,我认为您可能将路线放在了错误的位置。确保它位于路由文件的顶部,而不是在全部路由之后。路由文件是按顺序处理的,因此如果找到了包罗万象的文件,则首先使用它,而忽略其他路由。包罗万象的看起来像

*   /{controller}/{action}     {controller}.{action} 

If you have already tried the route below (which is the correct way to use routes)

#{form @Tweets.save()}

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

*   /{controller}/{action}     {controller}.{action} 
匿名的好友 2024-11-15 08:00:30

尝试使用

 #{form @Tweets.save()}

我认为建议使用类名和方法名。

编辑:

播放框架路由的工作方式是定义一些路由,

GET     /clients         Clients.index

如果遇到带有 URI /clients 的请求,那么它将被拦截到 Clients.index( )。如果您有另一个路由,

GET     /clients         Clients.save

则框架会忽略此路由,因为 /clients 已经有了映射。 (很可能它在控制台或日志流中给出了一些错误,请检查您的日志。)

因此您不能让它像那样工作。我明白了,您请求的反向映射将为不同的方法返回相同的 URI。然而,该框架的目的是拦截请求,以便它会简单地忽略您的第二个路由。

尝试分开页面。您最有可能想要的是为两个函数呈现相同的视图。您无需将它们重定向到相同的 URI 即可完成此操作。

Try using

 #{form @Tweets.save()}

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

GET     /clients         Clients.index

If a request encountered with URI /clients then it will be intercepted to Clients.index(). If you have another routing such that

GET     /clients         Clients.save

Then 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.

沙沙粒小 2024-11-15 08:00:30

我认为(如果我没有误读的话)问题在于你期待错误的行为。

据我了解,您期​​望提交将转到 Tweet.save() (POST 方法),然后返回 Tweet.create() (GET 方法),因为两者共享相同的路径(/new)。

实际上,Play 正在调用 Tweet.save(),并且它期望在 Tweet.save() 末尾进行渲染以显示一些结果。如果您想重定向到 Tweet.create(),您可以在 Tweet.save() 实现结束时调用该方法,使用:

create(<params>);

或 ,

render("@create", <params>);

并且应该重定向(通过 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:

create(<params>);

or

render("@create", <params>);

and that should redirect (via 302) to the GET version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文