如何在RestHelper中的lift框架中简单地访问get和post属性? 在文档中没有任何关于它的明确例子:(
package my.domain
import net.liftweb.http._
import net.liftweb.http.rest._
import net.liftweb.json.JsonAST._
import net.liftweb.json._
import net.liftweb.common.{Box,Full,Empty,Failure,ParamFailure}
import net.liftweb.mapper._
import ru.dmteam.model.{RssItem}
object ContentRest extends RestHelper {
def getq: String = {
val q = S.param("q")
q.toString
}
serve {
case "api" :: "static" :: _ XmlGet _=> <b>{getq}</b>
}
}
我想了解当我请求q
http://localhost:8080/api/static.xml?q=test
的电梯展示价值
答案 0 :(得分:23)
Lift使用Box而不是null来指示是否传递了参数。这样可以很好地利用Scala进行理解,将一个好的请求处理程序链接在一起。我会让代码说明一切:
object MyRest extends RestHelper {
// serve the q parameter if it exists, otherwise
// a 404
serve {
case "api" :: "x1" :: _ Get _ =>
for {
q <- S.param("q")
} yield <x>{q}</x>
}
// serve the q parameter if it exists, otherwise
// a 404 with an error string
serve {
case "api" :: "x2" :: _ Get _ =>
for {
q <- S.param("q") ?~ "Param 'q' missing"
} yield <x>{q}</x>
}
// serve the q parameter if it exists, otherwise
// a 401 with an error string
serve {
case "api" :: "x2" :: _ Get _ =>
for {
q <- S.param("q") ?~ "Param 'q' missing" ~> 401
} yield <x>{q}</x>
}
// serve the q, r, s parameters if this exists, otherwise
// different errors
serve {
case "api" :: "x3" :: _ Get _ =>
for {
q <- S.param("q") ?~ "Param 'q' missing" ~> 401
r <- S.param("r") ?~ "No 'r'" ~> 502
s <- S.param("s") ?~ "You're s-less" ~> 404
} yield <x><q>{q}</q><r>{r}</r><s>{s}</s></x>
}
}
答案 1 :(得分:5)
我不确定,但你可以试试吗
S.param("param_name")
http://scala-tools.org/mvnsites-snapshots/liftweb/scaladocs/index.html
或使用req类
case r @ JsonPost("some" :: "path" :: _, json) => _ => {
r.param("name")
}
http://scala-tools.org/mvnsites-snapshots/liftweb/scaladocs/index.html
修改:我正在运行此示例:
package code.rest
import net.liftweb.http.rest._
object SampleRest extends RestHelper {
serve {
case Get("sample" :: _, req) =>
<hello>{req.param("name") getOrElse ("??") }</hello>
}
}
答案 2 :(得分:2)
在代码段中,Get和Post参数是代码段生命周期的一部分。将GUID属性传递给传递给SHtml.text(defaultValue,passedFunction)的函数,并返回GUID在生成的HTML元素的name属性中的位置。提交表单时,Lift会在函数表中查找GUID并使用传递的参数调用该函数。
如需更多一般要求,请打开方框:
val q = S.param("named_parameter") openOr ""
您可以为有状态请求设置会话变量:
object myObject extends SessionVar[Box[Model]](Empty)