我正在尝试Play 2.1.1 的文档化路由示例
路线
# The version parameter is optional. E.g. /api/list-all?version=3.0
GET /api/list-all controllers.Api.list(Option[version])
Run Code Online (Sandbox Code Playgroud)
然后
$ play-2.1.1 compile
[error] /home/paul/server/conf/routes:2: Compilation error[`)' expected but `[' found]
Run Code Online (Sandbox Code Playgroud)
出了什么问题?
routing scala query-parameters playframework playframework-2.1
我有一个标准的,简单的代码来验证用户:
# controller
val addForm = Form(
tuple("email" -> nonEmptyText, "password" -> nonEmptyText)
.verifying("Invalid email or password", result => User.authenticate(result._1, result._2).isRight)
)
def add = Action { Ok(views.html.session.add(addForm)) }
def create = Action {
addForm.bindFromRequest.fold(
failure => BadRequest(views.html.session.add(failure)),
success => Redirect(routes.Home.index)
.flashing("key1" -> "You have logged in.")
)
}
Run Code Online (Sandbox Code Playgroud)
我希望它能显示错误,如果没有,User.authenticate(result._1, result._2).isRight但没有出现错误.是的,身份验证运行良好,但失败时没有错误.
@helper.form(action = routes.Session.create()) {
@helper.inputText(form("email"))
@helper.inputText(form("password"))
<!-- ........... -->
}
Run Code Online (Sandbox Code Playgroud)
为什么?
现在我有
@Entity
public class Argument extends Model
{
@Id
public Long id;
@Required @NotEmpty @Size(max = 140)
public String summary;
@SuppressWarnings("unchecked")
public static Finder<Long, Argument> find = new Finder(Long.class, Argument.class);
...
}
Run Code Online (Sandbox Code Playgroud)
和
@Entity
public class Relation extends Model
{
@Id
public Long id;
@Required @ManyToOne @NotNull @JsonManagedReference
public Argument from;
@ManyToOne @JsonManagedReference
public Argument toArgument;
@ManyToOne @JsonManagedReference
public Relation toRelation;
@Required @NotNull
public Integer type;
...
}
Run Code Online (Sandbox Code Playgroud)
基本上,Relation将两个参数(或参数和另一个关系)链接在一起.这是两个班级之间的单向关系.然而我明白了
[RuntimeException: java.lang.IllegalArgumentException: Infinite recursion
(StackOverflowError) (through reference chain: models.Argument["relations"]-> …Run Code Online (Sandbox Code Playgroud) 我在视图模板中使用playframework2,如何使用我自己定义的另一个模板?
我的第一页模板是:
<!DOCTYPE html>
<html>
<head>
<title>@title</title>
<link rel='shortcut icon' type='image/png' href='@routes.Assets.at("images/favicon.png")'>
<link rel='stylesheet' href='@routes.WebJarAssets.at(WebJarAssets.locate("bootstrap.min.css"))'>
<script type='text/javascript' src='@routes.WebJarAssets.at(WebJarAssets.locate("jquery.min.js"))'></script>
<script type='text/javascript' src='@routes.WebJarAssets.at(WebJarAssets.locate("bootstrap.min.js"))'></script>
<style>
body {
margin-top: 50px;
}
</style>
</head>
<body>
@views.html.slideframework("ssssss")
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
视图中的第二页折叠命名为slideframework.scala.html和内容:
@(message: String)
<div class="col-md-9" role="main">
message
</div>
Run Code Online (Sandbox Code Playgroud)
我的路线文件是:
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
GET / controllers.Application.index()
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)
GET /webjars/*file controllers.WebJarAssets.at(file) …Run Code Online (Sandbox Code Playgroud) 我正在用reactivemongo做测试
在我的控制器中我有这个:
package controllers
import models._
import models.JsonFormats._
import play.modules.reactivemongo.MongoController
import scala.concurrent.Future
import reactivemongo.api.Cursor
import org.slf4j.{LoggerFactory, Logger}
import javax.inject.Singleton
import play.api.mvc._
import reactivemongo.api.collections.default.BSONCollection
import reactivemongo.bson._
@Singleton
class Users extends Controller with MongoController {
private final val logger: Logger = LoggerFactory.getLogger(classOf[Users])
val collection = db[BSONCollection]("users")
// list all articles and sort them
def list = Action.async { implicit request =>
// get a sort document (see getSort method for more information)
val sort = getSort(request)
// build a selection document with …Run Code Online (Sandbox Code Playgroud) 我是Play 2.3.x和Scala的新手,并尝试实现表单输入验证.
我们假设我有一个示例表单.
val userForm = Form(
"firstName" -> nonEmptyText
)
Run Code Online (Sandbox Code Playgroud)
我想为名字字段实现类似的东西:
If a regex for first name (say firstName.regex = “regex for first name” ) is defined then {
Validate first name against specific regex
}else{
Validate against the global regex ( say global.regex = “global regex white list some regex valid for across the application”)
}
Run Code Online (Sandbox Code Playgroud)
此外,我想将其与多个(链式/逐步)验证相结合,以便能够显示:
我想开发一个通用解决方案,以便我可以将它用于所有领域.
感谢任何帮助.
我activator在play-scala模板的帮助下创建了一个测试Play 2.3应用程序:
activator new test play-scala
Run Code Online (Sandbox Code Playgroud)
这是build.sbt:
name := """test"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.1"
libraryDependencies ++= Seq(
jdbc,
anorm,
cache,
ws
)
Run Code Online (Sandbox Code Playgroud)
在application.conf中,我将MySQL设置为应用程序的数据库:
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://localhost:3306/*******?characterEncoding=UTF-8"
db.default.user=root
db.default.password="********"
Run Code Online (Sandbox Code Playgroud)
当我输入activator run控制台时,它在localhost上启动服务器,端口9999就好了.但是,当我在浏览器中打开应用程序时,出现以下错误:
Configuration error
Driver not found: [com.mysql.jdbc.Driver]
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我正在尝试使用以下方法创建一个向mongoddb插入数据的帖子请求:1.sbt 0.13.6 2.播放2.10 3. scala 2.11.2 4. play2-reactivamongo 0.10.2 5. mongodb 2.6.4
数据由json发布,并为模型创建一个case类,并使用JSPath将json转换为实体类.
这是我的示例代码:
def inserTransaction = Action(parser.json) { implicit request =>
val json = request.body
val data = json.as[Transaction]
Logger.info(data.toString)
val future = collection.insert(data.copy(id = Option[BSONObjectID](BSONObjectID.generate)))
var result = ""
future.onComplete {
case Failure(t) => result = "An error has occured: " + t.getMessage
case Success(post) => result = "success"
}
Ok(result)
}
Run Code Online (Sandbox Code Playgroud)
我已经看到一些示例代码使用Action.sync来处理控制器中的异步,但是当我尝试使用Action.sync时,我的Intellij IDE检测到错误"无法将Action.sync解析为签名",我试图改变像这样的功能的结果
future.onComplete {
case Failure(t) => Ok("An error has occured: " + t.getMessage)
case Success(post) …Run Code Online (Sandbox Code Playgroud) 我正在开发Play应用程序!框架.当我必须使用代码403(禁止)进行重定向传递http状态时,重定向不起作用,但是当我使用303(参见其他)时工作正常.
这是我的代码:
Future.successful( play.api.mvc.Results.Redirect("http://otherDomain",
play.api.http.Status.FORBIDDEN)
.discardingCookies( DiscardingCookie("key") ))
Run Code Online (Sandbox Code Playgroud)
任何的想法 ?!
我正在使用:
Scala 2.10和Play!框架2.3.3
谢谢 !
碰到一个问题我还没找到答案.
使用Scala在playframework 2上运行.
需要编写一个执行多个Future调用的Action方法.我的问题:1)附加的代码是否是非阻塞的,因此看起来应该如何?2)是否保证在任何给定时间都捕获两个DAO结果?
def index = Action.async {
val t2:Future[Tuple2[List[PlayerCol],List[CreatureCol]]] = for {
p <- PlayerDAO.findAll()
c <- CreatureDAO.findAlive()
}yield(p,c)
t2.map(t => Ok(views.html.index(t._1, t._2)))
}
Run Code Online (Sandbox Code Playgroud)
感谢您的反馈意见.
playframework ×10
scala ×8
akka ×1
asynchronous ×1
ebean ×1
json ×1
redirect ×1
regex ×1
routing ×1
templates ×1
validation ×1