Joe
Joe

Reputation: 13111

Java Play - how to make a simple form with 2 fields and send values to Controler?

I have a simple route '/firstlast' and I want to display web page 'firstlast.scala.html' with 2 fields and submit button.

How to call that 'firstlast.scala.html' file from a route?

And how to transfer values entered in a form to some controler (some method)?

Upvotes: 1

Views: 54

Answers (1)

Rich Dougherty
Rich Dougherty

Reputation: 3251

To display the file, you make a controller and call the controller from the routes file. The methods in the controller will call the template.

You'll want to make two controller methods one to handle a GET request and one to handle a POST request.

Routing GET and POST requests to controllers

How to inject a template into a controller and call it

The GET request is called first. It shows the form with empty values. For the GET the controller makes an empty form model object and passes the form to the template.

How to put default values into the form object on the GET request

When the user submits the form it sends a POST request. The controller for the POST request validates the request body and either process it or, if there's an error, passes it to the template.

How to validate the form on a POST request

Upvotes: 2

Related Questions