Play Framework 2.4.x module/sub-project assets

As I was working on creating a Play Module, I wanted to make reference of the module assets though the routes. This is so the new module can expose its own assets. I tried to follow the Play 2.4 SBTSubProjects instructions and wasn’t able to get the routing working correctly. I decided to inspect the code generated by sbt until I was able to get it working correctly.

Following the How to create module with Play Framework instructions from the previous post, all you need to do is create an Assets class inside of your module controller package and make reference of it from the module routing.

Your module’s Assets class would look something like this:

package sample_module.controllers;

import com.google.inject.Inject;
import play.api.mvc.Action;
import play.api.mvc.AnyContent;

/**
 * Created by lcamilo on 7/26/15.
 */
public class Assets {
    @Inject
    controllers.Assets assets;
    public Action<AnyContent> versioned(String path, controllers.Assets.Asset file) {
        return assets.versioned(path, file);
    }
}

Your module routing file would look something like this:

# module/app/conf/sample_module.routes
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET     /sample_module/             @sample_module.controllers.Application.index()

# Map static resources from the /public folder to the /assets URL path
GET     /sample_module/*file        @sample_module.controllers.Assets.versioned(path="/public/lib/sample_module", file)

Now just include the sub-module routing file from the base project

# app/conf/routes
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

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

#Add sample_module routes
->         /                           sample_module.Routes

# Map static resources from the /public folder to the /assets URL path
GET        /assets/*file               controllers.Assets.versioned(path="/public", file: Asset)

If you followed the previous post of How to create module with Play

Framework your folder structure would look something like :

├── app
│   ├── controllers
│   │   └── Application.java
│   └── views
│       ├── index.scala.html
│       └── main.scala.html
├── conf
│   ├── application.conf
│   ├── logback.xml
│   └── routes
├── module 
│   ├── app
│   │   └── sample_module
│   │       ├── controllers
│   │       └── views
│   ├── public
│   │   ├── images
│   │   ├── javascripts
│   │   └── stylesheets
│   ├── ...
├── ...

 

Now lets see how we use the assets from the module views :

From your views you can make reference of the newly added routes :

#file : module/app/sample_module/views/sample_module_main.scala.html
@(title: String)(content: Html)

<!DOCTYPE html>

<html lang="en">
    <head>
        <title>@title</title>
        <link rel="stylesheet" media="screen" href="@sample_module.controllers.routes.Assets.versioned("stylesheets/main.css")">
        <link rel="shortcut icon" type="image/png" href="@sample_module.controllers.routes.Assets.versioned("images/favicon.png")">
        <script src="@sample_module.controllers.routes.Assets.versioned("javascripts/hello.js")" type="text/javascript"></script>
    </head>
    <body>
        @content
    </body>
</html>

The source code for the play-module-sample can be found here.

lcamilo15
3
  1. studyfordream

    hello ,first of all , thank you very much. i read your blogs and i make the subproject work right. my project structure has root project and three subprojects :A , B and C. which i though is i want to make C to be common projects which A and B can reference UserModel or some method in C.

    whether my thought is right , i try many times ,but failed.

    You are my only hope, can you tell me how to do it, any advices will be appreciated , thank you very much.

    Like

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: