简而言之,这可以解释为"继承与功能库"
例如,我想在javax.servlet.http.HttpServletRequest中添加一个方法,它给我整个身体,一个getBody()方法,它将通过getReader方法读取正文,只是为了举个例子.
在其他语言中,比如ruby或javascript,你可以在基类中添加一个方法,甚至可以添加到特定的实例,但在java中我看到这两个选择......
扩展HttpServletRequest(类似于MyHttpServletRequest)并添加方法
或者使用静态方法使用以下方法创建HttpServeletHelper静态类
public static String HttpServeletHelper.getBody(HttpServletRequest request)
第一种方法更面向对象,更优雅,但强迫你每次需要时都会施放你的对象,不知怎的,你必须告诉jsp使用你的类......
第二种方法只是一个很好的旧功能库......根据你的看法,它可能是好事还是坏事......
你在每种方法中看到的利弊是什么,在这种情况下哪一种更受推荐?
根据
http://msdn.microsoft.com/en-us/library/ms524620.aspx
你应该使用server.createObject
如果您已熟悉VBScript或JScript,请注意您不使用脚本语言的函数来创建新的对象实例(VBScript中的CreateObject或JScript中的New).您必须使用ASP Server.CreateObject方法; 否则,ASP无法跟踪您在脚本中使用该对象的情况.
但是其他一些人认为server.createObject意味着可以避免大部分时间的开销
http://classicasp.aspfaq.com/components/should-i-use-createobject-or-server-createobject.html
CreateObject比Server.CreateObject具有更少的开销,因为后者使用MTS - 导致显着的开销.
当组件遇到错误时,您也会遇到性能命中,因为使用Server.CreateObject时,这些错误会写入事件日志(无可否认,这在调试过程中非常有用).
要么
http://www.4guysfromrolla.com/webtech/043099-1.shtml
如果您正在编写处理事务的组件,这可能会变得很重要,因为它将是一个通过MTS传递它的良好安全网,因为您将使用MTS命令.但是,如果您不使用MTS,则可以通过将其传递给Server.CreateObject来创建处理器和内存.这使得使用CreateObject更好,因为它直接通过.
所以,如果我不使用mts并且不需要访问内置的asp的对象(比如set d = createObject("scripting.dictionary")),那么忘记server.createObject并使用createobject就可以了吗?
非常感谢...
我打断了游戏框架部署到gae
我用它部署了它
play gae:deploy --gae=$GAE_PATH
Run Code Online (Sandbox Code Playgroud)
然后在它的中间按ctrl-c
现在,当我尝试重新部署它时,我收到以下错误:
Unable to update app: Error posting to URL: https://appengine.google.com/api/appversion/create?app_id=playdoces&version=20111007&
409 Conflict
Another transaction by user opensas is already in progress for app: s~playdoces, version: 20111007. That user can undo the transaction with "appcfg rollback".
Please see the logs [/tmp/appcfg1441845586056774629.log] for further information.
Run Code Online (Sandbox Code Playgroud)
我试过了
/home/sas/devel/gae/bin/appcfg.sh rollback
Run Code Online (Sandbox Code Playgroud)
但是没有这样的选择
任何的想法?
最后,我刚创建了另一个版本并将其设置为默认版本
但我想知道是否有某种方法可以取消之前的部署
我有一个元组列表,我想遍历并获取每个元素的值.
这是代码:
scala> val myTuples = Seq((1, "name1"), (2, "name2"))
myTuples: Seq[(Int, java.lang.String)] = List((1,name1), (2,name2))
scala> myTuples.map{ println _ }
(1,name1)
(2,name2)
res32: Seq[Unit] = List((), ())
Run Code Online (Sandbox Code Playgroud)
到目前为止,这么好,但是
scala> myTuples.map{ println _._1 }
<console>:1: error: ';' expected but '.' found.
myTuples.map{ println _._1 }
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
scala> myTuples.map{ println(_._1) }
<console>:35: error: missing parameter type for expanded function ((x$1) => x$1._1)
myTuples.map{ println(_._1) }
scala> myTuples.map{ val (id, name) = _ }
<console>:1: error: unbound placeholder parameter
myTuples.map{ val (id, …Run Code Online (Sandbox Code Playgroud) 我有这样一个简单的枚举:
object ConditionOperator extends Enumeration {
val Equal = Value("equal")
val NotEqual = Value("notEqual")
val GreaterOrEqual = Value("greaterOrEqual")
val Greater = Value("greater")
val LessOrEqual = Value("lessOrEqual")
val Less = Value("less")
Run Code Online (Sandbox Code Playgroud)
我想为每个枚举添加一个方法,以便我可以像这样使用它:
def buildSqlCondition(field: String, operator: ConditionOperator.Value, value: String ) = {
val sqlOperator = operator.toSql
[...]
Run Code Online (Sandbox Code Playgroud)
因此,ConditionOperator.Equal.toSql将返回"=",而ConditionOperator.NotEqual.toSql将返回"<>"等...
但我不知道如何定义toSql方法,以便每个枚举可以"看到"它自己的值并决定如何将自己转换为sql运算符...
我有以下方法:
def save(entity: A): Either[List[Error],A] + {....
Run Code Online (Sandbox Code Playgroud)
我想用specs2测试
我想在未指定必填字段时测试是否存在特定错误,如下所示:
val noNickname = User(
nickname = "",
name = "new name",
)
noNickname.save must beLeft.like {
case errors => {
atLeastOnceWhen(errors) {
case error => {
error.errorCode must equalTo(Error.REQUIRED)
error.field must equalTo("nickname")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我想定义自己的匹配器,使其更简洁,像这样:
noNickname.save must haveError.like {
case error => {
error.errorCode must equalTo(Error.REQUIRED)
error.field must equalTo("nickname")
}
}
}
Run Code Online (Sandbox Code Playgroud)
我查看了文档(http://etorreborre.github.com/specs2/guide/org.specs2.guide.Matchers.html#Matchers),但我无法弄清楚如何定义像hasError这样的自定义匹配器.喜欢
以下是一个非常常见的播放框架2控制器:
def save(ideaId : Long) = CORSAction { request =>
Idea.findById(ideaId).map { idea =>
request.body.asJson.map { json =>
json.asOpt[Comment].map { comment =>
comment.copy(idea = idea).save.fold(
errors => JsonBadRequest(errors),
comment => Ok(toJson(comment).toString)
)
}.getOrElse (JsonBadRequest("Invalid Comment entity"))
}.getOrElse (JsonBadRequest("Expecting JSON data"))
}.getOrElse (JsonBadRequest("Could not find idea with id '%s'".format(ideaId)))
}
Run Code Online (Sandbox Code Playgroud)
我发现所有嵌套的.maps都有点烦人,我也发现每个错误处理都在底部有点乏味
您将如何改进它以使其更具可读性,同时保持功能惯用的scala代码?
我想也许是这样的(它是seudo代码,仍然无法编译)
def save(ideaId : Long) = CORSAction { request =>
val idea = Idea.findById(ideaId).getOrElse(
return JsonBadRequest("Could not find idea with id '%s'".format(ideaId)))
val json = request.body.asJson.getOrElse(
return JsonBadRequest("Expecting JSON …Run Code Online (Sandbox Code Playgroud) 我有一些代码与几个讨厌的嵌套检查...
我很确定它可以用一个很好的理解来重写,但我对如何混合模式匹配的东西有点困惑
// first tries to find the token in a header: "authorization: ideas_token=xxxxx"
// then tries to find the token in the querystring: "ideas_token=xxxxx"
private def applicationTokenFromRequest(request: Request[AnyContent]): Option[String] = {
val fromHeaders: Option[String] = request.headers.get("authorization")
val tokenRegExp = """^\s*ideas_token\s*=\s*(\w+)\s*$""".r
val tokenFromHeader: Option[String] = {
if (fromHeaders.isDefined) {
val header = fromHeaders.get
if (tokenRegExp.pattern.matcher(header).matches) {
val tokenRegExp(extracted) = header
Some(extracted)
} else {
None
}
} else {
None
}
}
// try to find it in the queryString …Run Code Online (Sandbox Code Playgroud) 我正在从app 2.0.4迁移应用程序到2.1
但是下面的代码提出了这个警告:
def toConditionOperator(value: String): ConditionOperator.Value = {
if (value==null) {
ConditionOperator.Unknown
} else {
value.toLowerCase match {
case "equal" | "=" | ":" => ConditionOperator.Equal
case "notequal" | "!=" | "!:" | "<>" => ConditionOperator.NotEqual
case "greaterorequal" | ">=" => ConditionOperator.GreaterOrEqual
case "greater" | ">" => ConditionOperator.Greater
case "lessorequal" | "<=" => ConditionOperator.LessOrEqual
case "less" | "<" => ConditionOperator.Less
case "between" => ConditionOperator.Between
case "in" => ConditionOperator.In
case "startswith" => ConditionOperator.StartsWith
case "endswith" => ConditionOperator.EndsWith
case "contains" | …Run Code Online (Sandbox Code Playgroud) scala ×6
asp-classic ×1
closures ×1
com ×1
createobject ×1
deployment ×1
enums ×1
function ×1
idiomatic ×1
inheritance ×1
java ×1
specs2 ×1
testing ×1
tomcat ×1
tuples ×1