Can't get javascriptRoutes to work with Play F

2019-04-15 09:44发布

I'm trying to use javascriptRoutes in Play 2 (Scala) and I am getting an error (see below). Here is what I did:

Add javascriptRoutes method to Application controller

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

Add route to routes file

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

Add <script> import to main.scala.html

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

With these changes in place I am getting the following error in the JavaScript console:

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

What am I missing?

2条回答
再贱就再见
2楼-- · 2019-04-15 10:17

In the meantime I have found an other post related to this ->

Unable to resolve reverse routing methods in IntelliJ

I had to remove some info from the project config to get the following folders to become part of source route ->

File -> Project Structure

Select Sources in Right Pane

Add Source folder target/scala-XXX/classes_managed target/scala-XXX/src_managed/main

查看更多
仙女界的扛把子
3楼-- · 2019-04-15 10:27

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)`
查看更多
登录 后发表回答