How to add an application to the Play Framework 2.5 template

With the new version 2.4 / 2.5 of the Play Framework, they have moved on, injecting everything and removing the state of the servers. now out of date. However, I need an application in my template (for example, so that all supported languages ​​are displayed on all pages c ). play.Play.application()play.i18n.Lang.availables(play.Play.application())

I know I could:

  • Skip play.Applicationexplicitly to all my templates.
  • Add an implicit parameter to my template, for example @()(implicit app: play.Application). However, in my Java project this is not completely implicit, I have to pass it every time I create a template.
  • Create a Scala object that provides the application implicitly . However, this also requires outdated play.api.Play.current.

How can I embed play.Applicationin my templates?

---- Update: ----

What I have tried so far, I created the following setting:

index.scala.html:

@(title: String)
@template(title) { //here is the play.Application obviously missing, however I don't want to pass it in every template - even worse in every Controller <-- this is the core of my question
    Welcome to my page!
}

template.scala.html:

@(title: String)(content: Html)(implicit app: play.Application)
<html>
    <head>
        <title>@title</title>
    </head>
    <body>
        <p>Is live? @app.isProd</p>
        @content
    </body>
</html>

Controller function:

public Result index() {
    return ok(views.html.index.render("home"));
}
+4
source share
4 answers

I just needed to learn this for the Play 2.6.x platform. You can add an object to the template in accordance with the documentation: https://www.playframework.com/documentation/2.6.x/ScalaTemplatesDependencyInjection .

I implemented a simple example (a bit far-fetched), and I used scala:

test.scala.html:

@this(configuration: play.api.Configuration)
@(key: String)
config = @configuration.get[Seq[String]](key).mkString(", ")

HomeController.scala

package controllers

import javax.inject._

import play.api._
import play.api.i18n._
import play.api.mvc._

/**
 * This controller creates an `Action` to handle HTTP requests to the
 * application home page.
 */
@Singleton
class HomeController @Inject()(cc: ControllerComponents, testView: views.html.test) (implicit configuration: Configuration) extends AbstractController(cc) with I18nSupport{

  def test() = Action  { implicit request =>
    Ok(testView("play.i18n.langs"))
  }
}

routes:

GET        /test                controllers.HomeController.test()

views.html.test, . @this(configuration: play.api.Configuration) . Play , Configuration.

, . , ... , , , , , implicit configuration: play.api.Configuration , : @(message: String)(implicit messagesProvider: MessagesProvider, configuration: play.api.Configuration) . , , , .

+1

Play.current() , question. play.Application.

- .

, , injectappexample.scala.html:

@(app: play.Application)
.... use the app object 

:

public class SomeController extends Controller {
    Provider<Application> applicationProvider;

    @Inject 
    public SomeController(Provider<Application> applicationProvider) {
        this.applicationProvider = applicationProvider;
    }

    public Result injectAppExample() {
        return ok(injectappexample.render(applicationProvider.get());
    }
}

. , , . .

:

@(value: String)
.... use the value 

:

public class SomeController extends Controller {
    Configuration configuration;

    @Inject 
    public SomeController(Configuration configuration) {
        this.configuration = configuration;
    }

    public Result injectAppExample() {
        return ok(injectappexample.render(configuration.getString("SOME_PROPERTY"));
    }
}
+1

, . , , .

, , - , , .

0

, , , , .

- 2.5. , .

import com.google.inject.Inject;
import play.Configuration;

public class MyClass{
    private Configuration conf;

    @Inject
    public MyClass(Configuration conf){
        this.conf = conf;
    }

}

Now you have your own configuration class. Then, specifically for your requirement, posted in your comment, you can do this.

 List<Object> langList = conf.getList("play.i18n.langs");  
 //conf.getList("") returns an object array. 
 //You can cast to Strings (assuming you are using Java8).

 List<String> languages = new ArrayList<>(langList.size());
 for (Object object : langList) {
    languages.add(Objects.toString(object, null));
 }

Now you can have a list of your languages ​​in languages.

0
source

Source: https://habr.com/ru/post/1653202/


All Articles