本文整理汇总了Scala中play.api.mvc._类的典型用法代码示例。如果您正苦于以下问题:Scala _类的具体用法?Scala _怎么用?Scala _使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了_类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: LoginController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject._
import models.{LoginData, UserData}
import play.api.cache._
import play.api.data.Forms._
import play.api.data._
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.mvc.{Action, _}
import services.ImpConfService
import services.MD5
@Singleton
class LoginController @Inject()(cache: CacheApi,statusService: ImpConfService)(implicit val messagesApi: MessagesApi) extends Controller with I18nSupport {
val loginForm = Form(
mapping(
"name" -> text,
"pass"-> text
)(LoginData.apply)(LoginData.unapply)
)
def index = Action { implicit request =>
Ok(views.html.login())
}
def login = Action{ implicit request=>
loginForm.bindFromRequest.fold(
errorForm=>{
BadRequest(views.html.login())
},
validForm=> {
val user = cache.get[UserData](validForm.name)
user match {
case Some(UserData(name,fname,mname,lname,age,pass,mobile,gender,hobbies,status,isSuspended))=>if(isSuspended=="true") Redirect(routes.LoginController.index()).flashing("success" -> "you has been blocked ")
else {
if(pass== MD5.hash(validForm.pass)) Redirect(routes.ProfileController.index())withSession (request.session + ("mySession" -> s"${validForm.name}"))
else Redirect(routes.LoginController.index()).flashing("success" -> "please enter correct password")}
case None=> Redirect(routes.LoginController.index()).flashing("success" -> "you are not valid user")
}
}
)
}
}
示例2: I019_datetime_constructor_Spec
//设置package包名称以及导入依赖的类
import org.scalatestplus.play._
import play.api.mvc.{AnyContentAsEmpty, _}
import play.api.test.{FakeHeaders, _}
import play.api.test.Helpers._
import play.api.libs.json._
class I019_datetime_constructor_Spec extends PlaySpec with OneAppPerSuite {
val exptectedContentType = "application/json"
val headers = FakeHeaders(Seq("Accept" -> exptectedContentType))
"I09" should {
"be able to construct datetime objects from string" in {
val stringRequest =
"""{
| "complete_before": "2016-07-25T13:15:53.208Z",
| "complete_after": "2016-07-25T13:15:53.208Z"
|}""".stripMargin
val fromString = route(app, FakeRequest(POST, "/019", headers, Json.parse(stringRequest))).get
status(fromString) mustBe OK
contentType(fromString) mustBe Some(exptectedContentType)
contentAsString(fromString) must include("\"complete_before\":")
contentAsString(fromString) must include("\"complete_after\":")
}
"be able to construct datetime objects from long" in {
val longRequest =
"""{
| "complete_before": 1469452060,
| "complete_after": 1469452060
|}""".stripMargin
val fromLong = route(app, FakeRequest(POST, "/019", headers, Json.parse(longRequest))).get
status(fromLong) mustBe OK
contentType(fromLong) mustBe Some(exptectedContentType)
contentAsString(fromLong) must include("\"complete_before\":")
contentAsString(fromLong) must include("\"complete_after\":")
}
"be able to construct datetime objects from string in header and path" in {
val request = "2016-07-25T13:15:53.208Z"
val withHeader = FakeHeaders(("header", request) +: headers.data)
val fromString = route(app, FakeRequest(GET, s"/019/$request", withHeader, AnyContentAsEmpty)).get
contentAsString(fromString) must include("\"complete_before\":")
contentAsString(fromString) must include("\"complete_after\":")
status(fromString) mustBe OK
contentType(fromString) mustBe Some(exptectedContentType)
}
"NOT be able to construct datetime objects from long in header and path" in {
val request = 1469452060
val withHeader = FakeHeaders(("header", request.toString) +: headers.data)
val fromLong = route(app, FakeRequest(GET, s"/019/$request", withHeader, AnyContentAsEmpty)).get
status(fromLong) mustBe BAD_REQUEST
}
}
}
示例3: UserSignUpData
//设置package包名称以及导入依赖的类
package controllers
import model.Account
import play.api.data.Forms._
import play.api.data._
import play.api.mvc.{Action, _}
import play.api.i18n.Messages.Implicits._
import play.api.Play.current
case class UserSignUpData(email:String,password:String,confirm:String)
class SignupController extends Controller{
val userForm=Form(
mapping(
"email" -> email,
"password" -> nonEmptyText,
"confirm-password"->nonEmptyText
)(UserSignUpData.apply)(UserSignUpData.unapply)
.verifying("Invalid user name or password",result => check(result)
)
)
def check(data: UserSignUpData):Boolean = {
if(data.password == data.confirm)
true
else
false
}
def showSignUpPage =Action { implicit request =>
Ok(views.html.sign_up(userForm))
}
def createUser = Action { implicit request =>
userForm.bindFromRequest.fold(
formWithErrors =>{
BadRequest(views.html.sign_up(formWithErrors))
//
},
UsersData => {
Ok("User Created")
Account.createUser(UsersData.email,UsersData.password)
Redirect(routes.AccountController.showAccountsPage).withSession(
"connected" -> UsersData.email)
})
}
}
示例4: PetsController
//设置package包名称以及导入依赖的类
package com.wunder.pets.web.controllers
import java.util.UUID
import javax.inject.{Inject, Singleton}
import com.wunder.pets.entity.Entity.{All, Create, Find}
import com.wunder.pets.pets.{CreatePetForm, Pet}
import com.wunder.pets.web.Serializers._
import play.api.libs.json._
import play.api.mvc.{Controller, _}
import scala.concurrent.ExecutionContext
@Singleton
class PetsController @Inject()(findPet: Find[Pet], createPet: Create[CreatePetForm, Pet], allPets: All[Pet])
(implicit ec: ExecutionContext) extends Controller with CrudController {
def all() = Action.async {
// Future.successful(Ok("Pets"))
allPets().map(pets => Ok(Json.toJson(pets)))
}
def find(id: UUID) = Action.async {
findPet(id).map {
case Some(pet) =>
Ok(Json.toJson(pet))
case None =>
NotFound
}
}
def create = Action.async(parse.json) { request =>
createEntity(request, createPet)
}
}
示例5: UserController
//设置package包名称以及导入依赖的类
package controllers
import play.api.Play.current
import play.api.cache.Cache
import play.api.libs.json._
import play.api.mvc.{Action, Controller, _}
object UserController extends Controller {
def authenticatedAction(f: Request[AnyContent] => Result) = Action {
request => {
(request.session.get("idToken").flatMap { idToken =>
Cache.getAs[JsValue](idToken + "profile")
} map {
profile => f(request)
}).orElse {
Some(Redirect(routes.IndexController.index()))
}.get
}
}
def index = authenticatedAction {
request => {
val idToken = request.session.get("idToken").get
val profile = Cache.getAs[JsValue](idToken + "profile").get
Ok(views.html.user(profile))
}
}
}
示例6: Application
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.{Inject, _}
import actors.WebSocketActor
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import play.api.Play.current
import play.api.mvc.{Action, Controller, _}
@Singleton
class Application @Inject()(implicit system: ActorSystem) extends Controller {
implicit val mat = ActorMaterializer()
val index = Action {
Ok(views.html.index("1"))
}
def socket = WebSocket.acceptWithActor[String, String] { request => out =>
WebSocketActor.props(out, mat)
}
}
示例7: LoginController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject._
import models.LoginData
import play.api.data.Forms._
import play.api.data._
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.mvc.{Action, _}
import play.api.cache._
import models.UserData
@Singleton
class LoginController @Inject() (cache: CacheApi)(implicit val messagesApi: MessagesApi) extends Controller with I18nSupport {
val loginForm = Form(
mapping(
"name" -> text,
"pass"-> text
)(LoginData.apply)(LoginData.unapply)
)
def index = Action { implicit request =>
Ok(views.html.login())
}
def login = Action{ implicit request=>
loginForm.bindFromRequest.fold(
errorForm=>{
BadRequest(views.html.login())
},
validForm=> {
val user = cache.get[UserData](validForm.name)
user match {
case Some(UserData(name,fname,mname,lname,age,pass,mobile,gender,hobbies))=> if(pass==validForm.pass) Redirect(routes.ProfileController.index())withSession (request.session + ("mySession" -> s"${validForm.name}"))
else Redirect(routes.LoginController.index()).flashing("success" -> "please enter correct password")
case None=> Redirect(routes.LoginController.index()).flashing("success" -> "you are not valid user")
}
}
)
}
}
示例8: SignupController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject._
import play.api.data.Forms._
import play.api.data._
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.mvc.{Action, _}
import play.api.cache._
import models.UserData
@Singleton
class SignupController @Inject()(cache: CacheApi)(implicit val messagesApi: MessagesApi) extends Controller with I18nSupport {
val loginForm: Form[UserData] = Form(
mapping(
"username" -> nonEmptyText,
"firstname" -> nonEmptyText,
"middlename" -> text,
"lastname" -> nonEmptyText,
"age" -> number(min = 18, max = 75),
"pass"-> nonEmptyText,
"mobile" -> nonEmptyText,
"gender" -> nonEmptyText,
"hobbies"-> list(text)
)(UserData.apply)(UserData.unapply)
)
def index = Action { implicit request =>
Ok(views.html.singup("",loginForm))
}
def createUser = Action{ implicit request=>
loginForm.bindFromRequest.fold(
errorForm => {
BadRequest(views.html.singup("", errorForm))
}, validForm => {
val user = cache.get[models.UserData](validForm.name)
user match {
case Some(userdata)=>Redirect(routes.LoginController.index()).flashing("success" -> "User already exist please Login")
case None =>cache.set(validForm.name,validForm)
Redirect(routes.ProfileController.index())withSession (request.session + ("mySession" -> s"${validForm.name}"))
}
}
)
}
}
示例9: CandidatesListController
//设置package包名称以及导入依赖的类
package controllers
import com.google.inject.Inject
import play.api.cache.Cached
import play.api.mvc.{Controller, _}
import rendering.{CandidateListSection, PlainTextRendering}
import repos.CandidatesRepo
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
class CandidatesListController @Inject()(candidatesRepo: CandidatesRepo, cached: Cached) extends Controller {
def list() = cached("candidate-list").default(6.hours) {
Action.async { request =>
candidatesRepo.findAllCandidates().map { candidates =>
val sections = candidates.map { candidate =>
new CandidateListSection(candidate) with PlainTextRendering
}
Ok(views.txt.candidate_list(sections))
}
}
}
}
示例10: User
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.Inject
import play.api.cache.CacheApi
import play.api.libs.json._
import play.api.libs.ws.WSClient
import play.api.mvc.{Action, Controller, _}
class User @Inject()(cache: CacheApi, ws: WSClient) extends Controller {
def AuthenticatedAction(f: Request[AnyContent] => Result): Action[AnyContent] = {
Action { request =>
(request.session.get("idToken").flatMap { idToken =>
cache.get[JsValue](idToken + "profile")
} map { profile =>
f(request)
}).orElse {
Some(Redirect(routes.Application.index()))
}.get
}
}
def index = AuthenticatedAction { request =>
val idToken = request.session.get("idToken").get
val profile = cache.get[JsValue](idToken + "profile").get
Ok(views.html.user(profile))
}
}
示例11: FormController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject._
import model.UserData
import play.api.data.Form
import play.api.data.Forms._
import play.api.i18n.I18nSupport
import play.api.mvc.{Action, _}
@Singleton
class FormController @Inject()(cc: ControllerComponents) extends AbstractController(cc) with I18nSupport {
def form = Action {implicit request =>
Ok(views.html.form(FormController.userForm))
}
def submit = Action { implicit request =>
FormController.userForm.bindFromRequest.fold(
formWithErrors => {
// binding failure, you retrieve the form containing errors:
BadRequest(views.html.form(formWithErrors))
},
userData => {
val newUser = model.UserData(userData.name, userData.age)
val id = 1
Ok(views.html.formconfirm(userData))
}
)
}
}
object FormController {
val userForm = Form(
mapping(
"name" -> nonEmptyText,
"age" -> number(min = 0, max = 100)
)(UserData.apply)(UserData.unapply)
)
}