当前位置: 首页>>代码示例>>Scala>>正文


Scala HtmlFormat类代码示例

本文整理汇总了Scala中play.twirl.api.HtmlFormat的典型用法代码示例。如果您正苦于以下问题:Scala HtmlFormat类的具体用法?Scala HtmlFormat怎么用?Scala HtmlFormat使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了HtmlFormat类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: HelpController

//设置package包名称以及导入依赖的类
package controllers.help

import controllers.{ HasVisitLogging, WebJarAssets }
import javax.inject.{ Inject, Singleton }
import models.visit.VisitService
import play.api.mvc.{ Action, Controller, RequestHeader }
import play.twirl.api.HtmlFormat

@Singleton
class HelpController @Inject() (
    implicit val visits: VisitService,
    implicit val webjars: WebJarAssets
  ) extends Controller with HasVisitLogging {

  private def result(template: HtmlFormat.Appendable)(implicit request: RequestHeader) = {
    logPageView()
    Ok(template)
  }

  def index = Action { Redirect(routes.HelpController.showTutorial()) }

  def showTutorial  = Action { implicit request => result(views.html.help.tutorial()) }

  def showLocalizedTutorial(lang: String) = Action { implicit request =>
    lang.toUpperCase match {
      case "DE" => result(views.html.help.tutorial_de())
      case "ES" => result(views.html.help.tutorial_es())
      case "IT" => result(views.html.help.tutorial_it())
      case _ => NotFound(views.html.error404())
    }
  }

  def showFAQ = Action { implicit request => result(views.html.help.faq()) }

  def showAbout = Action { implicit request => result(views.html.help.about()) }

} 
开发者ID:pelagios,项目名称:recogito2,代码行数:38,代码来源:HelpController.scala

示例2: UserSocket

//设置package包名称以及导入依赖的类
package actors

import actors.UserSocket.{ChatMessage, Message}
import actors.UserSocket.Message.messageReads
import akka.actor.{Actor, ActorLogging, ActorRef, Props}
import akka.cluster.pubsub.DistributedPubSub
import akka.cluster.pubsub.DistributedPubSubMediator.{Publish, Subscribe}
import akka.event.LoggingReceive
import play.api.libs.json.{Writes, JsValue, Json}
import play.twirl.api.HtmlFormat

import scala.xml.Utility

object UserSocket {
  def props(user: String)(out: ActorRef) = Props(new UserSocket(user, out))

  case class Message(msg: String)

  object Message {
    implicit val messageReads = Json.reads[Message]
  }

  case class ChatMessage(user: String, text: String)

  object ChatMessage {
    implicit val chatMessageWrites = new Writes[ChatMessage] {
      def writes(chatMessage: ChatMessage): JsValue = {
        Json.obj(
          "type" -> "message",
          "user" -> chatMessage.user,
          "text" -> multiLine(chatMessage.text)
        )
      }
    }

    private def multiLine(text: String) = {
      HtmlFormat.raw(text).body.replace("\n", "<br/>")
    }
  }
}

class UserSocket(uid: String, out: ActorRef) extends Actor with ActorLogging {

  val topic = "chat"

  val mediator = DistributedPubSub(context.system).mediator

  mediator ! Subscribe(topic, self)

  def receive = LoggingReceive {
    case js: JsValue =>
      js.validate[Message](messageReads)
        .map(message => Utility.escape(message.msg))
        .foreach { msg => mediator ! Publish(topic, ChatMessage(uid, msg))}

    case c:ChatMessage => out ! Json.toJson(c)
  }
} 
开发者ID:onegrx,项目名称:playakkachat,代码行数:59,代码来源:UserSocket.scala

示例3: MyDeadboltHandler

//设置package包名称以及导入依赖的类
package handler

import be.objectify.deadbolt.scala.models.Subject
import be.objectify.deadbolt.scala.{ AuthenticatedRequest, DeadboltHandler, DynamicResourceHandler }
import com.google.inject.Inject
import play.api.mvc.{ Request, Result, Results }
import play.twirl.api.HtmlFormat
import play.api.i18n.{ I18nSupport, MessagesApi }

import daos.SubjectDao
import controllers.LoginController
import views.html.security.{ denied, login }

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

