How to call a Scala function from html game template

I am new to Scala / Play Framework.

I am currently trying to call a Scala function on my html: test.scala.html page and pass the hash parameters to the Scala function.

I added the following routes to routes:

GET   /hello                      controllers.Application.test
POST  /hello                      controllers.Application.hello

      

In my test.scala.html I have:

@params = { window.location.hash }
@helper.form(action = routes.Application.hello) {

}

      

And my hello function is defined as:

 def hello() = Action {
   Ok("Hello !")
 }

      

I'm completely confused about the concept of routing and @, so I'm not too sure which part I did right and which part I did wrong. Please point out my mistakes.

Thanks in advance.

+3


source to share


2 answers


If the function returns an action and not content to be rendered in HTML format (HTML format), you can submit a request for that action by clicking a link or submit a form to the URL configured in routing (aka /hello

).

To add a parameter, you either need to add it as a url query string (for example, for link -> /hello?p=1

), or using an input / field for a form (for example <input type="text" name="p" value="1" />

).



You can use reverse routing to make the URL invoke the configured action. For example, to POST the form to hello

: <form action="@routes.MyController.hello()" method="POST">...

. In this case, you will need to look at the display of the form, extract parameters from the request.

0


source


1) Concept of routing

The main purpose of this routing is to simply translate every incoming HTTP Action request to any of your controllers. It Reverse Routing

just lets you use the right side controllers.Application.hello

in your HTML / Controller / else.

So, for your 2 urls it will probably say that if there is a request /hello

with a method GET

, it will go to the controller Application

and test

. If you do not understand the role of each routing method, read this documentation .

2) magic symbol @

@

is a symbol that you can use in your HTML file if you need to use code or variables Scala

. It's like you can combine PHP

code in your HTML file if you are a PHP developer.



Here is complete documentation on what you can do with this symbol @

.

3) pass the hash to the controller

In this particular case, the simplest way would be to pass the value through the form:

@helper.form(action = routes.Application.hello) {
    @helper.inputText(myForm("username"), 'id -> "username", 'size -> 30, 'value -> 'value here' )
}

      

However, if you are a new Play developer, then I'm afraid you need to read about Form Submission and the Play Framework Form Assistant .

0


source







All Articles