我想开发一个Lift web应用程序,我在这里有一个索引页面:
<div id="main" class="lift:surround?with=default&at=content">
<div> App </div>
<div>
<form method="post" class="lift:DumbForm">
<table>
<tr><td> Email:</td> <td><input name="email" type="text"></td></tr>
<tr><td> Password:</td> <td><input name="pwd" type="password"></td></tr>
<tr><td> Name:</td> <td><input name="name" type="text"></td></tr>
<tr><td><input type="submit" value="Sign in"></td> <td><input type="reset" value="Reset"></td></tr>
</table>
</form>
</div>
</div>
使用相应的代码段(文件“DumbForm.scala”):
package code
package snippet
import net.liftweb._
import http._
import scala.xml.NodeSeq
/**
* A snippet that grabs the query parameters
* from the form POST and processes them
*/
object DumbForm {
def render(in: NodeSeq): NodeSeq = {
// use a Scala for-comprehension to evaluate each parameter
for {
r <- S.request if r.post_? // make sure it's a post
name <- S.param("name") // get the name field
} {
S.notice("Nom: "+name)
S.redirectTo("/hello")
}
// pass through the HTML if we don't get a post and
// all the parameters
in
}
}
我想将此代码段中的属性“name”传递给另一个获取并显示此名称的视图HTML页面(“hello.html”)。
但我不知道如何将“name”参数从代码段传递给视图(hello.html),以及如何在视图中获取此参数?!
目前,我的hello.html有:
<body>
<p> Hello ... (you must display the name!)</p>
</body>
答案 0 :(得分:2)
要执行您要执行的操作,您只需将表单直接指向安装hello.html
的位置即可。我假设它是hello
在同一条道路上。
<强> dumbform.html 强>
<div id="main" class="lift:surround?with=default&at=content">
<div> App </div>
<div>
<form method="post" action="hello">
<table>
<tr><td> Email:</td> <td><input name="email" type="text"></td></tr>
<tr><td> Password:</td> <td><input name="pwd" type="password"></td></tr>
<tr><td> Name:</td> <td><input name="name" type="text"></td></tr>
<tr><td><input type="submit" value="Sign in"></td> <td><input type="reset" value="Reset"></td></tr>
</table>
</form>
</div>
</div>
<强> hello.html的强>
<div data-lift="ShowHelloSnippet">
<p>Hello <span name="paramname"></span></p>
</div>
<强>段强>
class ShowHelloSnippet {
def render = {
"@paramname" #> S.param("name")
}
}
更多提升方式,可以使用Lift的SHtml form elements:
<强> dumbform.html 强>
<div id="main" class="lift:surround?with=default&at=content">
<div> App </div>
<div>
<form method="post" data-lift="FormHandlerSnippet">
<table>
<tr><td> Email:</td> <td><input name="email" type="text"></td></tr>
<tr><td> Password:</td> <td><input name="pwd" type="password"></td></tr>
<tr><td> Name:</td> <td><input name="name" type="text"></td></tr>
<tr><td><input id="submitbutton" type="submit" value="Sign in"></td> <td><input type="reset" value="Reset"></td></tr>
</table>
</form>
</div>
</div>
片段
class MyFormResponse(
var email:String="",
var password:String="",
var name:String ="")
class FormHandlerSnippet {
def render = {
val responseForm = new MyFormResponse()
"@email" #> SHtml.text("", (valueSupplied) => {
responseForm.email = valueSupplied
}) &
"@pwd" #> SHtml.password("", (valueSupplied) => {
responseForm.password = valueSupplied
}) &
"@name" #> SHtml.text("", (valueSupplied) => {
responseForm.name = valueSupplied
}) &
"#submitbutton" #> SHtml.submit("Sign In", () => {
S.redirectTo("/hello", () => ShowHelloSnippet.myVals(Full(responseForm)))
})
}
}
<强> hello.html的强>
<div data-lift="ShowHelloSnippet">
<p>Hello <span name="paramname"></span></p>
</div>
<强>段强>
object ShowHelloSnippet {
object myVals extends RequestVar[Box[MyFormResponse]](Empty)
}
class ShowHelloSnippet {
def render = "*" #> {
ShowHelloSnippet.myVals.get.map { r =>
"@paramname" #> r.name
}
}
}
这将在对象上具有表单设置值,然后执行有状态重定向,该重定向设置ShowHelloSnippet
中的值,供您在重定向页面后使用。作为两者的替代,您可以使用Ajax在同一页面上显示值。