如何使用Lift框架从片段传递的HTML页面中获取参数 [英] How to get parameters in an HTML page passed from a snippet with Lift framework

查看:185
本文介绍了如何使用Lift框架从片段传递的HTML页面中获取参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < p>< p> div id =mainclass =lift:surround?with = default& at = content> 
< div>应用程序< / div>
< div>
< form method =postclass =lift:DumbForm>
< table>
< tr>< td>电子邮件:其中; / TD> < td>< input name =emailtype =text>< / td>< / tr>
< tr>< td>密码:其中; / TD> < td>< input name =pwdtype =password>< / td>< / tr>
< tr>< td>名:其中; / TD> < td>< input name =nametype =text>< / td>< / tr>
< tr>< td>< input type =submitvalue =Sign in>< / td> < td>< input type =resetvalue =Reset>< / td>< / tr>
< / table>
< / form>
< / div>
< / div>

使用相应的片段(文件DumbForm.scala):

 包装代码
包装片段

进口net.liftweb._
进口http._
import scala.xml.NodeSeq
$ b $ / **
*从表单POST获取查询参数
*并处理它们的代码片段
* /
对象DumbForm {
def render(in:NodeSeq):NodeSeq = {
//使用Scala for-comprehension来评估每个参数
for {
r < - S。请求如果r.post_? //确保它是一个后缀
名称< - S.param(name)//获取名称字段
} {
S.notice(Nom:+ name)
S.redirectTo(/ hello)
}

//如果我们没有获得帖子和
//所有参数
in


$ / code>

我想传递属性name从这段代码到另一个视图,HTML页面(hello.html),它将获得并显示该名称。



t知道如何将代码中的name参数传递给view(hello.html),以及如何在视图中获取这个参数?!



<这一刻,我的hello.html有:

 < body> 
< p>你好...(你必须显示名字!)< / p>
< / body>


解决方案

要做你想做的事,只需将表单直接指向 hello.html 挂载的位置。我假定它是 hello 在同一个路径中。 / p>

 < div id =mainclass =lift:surround?with = default& at = content> 
< div>应用程序< / div>
< div>
< form method =postaction =hello>
< table>
< tr>< td>电子邮件:其中; / TD> < td>< input name =emailtype =text>< / td>< / tr>
< tr>< td>密码:其中; / TD> < td>< input name =pwdtype =password>< / td>< / tr>
< tr>< td>名:其中; / TD> < td>< input name =nametype =text>< / td>< / tr>
< tr>< td>< input type =submitvalue =Sign in>< / td> < td>< input type =resetvalue =Reset>< / td>< / tr>
< / table>
< / form>
< / div>
< / div>

hello.html

 < div data-lift =ShowHelloSnippet> 
< p>您好< span name =paramname>< / span>< / p>
< / div>

片段

  class ShowHelloSnippet {
def render = {
@paramname#> S.param(name)
}
}






更多提升方式可以做到这一点,可以使用Lift的 SHtml表单元素

dumbform.html

 < div id =mainclass =lift:surround?with = default& at = content> 
< div>应用程序< / div>
< div>
< form method =postdata-lift =FormHandlerSnippet>
< table>
< tr>< td>电子邮件:其中; / TD> < td>< input name =emailtype =text>< / td>< / tr>
< tr>< td>密码:其中; / TD> < td>< input name =pwdtype =password>< / td>< / tr>
< tr>< td>名:其中; / TD> < td>< input name =nametype =text>< / td>< / tr>
< tr>< td>< input id =submitbuttontype =submitvalue =Sign in>< / td> < td>< input type =resetvalue =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>您好< span name =paramname>< / span>< / p>
< / div>

片段
<
> $
class ShowHelloSnippet {$ b $ object $ {
object myVals extends RequestVar [Box [MyFormResponse]](Empty)
} b def render =*#> {
ShowHelloSnippet.myVals.get.map {r =>
@paramname#> r.name
}
}
}

对象上的表单设置值,然后做一个有状态的重定向,它将 ShowHelloSnippet 中的值设置为在页面重定向后使用。作为两者的替代方案,您可以使用Ajax将值显示在同一页面上。


I want to develop a Lift web app, I've an index page that has in here body:

<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>

With the corresponding snippet (file "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
  }
}

I want to pass the attribute "name" from this snippet to another view, HTML page, ("hello.html") that would get and display this name.

But I don't know how to pass the "name" parameter from the snippet to the view (hello.html), and how to get this parameter in the view?!

For this moment, my hello.html has:

<body>
<p> Hello ... (you must display the name!)</p>
</body>

解决方案

To do what you are looking to do, you would just point the form directly at where ever hello.html is mounted. I am assuming it is hello in the same path.

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>

Snippet

class ShowHelloSnippet {
  def render = {
    "@paramname" #> S.param("name")
  }
}


The more Lift way to do it thought, would be to use Lift's 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>

Snippet

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>

Snippet

object ShowHelloSnippet {
  object myVals extends RequestVar[Box[MyFormResponse]](Empty)
}

class ShowHelloSnippet {
  def render = "*" #> {
    ShowHelloSnippet.myVals.get.map { r =>
      "@paramname" #> r.name
    }
  }
}

This will have the form set values on a object, then do a stateful redirect which sets the values in the ShowHelloSnippet for you to use after the page has been redirected. As an alternate to both, you could use Ajax to simply display the values on the same page.

这篇关于如何使用Lift框架从片段传递的HTML页面中获取参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