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?
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)`
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
来源:https://stackoverflow.com/questions/14905392/cant-get-javascriptroutes-to-work-with-play-framework-2