Table of content
1. Write routes
2. Create Sample Controller
3. Start server
4. “Hello World” redendering in browser
Download scaffold source code
Github
Open route file and and line bellow
# /sample-play-scala/conf/routes
GET /sample/helloworld controllers.SampleController.helloworld()
Create the SampleController class in the controllers package.
Right-click the controllers directory on IntelliJ and click New> Scala Class.
Enter [SampleController] in the dialog that appears and press Enter.
/sample-play-scala-/app/controllers/SampleController.scala
Copy the import part and write the rest
package controllers
// Copy import
import javax.inject._
import play.api.mvc._
import play.api.data._
import play.api.data.Forms._
// Write
class SampleController @Inject()(mcc: MessagesControllerComponents)
extends MessagesAbstractController(mcc) {
def helloworld() = Action {
implicit request: MessagesRequest[AnyContent] => Ok("Hello World")
}
}
Method | Status code |
---|---|
Ok | 200 |
BadRequest | 400 |
Forbidden | 403 |
NotFound | 404 |
Redirect | Redirect |
Execute the following command directly under the project to enter the sbt shell.
$ sbt
Start the server with the run command.
> run
It will be displayed in the following behavior.
--- (Running the application, auto-reloading is enabled) ---
[info] p.c.s.AkkaHttpServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
(Server started, use Enter to stop and go back to the console...)
Please click this link
If you see the following screen, congratulation!