[2.0] base url

579 views
Skip to first unread message

Ian Downard

unread,
Jun 26, 2012, 2:11:11 PM6/26/12
to play-fr...@googlegroups.com
Is it possible to obtain an application's base url from the Java Play API?  Right now, I'm hard coding the port in application.conf, and assuming the hostname as "localhost" to derive the base url as follows:

String baseurl = "localhost" + Play.current().configuration().getInt("application.port").get().toString();

In my application.conf, I have
application.port=7000


There must be a better way.

Thanks for any help.

biesior

unread,
Jun 26, 2012, 2:25:36 PM6/26/12
to play-fr...@googlegroups.com
You can just get the absolute URL to your Home Page action 

routes
# Home page
GET     /      controllers.Application.index()

Application.java:
String baseUrl = routes.Application.index().absoluteURL(request());

biesior

unread,
Jun 26, 2012, 2:54:20 PM6/26/12
to play-fr...@googlegroups.com
Of course you can also use this trick in the template if required:

    <head>
        <base href="@routes.Application.index().absoluteURL(request)" />
        <title>@title</title>
        <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
    </head>

Ian Downard

unread,
Jun 26, 2012, 3:32:36 PM6/26/12
to play-fr...@googlegroups.com
Thanks for your reply.  I'm doing this from a multi-threaded controller, and it's giving me the following error:

Exception in thread "Thread-406" java.lang.RuntimeException: There is no HTTP Context available from here.
    at play.mvc.Http$Context.current(Http.java:27)
    at play.mvc.Controller.request(Controller.java:28)
    at controllers.Application$ProcessWatcher.run(Application.java:328)

Ian Downard

unread,
Jun 28, 2012, 12:25:13 PM6/28/12
to play-fr...@googlegroups.com
I found my problem. I got this exception because I was trying to parse HTTP responses without properly getting an HTTP stream to hold those responses.  Fixed now.
Reply all
Reply to author
Forward
0 new messages