代码之家  ›  专栏  ›  技术社区  ›  Jaanus

重头戏2.5在模板中使用依赖注入

  •  0
  • Jaanus  · 技术社区  · 6 年前

    UiCommonService 现在是服务,也就是说是可注入的,以前是静态的。

    这就是控制器的样子。

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

    @import

    @(variables: String, authorisedUser: models.security.UserSession, gaKey: String)
    
    @import services.ui.UiCommonService
    
    @layout.masterPage(UiCommonService.getSimpleHeaderAttributes(), styles){
        // my custom layout code
    }
    

    我读到要使用依赖注入,我必须将它与@this属性一起包含。

    @this(uiCommonService: services.ui.UiCommonService)
    
    @(variables: String, authorisedUser: models.security.UserSession, gaKey: String)
    
    @layout.masterPage(uiCommonService.getSimpleHeaderAttributes(), styles){
        // my custom layout code
    }
    

    但现在我发现我的控制器出错了 controller.index() 方法。

    mainPage.scala.html:1: too many arguments for method apply: ()play.twirl.api.HtmlFormat.Appendable in class mainPage
    
    0 回复  |  直到 6 年前
    推荐文章