使用Jerkson,我能够解析包含JSON数组的String,如下所示:
com.codahale.jerkson.Json.parse[Array[Credentials]](contents)
Run Code Online (Sandbox Code Playgroud)
其中contents是包含以下内容的String:
[{"awsAccountName":"mslinn","accessKey":"blahblah","secretKey":"blahblah"}]
Run Code Online (Sandbox Code Playgroud)
......我会得到一系列证书.
(简短转移)我尝试使用新的JSON解析器为Play 2.1和Scala使用不同的数据做类似的事情.对于简单的解析,以下工作正常.案例类(S3File)定义了此工作所需的unapply方法:
case class S3File(accountName: String,
bucketName: String,
endpoint: String = ".s3.amazonaws.com")
implicit val s3FileFormat = Json.format[S3File]
val jsValue = Json.parse(stringContainingJson)
Json.fromJson(jsValue).get
Run Code Online (Sandbox Code Playgroud)
让我们重新考虑一下名为包含JSON的内容的原始字符串.与所有集合一样,对象数组没有unapply方法.这意味着我在上面的转移中展示的技术将无效.我试图为此目的创建一次性案例类:
case class ArrayCreds(payload: Array[Credentials])
implicit val credsFormat = Json.format[ArrayCreds]
val jsValue = Json.parse(contents)
val credArray = Json.fromJson(jsValue).get.payload
Run Code Online (Sandbox Code Playgroud)
......不幸的是,这失败了:
No unapply function found
[error] implicit val credsFormat = Json.format[ArrayCreds]
[error] ^
[error]
/blah.scala:177: diverging implicit expansion for type play.api.libs.json.Reads[T]
[error] starting with method ArrayReads in trait DefaultReads
[error] val credArray = Json.fromJson(jsValue).get
[error] …Run Code Online (Sandbox Code Playgroud) 我需要在Play 2.1应用程序中对日志记录进行更精细的控制.为此,我在每个Controller中放置了一个独特的记录器,如下所示:
package controllers.admin
object Accounts extends Controller {
val log = LoggerFactory.getLogger(getClass())
def session = Action { implicit request =>
log.trace("session {}", request)
val body: AnyContent = request.body
log.debug("body: {}", body);
val jsonBody: Option[JsValue] = body.asJson
Ok("Hello " + jsonBody)
}
}
Run Code Online (Sandbox Code Playgroud)
这会产生的问题在conf/logger.xml文件中.当我使用对象的专有名称时; controllers.admin.Accounts $在这种情况下,播放运行时在我身上喷出stacktrace.
09:08:05,885 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@24:62 - RuntimeException in Action for tag [logger] java.lang.IllegalArgumentException: Failed to parse input [controllers.admin.Accounts$]
at java.lang.IllegalArgumentException: Failed to parse input [controllers.admin.Accounts$]
at at ch.qos.logback.core.util.OptionHelper.substVars(OptionHelper.java:125)
...
Run Code Online (Sandbox Code Playgroud)
我的conf/logger.xml看起来像
<logger name="play" level="info" />
<logger …Run Code Online (Sandbox Code Playgroud) 播放框架,如果其他情况不在这里工作
如果userprofile.useraccountid,useraccount.id具有相同的值,则不在视图页面上查看id用户
我在视图中的代码..
@(userprofiles: List[UserProfile],myFriend:models.MyFriend,userprofile:models.UserProfile,useraccount:models.UserAccount)
@helper.form(action = routes.Application.createMyFriend) {
<br/><br/><br/>
@for(userprofile <- userprofiles){
@if(userprofile.useraccountid != useraccount.id) {
<img src="@routes.Assets.at("images/img2.png")" width="200" height="200" />
<br>
<h5>@userprofile.name</h5>
<h5>@userprofile.useraccountid</h5>=<h5>@useraccount.id</h5>
<h6>@userprofile.gender</h6>
<h6>@userprofile.date_of_birth</h6>
<div class="actions">
<input type="submit" class="btn primary" value="+1 Add As Friend" title="Send Friend Request">
</div>
<br/>
}
}
}
Run Code Online (Sandbox Code Playgroud)
检查条件时,数据库值是视图页面中的视图
@if(userprofile.useraccountid != useraccount.id)
Run Code Online (Sandbox Code Playgroud)
如果改变条件
@if(userprofile.useraccountid == useraccount.id)
Run Code Online (Sandbox Code Playgroud)
视图页面中没有任何内容.
在此代码中运行程序代码部分
<h5>@userprofile.useraccountid</h5>=<h5>@useraccount.id</h5>
Run Code Online (Sandbox Code Playgroud)
id在这里是相同的,并且在视图中显示然后该想法不是假的...例如15 = 15.
这里的2 id是相同的,但是在if情况下的检查不能正常工作......或者编码不正确.
编辑这是在申请中
def listMyFriend = Action { implicit request =>
var cid=request.session.get("userId")
println("aa",cid)
if (request.session.get("userId") == None) {
Results.Redirect("/")
} …Run Code Online (Sandbox Code Playgroud) 我尝试使用Play framework 2.1.1和scala验证注册表单.我有
编译错误:类型不匹配; found:(String,String,String,String,String,String,String,Int)=> models.Contact required :( String,String,String,String,String,(String,String),String,Int)=>?
models.scala:
package models
case class Contact(firstname: String, lastname: String, jobtitle: String, phone:String, email: String, password: String, companyname: String, employeescount: Int)
Run Code Online (Sandbox Code Playgroud)
Application.scala:
val contactForm = Form(
mapping(
"firstname" -> nonEmptyText(minLength=2, maxLength=10),
"lastname" -> nonEmptyText(minLength=2, maxLength=10),
"jobtitle" -> nonEmptyText(minLength=2, maxLength=10),
"phone" -> nonEmptyText(minLength=2, maxLength=10),
"email" -> (email verifying nonEmpty),
"password" -> tuple(
"main" -> text(minLength = 6),
"confirm" -> text
).verifying(
// Add an additional constraint: both passwords must match
"Passwords don't match", …Run Code Online (Sandbox Code Playgroud) 我试图在playframework视图中迭代,但现在没有成功.我有以下结构:
@if(list != null) {
for(a <- 0 to list.size()/5)
{
// some html, where I want to get the value of a
for(b <- a*5 to a*5+5) // here I want to use the a value again
{
some html
}
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是如何获取循环的当前索引,以便我能够使用它.
我试图从Play 2.1.3升级到2.2.0,我在尝试编译时遇到错误.主要的变化是不推荐使用play.api.libs.concurrent.Promise,并指示用scala.concurrent.Promise替换它.编译错误如下:
错误信息:
WebSocketJoin.scala:23: object creation impossible, since method fold in trait Iteratee of type [B](folder: play.api.libs.iteratee.Step[A,B] => scala.concurrent.Future[B])(implicit ec: scala.concurrent.ExecutionContext)scala.concurrent.Future[B] is not defined
[error] def wrap(delegate: Iteratee[A, B]): Iteratee[A, B] = new Iteratee[A, B] {
[error] ^
[error] one error found
[error] (adapter-api/compile:compile) Compilation failed
Run Code Online (Sandbox Code Playgroud)
码:
package utils
import play.api.libs.iteratee.{Step, Enumerator, Iteratee}
import scala.concurrent.{Promise, ExecutionContext, Future}
import ExecutionContext.Implicits.global
object WebSocketJoin {
/**
* Create a joined iteratee enumerator pair.
*
* When the enumerator is applied to an iteratee, …Run Code Online (Sandbox Code Playgroud) 我正在摆弄Play Framework 2.2,尝试各种场景并且来自LAMP环境我有这个问题:在现场制作服务器上是否可以进行某种形式的热修复?如果是这样,它究竟是如何工作的?如果不是,那么最接近的是什么?
服务器操作系统是Centos 6.4.LAMP中的等效示例是使用hot fix重新上传某个文件.
我是scala和cassandra的新手.我知道使用hector将cassandra与java连接起来.但我不知道如何通过scala连接cassandra.我想要一个简单的例子.
当我尝试测试应用程序(通过遵循本教程制作)时,我收到以下错误
[info] Compiling 1 Java source to C:\Users\Xyzk\zentask\target\scala-2.10\classes...
[error] C:\Users\Xyzk\zentask\app\models\ModelsTest.java:4: error: package org.junit does not exist
[error] import org.junit.*;
[error] ^
[error] C:\Users\Xyzk\zentask\app\models\ModelsTest.java:5: error: package org.junit does not exist
[error] import static org.junit.Assert.*;
[error] ^
[error] C:\Users\Xyzk\zentask\app\models\ModelsTest.java:6: error: package play.test does not exist
[error] import play.test.WithApplication;
[error] ^
[error] C:\Users\Xyzk\zentask\app\models\ModelsTest.java:7: error: package play.test does not exist
[error] import static play.test.Helpers.*;
[error] ^
[error] C:\Users\Xyzk\zentask\app\models\ModelsTest.java:9: error: cannot find symbol
[error] public class ModelsTest extends WithApplication {
[error] ^
[error] …Run Code Online (Sandbox Code Playgroud) 我开始使用Play 2.2.x,我正在尝试处理POST请求,我的理解是我不需要在conf/routes文件中指定参数,而是使用play的DynamicForm类提取查询,如下所示:
import play.*;
import play.api.data.Form;
import play.data.DynamicForm;
import play.mvc.*;
import views.html.*;
public static Result hello() {
DynamicForm requestData = Form.form().bindFromRequest();
String firstname = requestData.get("firstname");
String lastname = requestData.get("lastname");
return ok("Hello " + firstname + " " + lastname);
}
Run Code Online (Sandbox Code Playgroud)
但是,我在Eclipse中遇到此错误:
"The method form() is undefined for the type Form
Run Code Online (Sandbox Code Playgroud)
当我执行"play run"时,我得到了这个编译器:
error: cannot find symbol
DynamicForm data = Form.form().bindFromRequest();
Run Code Online (Sandbox Code Playgroud)
这段代码有什么问题?这个类或方法是否已弃用?
谢谢!