如何从 Play 2 Java 上的路由反向生成绝对 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11158750/
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-10-31 04:01:10  来源:igfitidea点击:

How to reverse generate an absolute URL from a route on Play 2 Java?

javaurlplayframeworkplayframework-2.0

提问by Cyril N.

I'd like to get the absolute URL from a controller in Play 2 Java. I found the exact same question for Scala, but I can't make it work in Java.

我想从 Play 2 Java 中的控制器获取绝对 URL。我为 Scala找到了完全相同的问题,但我无法在 Java 中使用它。

public class MyController extends Controller {
    public static Result myMethod() {
        return ok();
    }

    public static Result test() {
        Logger.info(routes.MyController.myMethod().url); // Doesn't work !
        Logger.info(routes.MyController.myMethod().absoluteURL()); // Doesn't work !
        Logger.info(routes.MyController.myMethod().absoluteURL(true)); // Doesn't work !
        return ok();
    }
}

Thanks for your help !

谢谢你的帮助 !

回答by biesior

Add request to absoluteURL()

添加请求到 absoluteURL()

routes.MyController.myMethod().absoluteURL(request());

回答by Alex

I'm not sure if this works in 2.0, but since you're using Java it might do the trick. I use it in 1.2.4.

我不确定这是否适用于 2.0,但由于您使用的是 Java,它可能会奏效。我在 1.2.4 中使用它。

Router.getFullUrl("Controller.action")

Good luck !

祝你好运 !

Edit : I import play.mvc.Router so if this doesn't exist in 2.0 you might find something similar.

编辑:我导入 play.mvc.Router 所以如果这在 2.0 中不存在,你可能会发现类似的东西。

Also, this is play's 2.0 documentation on routing, check Reverse routing, maybe it will help.

另外,这是关于路由的 play 2.0 文档,检查反向路由,也许会有所帮助。

http://www.playframework.org/documentation/2.0.1/JavaRouting

http://www.playframework.org/documentation/2.0.1/JavaRouting