How to get parameters in an HTML page passed from a snippet with Lift framework

﹥>﹥吖頭↗ 提交于 2019-12-10 17:38:29

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/16086687/how-to-get-parameters-in-an-html-page-passed-from-a-snippet-with-lift-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!