如何从PlayFramework 2.5.x调用java方法

kxe2p93d  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(320)

我有一个javascript文件 morris.js 我必须调用一些java方法来检索 map 或者 list 结果。我不能使用reverserouting或controllers方法,因为我必须返回 Result 在控制器中键入。
我怎样才能做到这一点?
预期结果示例:

$.ajax(some method)
      .done(some treatment)
      .fail(console.log('not ok'));

// data stolen from http://howmanyleft.co.uk/vehicle/jaguar_'e'_type

var tax_data = [
   {"period": "2013", "licensed": 400, "sorned": 550},
   {"period": "2012", "licensed": 450, "sorned": 400},
   {"period": "2011", "licensed": 350, "sorned": 550},
   {"period": "2010", "licensed": 500, "sorned": 700},
   {"period": "2009", "licensed": 250, "sorned": 380},
   {"period": "2008", "licensed": 350, "sorned": 240},
   {"period": "2007", "licensed": 180, "sorned": 300},
   {"period": "2006", "licensed": 300, "sorned": 250},
   {"period": "2005", "licensed": 200, "sorned": 150}
];

我在控制器中有一个带参数的方法。如何将其添加到javascript路由器:

public Result getYearUploadedVideos(Long userId){
    // ...
}

我的路由器:

public Result jsRoutes() {
    return ok(play.routing.JavaScriptReverseRouter.create("jsRoutes",
              routes.javascript.Signup.forgotPassword(),
              routes.javascript.AdminManagement.contact(),
              routes.javascript.ProfilManagement.view(),
              routes.javascript.DashboardManagement.getYearUploadedVideos()))
        .as("text/javascript");
}

... 但它不起作用。有什么帮助吗?

okxuctiv

okxuctiv1#

其实很简单:

public Result jsRoutes() {
    return ok(
            play.routing.JavaScriptReverseRouter.create("jsRoutes",
                    routes.javascript.Signup.forgotPassword(),
                    routes.javascript.DashboardManagement.getSmartCasterSharedDonutStatDataJson(),
                    routes.javascript.DashboardManagement.getSmartCasterActionStatDataJson(),
                    routes.javascript.DashboardManagement.getUploadedPublishedVideoStatDataJson(),
                    routes.javascript.DashboardManagement.getVideoSharedDonutStatDataJson(),
                    routes.javascript.DashboardManagement.getVideoActionStatDataJson()))
            .as("text/javascript");

}

然后:

$.ajax(jsRoutes.controllers.DashboardManagement.getVideoSharedDonutStatDataJson(id))
    .done(function(donut){ 
        console.log(JSON.stringify(donut));
...});

我们没有在反向js路由中配置id

相关问题