Skip to content Skip to sidebar Skip to footer

How To Get Parameters In An Html Page Passed From A Snippet With Lift Framework

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

Solution 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

<divid="main"class="lift:surround?with=default&at=content"><div> App </div><div><formmethod="post"action="hello"><table><tr><td> Email:</td><td><inputname="email"type="text"></td></tr><tr><td> Password:</td><td><inputname="pwd"type="password"></td></tr><tr><td> Name:</td><td><inputname="name"type="text"></td></tr><tr><td><inputtype="submit"value="Sign in"></td><td><inputtype="reset"value="Reset"></td></tr></table></form></div></div>

hello.html

<divdata-lift="ShowHelloSnippet"><p>Hello <spanname="paramname"></span></p></div>

Snippet

classShowHelloSnippet {
  defrender = {
    "@paramname"#> S.param("name")
  }
}

The more Lift way to do it thought, would be to use Lift's SHtml form elements:

dumbform.html

<divid="main"class="lift:surround?with=default&at=content"><div> App </div><div><formmethod="post"data-lift="FormHandlerSnippet"><table><tr><td> Email:</td><td><inputname="email"type="text"></td></tr><tr><td> Password:</td><td><inputname="pwd"type="password"></td></tr><tr><td> Name:</td><td><inputname="name"type="text"></td></tr><tr><td><inputid="submitbutton"type="submit"value="Sign in"></td><td><inputtype="reset"value="Reset"></td></tr></table></form></div></div>

Snippet

classMyFormResponse(
    var email:String="", 
    var password:String="", 
    var name:String ="")

classFormHandlerSnippet{

  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

<divdata-lift="ShowHelloSnippet"><p>Hello <spanname="paramname"></span></p></div>

Snippet

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

classShowHelloSnippet{
  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.

Post a Comment for "How To Get Parameters In An Html Page Passed From A Snippet With Lift Framework"