Play provides a feature to make HTTP calls using Scala methods. For every route defined, an equivalent Scala method is generated in the routes_ReverseRouting.scala
file. This is very convenient when making a request from within our Scala code, for example, within views such as the following:
@(tasks: List[Task], taskForm: Form[String]) @import helper._ @main("Task Tracker") { <h2>Task Tracker</h2> <div> @form(routes.TaskController.newTask) { @taskForm.globalError.map { error => <p class="error"> @error.message </p> } <form> <input type="text" name="taskName" placeholder="Add a new Task" required> <input type="submit" value="Add"> </form> } </div> <div> <ul> @tasks.map { task => <li> @form(routes.TaskController.deleteTask(task.id)) { @task.name <input type="submit" value="Remove"> } </li> } </ul> </div> }
The content of the routes_reverseRouting.scala
file would be similar to the following:
import Routes.{prefix => _prefix, defaultPrefix => _defaultPrefix} import play.core._ import play.core.Router._ import play.core.j._ import play.api.mvc._ import Router.queryString // @LINE:5 package controllers { // @LINE:5 class ReverseAppController { // @LINE:5 def index(): Call = { Call("GET", _prefix) } } } // @LINE:5 package controllers.javascript { // @LINE:5 class ReverseAppController { // @LINE:5 def index : JavascriptReverseRoute = JavascriptReverseRoute( "controllers.AppController.index", """ function() { return _wA({method:"GET", url:"""" + _prefix + """"}) } """ ) } } // @LINE:5 package controllers.ref { // @LINE:5 class ReverseAppController { // @LINE:5 def index(): play.api.mvc.HandlerRef[_] = new play.api.mvc.HandlerRef( controllers.AppController.index(), HandlerDef(this, "controllers.AppController", "index", Seq(), "GET", """ Routes This file defines all application routes (Higher priority routes first) ~~~~ Home page""", _prefix + """""") ) } }
The reverse routes return a call. A call describes an HTTP request and can be used to create links or fill and redirect data. It is defined as follows:
case class Call(method: String, url: String) extends play.mvc.Call { //Transform this call to an absolute URL. def absoluteURL(secure: Boolean = false)(implicit request: RequestHeader) = { "http" + (if (secure) "s" else "") + "://" + request.host + this.url } // Transform this call to an WebSocket URL. def webSocketURL(secure: Boolean = false)(implicit request: RequestHeader) = { "ws" + (if (secure) "s" else "") + "://" + request.host + this.url } override def toString = url }
In routes_reverseRouting.scala
, there is also a method that returns JavascriptReverseRoute
. We could use this in our JavaScript code when we wish to send a request. Prior to this, however, we would need to define a JavaScript router. We could do this by defining an action and then adding a route for it, as shown in this example:
def javascriptRoutes = Action { implicit request => Ok( Routes.javascriptRouter("jsRouter")( routes.javascript.index ) ).as("text/javascript") }
Then, we could include it in the routes file in this way:
GET /javascriptRoutes controllers.AppController.javascriptRoutes
Next, we could refer to it in our views as follows:
<script type="text/javascript" src="@routes.AppController.javascriptRoutes"></script>
Once this is done, in our JavaScript scripts we could use the router to send requests to the server, as follows:
jsRouter.controllers.AppController.index.ajax({ success: function(data) { console.log("redirect successful"); } , error:function(e){ console.log("something terrible happened" + e); } });
18.117.231.15