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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24179874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:39:05  来源:igfitidea点击:

Play Framework 2.2 : Get URL of the requesting page

javaplayframeworkplayframework-2.2

提问by Incpetor

PLAY FRAMEWORK JAVA:

玩框架Java:

I am trying to get the name of the URL that requested the controller function. For example , I have a routes as

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

GET /mypage controllers.Mypage.myfunction()

GET /mypage controllers.Mypage.myfunction()

and I have another page that requests the same controller

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

GET /anotherpage controllers.Mypage.myfunction()

GET /anotherpage controllers.Mypage.myfunction()

is there a way to find in controllers if the request is from /mypageor from /anotherpage?

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

Thanks

谢谢

采纳答案by Michael Zajac

Say you visit example.com:9000/login?param=test, then within your controller function:

假设您访问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(); 

    ...
}

回答by Govind Singh

you didn't mention language

你没有提到语言

In Scala you can get path in controller by

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

val path= request.path

or

或者

val path=request.uri

i have tried this in play framework 2.2*

我已经在 play framework 2.2* 中尝试过这个



In Java

在 Java 中

String path= request.path();

or

或者

String path=request.url();

according to this link http://www.playframework.com/documentation/1.0.1/api/play/mvc/Http.Request.html

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