class MyDeadboltHandler @Inject() (
    val subjectDao: SubjectDao,
    val messagesApi: MessagesApi
) extends DeadboltHandler with I18nSupport {

  override def beforeAuthCheck[A](request: Request[A]): Future[Option[Result]] = Future { None }

  override def getDynamicResourceHandler[A](request: Request[A]): Future[Option[DynamicResourceHandler]] = Future { None }

  override def getSubject[A](request: AuthenticatedRequest[A]): Future[Option[Subject]] = {
    request.subject match {
      case s @ Some(_) ? Future(s)
      case None ? request.session.get("login") match {
        case Some(userId) ? Future(subjectDao.subjectByIdentifier(userId))
        case None         ? Future(None)
      }
    }
  }

  override def onAuthFailure[A](request: AuthenticatedRequest[A]): Future[Result] = {
    implicit val req = request
    def toContent(maybeSubject: Option[Subject]): (Boolean, HtmlFormat.Appendable) =
      maybeSubject.map(subject ? (true, denied(Some(subject))(messagesApi.preferred(request), request)))
        .getOrElse { (false, login(LoginController.loginForm, getRedirectUri(request))) }

    getSubject(request).map(maybeSubject ? toContent(maybeSubject))
      .map(subjectPresentAndContent ?
        if (subjectPresentAndContent._1) Results.Forbidden(subjectPresentAndContent._2)
        else Results.Unauthorized(subjectPresentAndContent._2))
  }

  private[this] def getRedirectUri[A](request: AuthenticatedRequest[A]): String =
    request.session.get("redirectUri").getOrElse("")
} 
开发者ID:kdoomsday,项目名称:kaminalapp,代码行数:51,代码来源:MyDeadboltHandler.scala

示例4: MyDeadboltHandler

//设置package包名称以及导入依赖的类
package handler

import be.objectify.deadbolt.scala.models.Subject
import be.objectify.deadbolt.scala.{AuthenticatedRequest, DeadboltHandler, DynamicResourceHandler}
import com.google.inject.Inject
import play.api.mvc.{Request, Result, Results}
import play.twirl.api.HtmlFormat
import play.api.i18n.{ I18nSupport, MessagesApi }

import daos.SubjectDao
import controllers.LoginController
import views.html.security.{denied, login}

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

class MyDeadboltHandler @Inject() (
  val subjectDao:  SubjectDao,
  val messagesApi: MessagesApi
) extends DeadboltHandler with I18nSupport {

  override def beforeAuthCheck[A](request: Request[A]): Future[Option[Result]] = Future {None}

  override def getDynamicResourceHandler[A](request: Request[A]): Future[Option[DynamicResourceHandler]] = Future {None}

  override def getSubject[A](request: AuthenticatedRequest[A]): Future[Option[Subject]] = {
    request.subject match {
      case s @ Some(_) => Future(s)
      case None => request.session.get("login") match {
        case Some(userId) => subjectDao.subjectByIdentifier(userId)
        case None         => Future(None)
      }
    }
  }

  override def onAuthFailure[A](request: AuthenticatedRequest[A]): Future[Result] = {
    def toContent(maybeSubject: Option[Subject]): (Boolean, HtmlFormat.Appendable) =
      maybeSubject.map(subject => ( true, denied(Some(subject))(messagesApi.preferred(request), request)) )
                  .getOrElse { (false, login(LoginController.loginForm, getRedirectUri(request))) }

    getSubject(request).map(maybeSubject => toContent(maybeSubject))
    .map(subjectPresentAndContent =>
      if (subjectPresentAndContent._1) Results.Forbidden(subjectPresentAndContent._2)
      else Results.Unauthorized(subjectPresentAndContent._2))
  }

  private[this] def getRedirectUri[A](request: AuthenticatedRequest[A]): String =
    request.session.get("redirectUri").getOrElse("")
} 
开发者ID:kdoomsday,项目名称:doomcart,代码行数:50,代码来源:MyDeadboltHandler.scala

示例5: SparkWebScalaHelpers

//设置package包名称以及导入依赖的类
package moe.pizza.sparkhelpers

import play.twirl.api.HtmlFormat
import spark._


object SparkWebScalaHelpers {
  implicit def stringlambda2route(l: (Request, Response) => String): Route = new Route {
    override def handle(request: Request, response: Response): String = l(request, response)
  }

