首页  

play action 各种写法     所属分类 play 浏览量 217
package controllers

import akka.util.ByteString
import play.api.http.HttpEntity
import play.api.mvc._
import java.time.LocalDateTime
import javax.inject._


@Singleton
class HelloController @Inject()(cc: ControllerComponents) extends AbstractController(cc) {

  def hello() = Action { implicit request: Request[AnyContent] =>
    var name = request.getQueryString("name").getOrElse(null);
    if (name == null) {
      name = "scala"
    }
    Ok("hello " + name + "," + LocalDateTime.now())
  }

  def sayHello() = Action {

    implicit request: Request[AnyContent] => {
      var name = request.getQueryString("name").getOrElse(null);
      if (name == null) {
        name = "scala"
      }
      Ok("say hello to " + name + "," + LocalDateTime.now())
    }

  }

  def helloNotImplemented() = Action {
    // 501
    NotImplemented
  }

  def helloRequestTimeout() = Action {
    // 408
    RequestTimeout
  }

  def helloOK() = Action.apply({
    Ok("hello ok," + LocalDateTime.now())
  })

  // 展示内置的TODO页面
  // Action not implemented yet.
  def todo() = TODO

  def redirectToHome() = Action {
    // Status Code: 303 See Other
    Redirect("/")
  }

  def redirectToHome2() = Action {
    // Status Code: 301 Moved Permanently (from disk cache)
    Redirect("/", MOVED_PERMANENTLY)
  }

  def helloWorld = Action {
    Result(
      header = ResponseHeader(200, Map.empty),
      body = HttpEntity.Strict(ByteString("Hello world!"), Some("text/plain"))
    )
  }
  
}


https://gitee.com/dyyx/hellocode/blob/master/demo/scala/play-rest-hello/app/controllers/HelloController.scala

上一篇     下一篇
play框架web编程实例

scala 大括号省略

scala 表达式/代码块 当参数

Scala函数调用省略点号和括号

Scala 闭包 和 柯里化

scala 函数参数的求值策略