【问题标题】:play 2 get url parameter in filterplay 2 获取过滤器中的url参数
【发布时间】:2015-01-26 14:22:37
【问题描述】:

如何在Filter中获取url参数

路线

GET        /:lang/               controllers.SiteIndex.index(lang)
GET        /:lang/genres/        controllers.Genres.allGenres(lang)

全球

object Global extends GlobalSettings {
  override def onRouteRequest(request: RequestHeader): Option[Handler] = {
    val a = request.getQueryString("lang")
    val b = request.getQueryString("language")
    println("executed before every request:" + request.toString)
    super.onRouteRequest(request)
  }
}

如何在全局中获取参数语言并在所有操作中设置语言

【问题讨论】:

    标签: scala playframework playframework-2.0


    【解决方案1】:

    我认为使用自定义操作构建器会更容易,因为 onRouteRequest 不会影响传递给您操作的参数。你可以得到类似的东西:

    def myAction = ActionWithLang { req: MyCustomReqWithLang => 
       ...
       Ok("woo " + req.myLang)
    }
    

    查看动作组合下的播放文档以获取更多信息:https://www.playframework.com/documentation/2.3.x/ScalaActionsComposition

    【讨论】:

    • 请求中有一个可能的获取 url 参数,比如这个对象 LocaleController extends ActionBuilder[Request] { override def invokeBlock[A](request: Request[A], block: (Request[A]) = > Future[Result]): Future[Result] = { request.getParameter("locale") block(request) } }
    • 确切地说,ActionWithLang 最容易使用 ActionBuilder 构建
    猜你喜欢
    • 2013-10-03
    • 2015-10-25
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 2012-06-28
    • 2014-06-25
    • 2011-12-19
    • 1970-01-01
    相关资源
    最近更新 更多