0Pricing
Scala for Backend Engineering & Functional Programming · Lektion

Grundlagen des Play Frameworks

Machen Sie sich mit Play vertraut, verstehen Sie seine Architektur und richten Sie Ihre erste Webanwendung ein.

Grundlagen des Play Frameworks ist eine kostenlose Scala for Backend Engineering & Functional Programming-Lektion auf CoddyKit. Dies ist Lektion 1 von 3. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Scala for Backend Engineering & Functional Programming-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Scala for Backend Engineering & Functional Programming-Kurs umfasst insgesamt 3 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

Intro to Play Framework

Welcome to the Play Framework! Play is a powerful open-source web application framework for Scala and Java.

It's designed for building highly scalable, modern web applications and APIs, emphasizing developer productivity and performance.

Reactive & Asynchronous Core

Play is built on a reactive foundation, leveraging Akka underneath. This means it handles requests asynchronously and is non-blocking by default.

  • Reactive: Efficiently handles many concurrent requests.
  • Asynchronous: Operations don't block the server thread.
  • Stateless: Designed for horizontal scaling, making it great for microservices.

Create a Play Project

To start a new Play project, you'll use SBT (Scala Build Tool) with a Play seed template. Make sure you have SBT installed.

Run this command in your terminal:

sbt new playframework/play-scala-seed.g8

This command creates a new project directory with a basic Play Scala application structure.

Play Project Layout

A Play project has a well-defined directory structure:

  • app/: Contains your Scala source code (controllers, models, views).
  • conf/: Configuration files like routes (URL mapping) and application.conf.
  • public/: Static assets (JavaScript, CSS, images).
  • project/: SBT build definition files (e.g., build.sbt).
  • test/: Your application's tests.

Mapping URLs with `routes`

The conf/routes file is crucial. It defines how HTTP requests (method + path) are mapped to specific controller actions in your application.

Each line specifies an HTTP method, a URL path, and the controller method to call.

# conf/routes
GET     /               controllers.HomeController.index()
GET     /hello/:name    controllers.HelloController.sayHello(name: String)

Creating Play Controllers

Controllers are Scala objects (or classes) that handle incoming HTTP requests. They contain action functions that process requests and return results.

Your controllers typically extend BaseController and are injected with ControllerComponents.

package controllers

import play.api.mvc._
import javax.inject._

@Singleton
class HomeController @Inject()(val controllerComponents: ControllerComponents) extends BaseController {
  // Action functions go here
}

Defining Action Logic

An action function is the core logic within a controller. It takes an implicit Request and returns a Result (e.g., Ok, NotFound, Redirect).

The Action { ... } block wraps your code, making it a valid Play action.

// Inside HomeController
def sayHello(name: String) = Action { implicit request =>
  Ok(s"Hello, $name! Welcome to Play.")
}

Dynamic HTML with Twirl

Play uses Twirl templates (.scala.html files) to generate dynamic HTML responses. They allow you to embed Scala code directly into your HTML.

Templates are type-safe and compiled, catching errors early.

@* app/views/welcome.scala.html *@
@(userName: String)

<!DOCTYPE html>
<html>
<head>
    <title>Welcome</title>
</head>
<body>
    <h1>Welcome, @userName!</h1>
    <p>This page was generated by a Play template.</p>
</body>
</html>

Starting Your Play App

Once your project is set up and you've defined some routes and controllers, you can run your application using SBT.

Navigate to your project's root directory in the terminal and execute:

sbt run

The application will typically start on http://localhost:9000.

Play Components Quiz

Let's check your understanding of Play Framework fundamentals.

Recap: Play Fundamentals

You've taken your first steps with Play Framework! We covered:

  • What Play is and its reactive nature.
  • How to set up a new project with SBT.
  • The key project directories.
  • The role of conf/routes for URL mapping.
  • How to create controllers and action functions.
  • Using Twirl templates for dynamic views.
  • Running your Play application.

Next, you'll learn to build RESTful APIs with Play!

Häufig gestellte Fragen

Ist die Lektion „Grundlagen des Play Frameworks“ kostenlos?

Ja — der vollständige Text von „Grundlagen des Play Frameworks“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Scala for Backend Engineering & Functional Programming-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Scala for Backend Engineering & Functional Programming-Kurs umfasst insgesamt 3 Lektionen.

Was lerne ich in „Grundlagen des Play Frameworks“?

Machen Sie sich mit Play vertraut, verstehen Sie seine Architektur und richten Sie Ihre erste Webanwendung ein. Du übst Scala for Backend Engineering & Functional Programming mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Scala for Backend Engineering & Functional Programming zu starten?

Keine Vorkenntnisse erforderlich. Scala for Backend Engineering & Functional Programming auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 3.

Wie lange dauert die Lektion „Grundlagen des Play Frameworks“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Scala for Backend Engineering & Functional Programming-Lektion Code schreiben und ausführen?

Ja. Jede Scala for Backend Engineering & Functional Programming-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Grundlagen des Play Frameworks
  2. RESTful APIs mit Play entwickeln
  3. Datenbankintegration mit Slick/Doobie
← Zurück zu Scala for Backend Engineering & Functional Programming