Ashish Khatkar
Ashish Khatkar

Reputation: 104

How to serve same static .html file for all dynamic urls using Play framework and Java?


I am trying to serve a static html file let's call it index.html for all dynamic urls like /tachyon/someId. This someId is generated dynamically. I have tried multiple ways to do this but all failed. This is what all I have tried.

It would be better if there is a way to do this through controller and returning Result from method in task helper class to task since I am returning Promise<Result> from method in task class.

Upvotes: 1

Views: 291

Answers (2)

Ashish Khatkar
Ashish Khatkar

Reputation: 104

I got it working. Not sure if it is the correct solution or not. I rendered the index.html by converting it to a byte array and then using ok to render using ByteArrayInputStream. Something like this.

File file = Play.getFile("path to the file", Play.current());
byte[] byteArray = IOUtils.toByteArray(new FileInputStream(file));
return ok(new ByteArrayInputStream(byteArray)).as("text/html");

Let me know if there is any better way to do this without using scala templating.

Thanks

Upvotes: 0

Haijin
Haijin

Reputation: 2681

I think you can use Twirl to generate the page. Since you want a static html, you can ignore the parameter in the body.

so in routes, add:

GET /tachyon/*someId   somecontroller.index(someId)

in the controller's index function, you can return

Ok(views.html.somepage(someId))

And you create a somepage.scala.html Twirl function in views folder, but do not use someId in the body.

Upvotes: 1

Related Questions