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

Inc*_*tor 12 java playframework playframework-2.2

播放框架JAVA:

我试图获取请求控制器功能的URL的名称.例如,我有一条路线

GET /mypage controllers.Mypage.myfunction()

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

GET /anotherpage controllers.Mypage.myfunction()

有没有办法在控制器中找到请求是来自/mypage还是来自/anotherpage

谢谢

Mic*_*jac 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(); 

    ...
}
Run Code Online (Sandbox Code Playgroud)


Gov*_*ngh 7

你没有提到语言

在 Scala 中,您可以通过以下方式获取控制器中的路径

val path= request.path
Run Code Online (Sandbox Code Playgroud)

或者

val path=request.uri
Run Code Online (Sandbox Code Playgroud)

我在 Play Framework 2.2* 中尝试过这个


爪哇语

String path= request.path();
Run Code Online (Sandbox Code Playgroud)

或者

String path=request.url();
Run Code Online (Sandbox Code Playgroud)

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