Hello World for Play Scala Framework

Hello World

Table of content
1. Write routes
2. Create Sample Controller
3. Start server
4. “Hello World” redendering in browser

1)Write routes

Download scaffold source code
Github

Open route file and and line bellow

# /sample-play-scala/conf/routes 
GET     /sample/helloworld    controllers.SampleController.helloworld()

2)Creating SampleController

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

3)Start the server

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...)

4) “Hello World” redendering in browser

Please click this link

Hello World View

If you see the following screen, congratulation!