POST请求后重定向到引荐来源 [英] Redirect to referer after a POST Request

查看:105
本文介绍了POST请求后重定向到引荐来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Play中有一个Web应用程序.该Web应用程序由几个页面组成.每个页面上都有一个小标志,使用户可以将语言(语言环境)从德语更改为英语,然后再更改为英语.

I have a web application in Play. The web application consists of several pages. In every page there is a small flag that enables the user to change the language (locale) from german to english and back.

我通过重定向到引荐网址来解决此问题:

I handle this with a redirect to referer:

  def referer(implicit request: Request[AnyContent]) =
    request.headers.get(REFERER).getOrElse(mainUrl)
  def locale(l: String) = Authenticated { user =>
     implicit request =>
     Redirect(referer).withCookies(Cookie(LANG, if (l == "de" || l == "en") l else "de"))
  }

工作正常.好吧,至少对于GET请求.

It is working fine. Well, at least for GET requests.

我有一个特定的页面,用户必须在其中输入表单的数据.然后将此表单过帐到服务器.如果发现错误,则照常使用错误消息再次显示该表单.现在,如果用户想更改语言(通过单击标志),则重定向到引荐来源将不起作用,因为它尝试使用GET请求,并且Play抱怨此方法不存在GET路由(是真的).​​

I have a specific page where the user has to input data in a form. This form is then POSTed to the server. Were errors found, the form is displayed again with the error messages, as usual. Now, if the user wants to change the language (by clicking on the flag), the redirect to referer does not work, because it tries to use a GET request, and Play complains that a GET route does not exist for this method (which is true).

我通过缓存表单并定义从缓存中获取表单的另一种方法来解决这个问题:

I am solving this by caching the form and defining another method where the form is taken from the cache:

# User data is POSTed to the server
POST    /create/insert              controllers.MyCreate.insert()
# After a redirect the cached form is displayed again
GET     /create/insert              controllers.MyCreate.insertGet()

它可以工作,但是我不喜欢这种解决方案.仅为了解决此问题而在路由中创建另一个条目和另一种方法似乎并不正常.我需要为我的应用程序中的每个POST路由添加此hack!

It works, but I don't like this solution. It does not seem normal to have to create another entry in the routes and another method just to adress this problem. I would need to add this hack for every POST route in my application!

是否有更优雅的解决方案?

Is there a more elegant solution to this?

推荐答案

您可以将其更改为以下内容(未经测试):

You could change it into something like this (untested):

def changeLang(lang:String, returnUri:String) = Action {
  Redirect(returnUri)
    .withCookies(Cookie(LANG, if (lang == "de" || lang == "en") lang else "de"))
}

在您的模板中,您将在链接中输出到changeLang的路线,您可以通过request

In you template you would output the route to changeLang in the link, you can get the uri via the request

@routes.Application.changeLang("en", request.uri).url

我建议您将request隐式包含在操作中,并将其定义为隐式包含在模板中,这样就无需将其传递给每个模板.

I suggest you make request implicit in your action and define it as implicit in your template so you don't need to pass it on to each template.

// in the controller
def myUrl = Action { implicit request =>
  Ok(views.html.myTemplate("something"))
}

// in the template
@(title:String)(implicit request:play.api.mvc.RequestHeader)


编辑

对于POST请求,通常(对于这些类型的框架)使POST请求简单处理,然后重定向到另一页.通常的流程是这样的:


Edit

As for the POST requests, it common (for these types of framework) to have POST requests simple handle stuff and then redirect to another page. The usual flow is like this:

  • 表格提交给处理程序
  • 处理程序对表单信息进行处理
  • 处理程序重定向到页面

一个例子:

// Hooked up to a GET route
def edit(id:Long) = Action {
   // render the view with a form that displays the element with given id
   // if the flash scope contains validation information, use that in display
}

// Hooked up to a POST route
def editHandler = Action {
   // validate the form
   // if validation succeeds
     // persist the object
     // redirect to edit
   // else
     // put the form information into the flash scope
     // put any validation messages into the flash scope
     // redirect to edit
}

如果您不想使用此流程,则无论如何都需要同时具有GET和POST路由.用户可能会在结果页面上重新加载页面.

If you do not want to use this flow you need to have both a GET and POST route anyway. The user might do a page reload on the resulting page.

这篇关于POST请求后重定向到引荐来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