1

我正在尝试javascriptRoutes在 Play 2 (Scala) 中使用,但出现错误(见下文)。这是我所做的:

向控制器添加javascriptRoutes方法Application

def javascriptRoutes = Action { implicit request =>
    import routes.javascript._
    Ok(Routes.javascriptRouter("jsRoutes")(Orders.searchProducts))
        .as("text/javascript")
}

将路由添加到routes文件

GET    /assets/javascripts/routes    controllers.Application.javascriptRoutes

添加<script>导入到main.scala.html

<head>
...
<script type="text/javascript" src="@routes.Application.javascriptRoutes"></script>
...
</head>

进行这些更改后,我在 JavaScript 控制台中收到以下错误:

GET http://localhost:9000/assets/javascripts/routes 404 (Not Found)
Uncaught ReferenceError: jsRoutes is not defined

我错过了什么?

4

2 回答 2

6

wrong order within the conf/routes file may cause the issue.

there is a hint:

http://grokbase.com/t/gg/play-framework/1348rbs2vk/2-1-scala-javascript-router-404

once I adjusted the order according to the hint:

Move the route definition of the javascript router action above the assets route.

the issue was fixed.

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

    GET     /                           controllers.MainController.index()
    GET     /message                    controllers.MessageController.getMessage()
    GET     /assets/javascripts/routes  controllers.MessageController.javascriptRoutes()

    # Map static resources from the /public folder to the /assets URL path
    GET     /assets/*file               controllers.Assets.at(path="/public", file)
    GET     /webjars/*file              controllers.WebJarAssets.at(file)`
于 2013-12-07T21:07:43.747 回答
1

与此同时,我发现了与此相关的另一篇文章->

无法解析 IntelliJ 中的反向路由方法

我必须从项目配置中删除一些信息才能使以下文件夹成为源路径的一部分->

文件 -> 项目结构

在右窗格中选择源

添加源文件夹 target/scala-XXX/classes_managed target/scala-XXX/src_managed/main

于 2014-08-19T18:36:03.610 回答