Cyril N.
Cyril N.

Reputation: 39859

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

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.

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 !

Upvotes: 16

Views: 11663

Answers (2)

biesior
biesior

Reputation: 55798

Add request to absoluteURL()

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

Upvotes: 26

Alex
Alex

Reputation: 140

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.

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.

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

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

Upvotes: 2

Related Questions