Friday, May 13, 2016

Working with static html files in Play

Play comes with its own template engine, which compiles the Play views of type *.html.scala into static html files.

But what if we need to use instead of a Play view a plain html file?
For this purpose Play has a build-in static assets controller.

Usage of plain html files from the Play application is done via routes configuration: as soon as a route to a particular html file will be defined in the routes file, it will be served by the assets controller.

The example below demonstrates, how to add a static html file into the application:
1. Create a folder html under the public folder of the Play project.
2. Create an html file and put into it the following content:
 <!DOCTYPE html>  
 <meta charset="utf-8">  
 <title>Example 1</title>  
      <h1>Static html file example 1</h1>  
      <p>This file resides in the public/html folder</p>  
      <p>It will be embedded into the jar</p>  
2. Save the file into the public/html folder as example1.html.
    The updated public folder structure should be like this:

3. Add into the routes file the entry:
 GET /example1"/public/html", file="example1.html")  
4. Run the application.
5. From a browser open the url http://localhost:9000/example1. The following page will appear:

Static files distribution

Upon building of a distribution package the whole public folder is packed into a dedicated assets jar. This jar is included into the distribution package.
So, the example1.html will be distributed inside the folder /public/html same to its physical location.

The distribution package may be build with one of the following ways:
- With activator dist command
- With the maven install, if the maven project was created according this earlier post.

The distribution package is prepared like a zip file in the target/universal folder under the project root directory.

Let's inspect the distributed assets.
1. Open the distribution zip file.
2. Open the jar.
3. Expand the lib directory inside the jar.
4. Find the jar ending with "-assets".
5. Open this jar - you should find the public folder with the whole content like it is in the source.

It is very easy to add static files to the Play application. Any file or folder, which was put under the public folder, is embedded into the application jar similar to its physical location.

All the sources on Git.


Ala Schneider said...

I am glad that it was useful for you. Thanks for the feed back.

ajay said...

Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..

About the author

My Photo
I trust only simple code and believe that code should be handsome. This is not a matter of technology, but professional approach, consolidated after years of software development. I enjoy to cause things working and feel very happy, when I manage to solve a problem.
Back to Top