Play 2.5 using dependency injection in template


#1

I upgraded to Play 2.5 and moving everything to dependency injection. The class UiCommonService is now service, that is injectable, it used to be static.

This is how the the controller looks like.

public Result index() {
	if (CurrentUserService.isAuthenticated()) {
		return ok(mainPage.render(CurrentUserService.getUserStateVariables(),
				CurrentUserService.getCurrentUser(),
				configService.getGoogleAnalyticsKey()));
	} else {
		return redirect("/login");
	}
}

This is how my scala template looks like, it is using the @import command to use the static method from class.

@(variables: String, authorisedUser: models.security.UserSession, gaKey: String)

@import services.ui.UiCommonService

@layout.masterPage(UiCommonService.getSimpleHeaderAttributes(), styles){
	// my custom layout code
}

I read that to use dependency injection I have to include it with @this attribute.

@this(uiCommonService: services.ui.UiCommonService)

@(variables: String, authorisedUser: models.security.UserSession, gaKey: String)

@layout.masterPage(uiCommonService.getSimpleHeaderAttributes(), styles){
	// my custom layout code
}

But now I getting error at my controller controller.index() method.

mainPage.scala.html:1: too many arguments for method apply: ()play.twirl.api.HtmlFormat.Appendable in class mainPage