  implicit def twirllambda2route(l: (Request, Response) => HtmlFormat.Appendable): Route = new Route {
    override def handle(request: Request, response: Response): String = l(request, response).toString()
  }

  implicit def lambda2modelroute(l: (Request, Response) => ModelAndView): TemplateViewRoute = new TemplateViewRoute {
    override def handle(request: Request, response: Response): ModelAndView = l(request, response)
  }

  implicit def unitlambda2filter(l: (Request, Response) => Unit): Filter = new Filter {
    override def handle(request: Request, response: Response): Unit = l(request, response)
  }

  implicit def unitlambda2route(l: (Request, Response) => Unit): Route = new Route {
    override def handle(request: Request, response: Response): AnyRef = { l(request, response); "" }
  }
} 
开发者ID:xxpizzaxx,项目名称:set.them.red,代码行数:28,代码来源:SparkWebScalaHelpers.scala

示例6: User

//设置package包名称以及导入依赖的类
package actors

import akka.actor.{Actor, ActorLogging, ActorRef, Props}
import play.api.libs.json.{JsValue, Json}
import play.twirl.api.HtmlFormat


object User {
  def props(room: ActorRef, out: ActorRef): Props =
    Props(classOf[User], room, out)
}

class User(room: ActorRef, out: ActorRef) extends Actor with ActorLogging {

  override def preStart(): Unit =
    room ! Subscribe // subscribe to the room on start

  override def postStop(): Unit =
    room ! Unsubscribe // unsubscribe to the room when the actor stops

  def receive: Receive = {
    // A JSON from the websocket connection
    case json: JsValue => {
      json.asOpt[Message] match {
        case Some(msg) => {
          val sanitisedText = sanitise(msg.text)
          room ! msg.copy(sanitisedText)
        }
        case _ => log.error(s"Unanticipated JSON: $json")
      }
    }
    // A message from the chatroom
    case msg: Message => out ! Json.toJson(msg)
  }

  private def sanitise(text: String): String = {
    HtmlFormat.escape(text).toString
  }
} 
开发者ID:lloydmeta,项目名称:play-chatroom,代码行数:40,代码来源:User.scala

示例7: ChatMessage

//设置package包名称以及导入依赖的类
package actors

import play.api.libs.json.{JsValue, Json, Writes}
import play.twirl.api.HtmlFormat



case class ChatMessage(topic: String, user: String, text: String, created: java.util.Date)

  object ChatMessage {
    implicit val chatMessageWrites = new Writes[ChatMessage] {
      def writes(chatMessage: ChatMessage): JsValue = {
        Json.obj(
          "type"  -> "message",
          "topic" -> chatMessage.topic,
          "user"  -> chatMessage.user,
          "text"  -> multiLine(chatMessage.text)
        )
      }
    }

    private def multiLine(text: String) = {
      HtmlFormat.raw(text).body.replace("\n", "<br/>")
    }
  } 
开发者ID:Bzaad,项目名称:play-akka-cluster,代码行数:26,代码来源:ChatMassage.scala

示例8: ErrorHandler

//设置package包名称以及导入依赖的类
package dcos.metronome.api

import org.slf4j.LoggerFactory
import play.api.http.HttpErrorHandler
import play.api.http.Status._
import play.api.libs.json.Json
import play.api.mvc.{ RequestHeader, Result, Results }
import play.twirl.api.HtmlFormat

import scala.concurrent.Future

class ErrorHandler extends HttpErrorHandler {

  private[this] val log = LoggerFactory.getLogger(getClass)

  override def onClientError(request: RequestHeader, statusCode: Int, message: String): Future[Result] = {
    log.debug(s"Client Error on path ${request.path}. Message: $message Status: $statusCode")
    val json = Json.obj("message" -> escape(message), "requestPath" -> escape(request.path))
    Future.successful(Results.Status(statusCode)(json))
  }

  override def onServerError(request: RequestHeader, exception: Throwable): Future[Result] = {
    log.error(s"Error serving ${request.path}", exception)
    val json = Json.obj("requestPath" -> escape(request.path))
    Future.successful(Results.Status(INTERNAL_SERVER_ERROR)(json))
  }

  
  private def escape(msg: String): String = HtmlFormat.escape(msg).body
} 
开发者ID:dcos,项目名称:metronome,代码行数:31,代码来源:ErrorHandler.scala


注:本文中的play.twirl.api.HtmlFormat类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。