0Pricing
Scala for Backend Engineering & Functional Programming · 课时

Play Framework 基础

开始使用 Play,了解其架构,并配置您的第一个 Web 应用程序。

Play Framework 基础 是 CoddyKit 上的免费 Scala for Backend Engineering & Functional Programming 课时。 这是第 1 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Scala for Backend Engineering & Functional Programming 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Scala for Backend Engineering & Functional Programming 课程共包含 3 节课。

本课时的部分内容尚未翻译,以英文显示。

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!

常见问题解答

「Play Framework 基础」课时是免费的吗?

是的 — 「Play Framework 基础」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Scala for Backend Engineering & Functional Programming 课程的其余内容,请升级到 CoddyKit PRO。 Scala for Backend Engineering & Functional Programming 课程共包含 3 节课。

「Play Framework 基础」这节课中我会学到什么?

开始使用 Play,了解其架构,并配置您的第一个 Web 应用程序。 你通过在浏览器中直接运行的动手代码来练习 Scala for Backend Engineering & Functional Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Scala for Backend Engineering & Functional Programming 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Scala for Backend Engineering & Functional Programming 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 3 节。

「Play Framework 基础」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Scala for Backend Engineering & Functional Programming 课中编写并运行代码吗?

能。每节 Scala for Backend Engineering & Functional Programming 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Play Framework 基础
  2. 使用 Play 构建 RESTful API
  3. 使用 Slick/Doobie 集成数据库
← 返回 Scala for Backend Engineering & Functional Programming