Play Framework in Scala

Play Framework in Scala


The Play Framework is a web framework for building web applications in Scala. It provides a simple and lightweight approach to building web applications using a combination of MVC patterns, functional programming, and asynchronous IO. Here's an example of defining a controller and routing in Play:

java

Copy codepackage controllers


import javax.inject._

import play.api.mvc._


@Singleton

class HomeController @Inject()(val controllerComponents: ControllerComponents) extends BaseController {

  def index() = Action { implicit request: Request[AnyContent] =>

    Ok("Hello, world!")

  }

}


// routes file

GET     /               controllers.HomeController.index()

In this example, a HomeController class is defined that extends the BaseController trait and has an index method that returns an Action that simply returns the string "Hello, world!". The HomeController is also annotated with @Singleton and has a constructor that takes a ControllerComponents parameter. Finally, a routing file is defined that maps the / route to the index method of the HomeController.

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...