[英]liftweb - accessing get/post parameters
How is it possible to simply access to get and post attributes in lift framework inside RestHelper? 如何在RestHelper中的lift框架中简单地访问get和post属性? There are no any explicit examples about it in documentation :(
文档中没有任何明确的例子:(
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>
}
}
I want to understand how to make lift show value of q
when I am requesting http://localhost:8080/api/static.xml?q=test
我想了解当我请求
http://localhost:8080/api/static.xml?q=test
时,如何使q
提升显示值http://localhost:8080/api/static.xml?q=test
Lift uses Box rather than null to indicate if a parameter was passed. Lift使用Box而不是null来指示是否传递了参数。 This allows the nice use of Scala's for comprehension to chain together a nice request handler.
这样可以很好地利用Scala进行理解,将一个好的请求处理程序链接在一起。 I'll let the code speak for itself:
我会让代码说明一切:
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>
}
}
I'm not sure, but can you try with 我不确定,但你能试试吗?
S.param("param_name")
http://scala-tools.org/mvnsites-snapshots/liftweb/scaladocs/index.html http://scala-tools.org/mvnsites-snapshots/liftweb/scaladocs/index.html
or with the req class 或者使用req类
case r @ JsonPost("some" :: "path" :: _, json) => _ => {
r.param("name")
}
http://scala-tools.org/mvnsites-snapshots/liftweb/scaladocs/index.html http://scala-tools.org/mvnsites-snapshots/liftweb/scaladocs/index.html
Edit : I have this sample running : 编辑 :我有这个示例运行:
package code.rest
import net.liftweb.http.rest._
object SampleRest extends RestHelper {
serve {
case Get("sample" :: _, req) =>
<hello>{req.param("name") getOrElse ("??") }</hello>
}
}
In snippets, Get and Post parameters are part of the snippet lifecycle. 在代码段中,Get和Post参数是代码段生命周期的一部分。 Lift attributes a GUID to the function passed to SHtml.text(defaultValue, passedFunction) and returns places that GUID in the name attribute of the generated HTML element.
将GUID属性传递给传递给SHtml.text(defaultValue,passedFunction)的函数,并返回GUID在生成的HTML元素的name属性中的位置。 When the form is submitted, Lift looks up the GUID in the function table and calls the function with the passed parameter.
提交表单时,Lift会在函数表中查找GUID并使用传递的参数调用该函数。
For more general requests, open the Box: 有关更一般的请求,请打开方框:
val q = S.param("named_parameter") openOr ""
and you could set a session variable for stateful requests: 并且您可以为有状态请求设置会话变量:
object myObject extends SessionVar[Box[Model]](Empty)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.