How to get parameters on HTML page passed from fragment using Lift frame

I want to create a Lift web application, I have an index page in which there is a 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 appropriate 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 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 transfer the name attribute from this fragment to another view, an HTML page ("hello.html"), which will receive and display this name.

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

At this point, my hello.html has:

 <body> <p> Hello ... (you must display the name!)</p> </body> 
+4
source share
1 answer

To do what you want to do, you simply specify the form directly where hello.html is ever mounted. I guess this is hello in the same way.

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> 

Excerpt

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

The greater the way to do this, the better it will be to use 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> 

Excerpt

 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> 

Excerpt

 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 given form values ​​for the object, and then perform a stateful redirection that sets values ​​in ShowHelloSnippet for use after the page is redirected. As an alternative to both, you can use Ajax to easily display values ​​on one page.

+2
source

All Articles