Play Framework 2.2:获取请求页面的URL

时间:2014-06-12 08:37:21

标签: java playframework playframework-2.2

播放框架JAVA:

我正在尝试获取请求控制器功能的URL的名称。例如 , 我有一条路线

GET /mypage controllers.Mypage.myfunction()

我有另一个页面请求同一个控制器

GET /anotherpage controllers.Mypage.myfunction()

如果请求来自/mypage或来自/anotherpage,是否可以在控制器中查找?

由于

2 个答案:

答案 0 :(得分:21)

假设您访问example.com:9000/login?param=test,然后在控制器功能中访问:

public static Result login() {

    // set to "/login" -- The URI path without query parameters.
    String path = request().path(); 

    // set to "/login?param=test" -- The full URI.
    String uri = request().uri(); 

    // set to "example.com:9000" -- The host name from the request, with port (if specified by the client).
    String host = request().host(); 

    ...
}

答案 1 :(得分:4)

你没有提到语言

在Scala中,您可以通过

获取控制器中的路径
val path= request.path

val path=request.uri

我在play framework 2.2 *

中试过这个

在Java中

String path= request.path();

String path=request.url();

根据此链接http://www.playframework.com/documentation/1.0.1/api/play/mvc/Http.Request.html