I have a search page that contains a simple form with a single text
field and submit button.  On submit a search is performed using the
value from the textfield as a param and the results are displayed.
Easy so far except that I want to show the same page again i.e
search.html with no results -> search html with results.  I have
struggled with this for a while but finally got it working by making
the snippet a stateful one but I don't really understand why it would
have to be stateful to work as I don't need to remember the results
between requests.

Am I going about the the wrong way?  In MVC land we'd show the initial
search page with an empty results table and on submit pop the results
into a list and add to the model which would be rendered by jsp tags.

Code below which is not pretty and gets results from a Solr engine as
xml:

<lift:surround with="default" at="content">
    <div>
        <lift:PropertySearch.search form="POST">
        <div id="search">
                <s:searchField/><s:submit/>
        </div>
                <s:theresults>
                <table>
                        
<thead><td>Id</td><td>Name</td><td>Location</td><td>Homepage</
td><td>Smoking</td><td>Pets</td></thead>
                        <tbody><result:list><tr><td><r:id/></td><td><r:name/></
td><td><r:location/></td><td><r:url/></td><td><r:smoking/></
td><td><r:pets/></td></tr></result:list></tbody>
                </table>
                </s:theresults>

        </lift:PropertySearch.search>
    </div>

</lift:surround>

--------------

class PropertySearch extends StatefulSnippet {
  var searchTxt = ""
  var results = List[HashMap[String,String]]() ::: Nil

  var dispatch : DispatchIt = {
      case "search" => search _
  }

  def search(xhtml: NodeSeq): NodeSeq = {
    def processSearch() = {
      results = List[HashMap[String,String]]() ::: Nil
      Log.info("Running camel route direct:querySolr to find docs")
          val p = RouteBuilderHelper.getTemplate
          p.start

          var resp = p.sendBodyAndHeader("direct:querySolr",
"direct:querySolr", HttpProducer.QUERY, "q=" + searchTxt)
          val x1 = RouteBuilderHelper.camelResponseToXml(resp)

          Log.info("Received: " + x1)
          Log.info("resul...@numfound=" + x1 \ "result" \ "@numFound")

          if (hasResult(x1)) {
            Log.info("found matches")
            val xmlResults = x1 \ "result"
            Log.info(xmlResults)
            for(result <- xmlResults \\ "doc") {
              val attrMap = new HashMap[String, String]
              for(str <- result \\ "str") {
                val attrName = (str \ "@name").text
                val attrVal = str.text
                Log.info(attrName + "=" + attrVal)
                attrMap += (attrName -> attrVal) // add to map
              }
          for(i <- result \\ "int") {
                val attrName = (i \ "@name").text
                val attrVal = i.text
                Log.info(attrName + "=" + attrVal)
                attrMap += (attrName -> attrVal) // add to map
              }
          for(i <- result \\ "bool") {
                val attrName = (i \ "@name").text
                val attrVal = i.text
                Log.info(attrName + "=" + attrVal)
                attrMap += (attrName -> attrVal) // add to map
              }

          results = results ::: List(attrMap) // add to list
            }
          } else {
            Log.info("No matches")
          }

      p.stop
      Log.info("Finished results: " + results)
    }

    bind("s", xhtml,
         "searchField" -> SHtml.text(searchTxt, searchTxt = _),
         "theresults" -> results.flatMap(r => bind("r", chooseTemplate
("result","list", xhtml),
                                                   "id" -> r
("id"),
                                                   "name" -> r
("name"),
                                                   "location" -> r
("location"),
                                                   "url" -> r("url"),
                                                   "smoking" -> r
("smoking"),"pets" -> r("pets")
         )),
                  "submit" -> SHtml.submit("Search", processSearch)
         )
  }
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to liftweb@googlegroups.com
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to