Lift Framework

3,980 views

Published on

This presentation is giving a short overview of forms, site map and mappers in the lift framework.

Published in: Education
  • Be the first to comment

Lift Framework

  1. 1. The Lift framework at least some parts of
  2. 2. agenda - How we divide
  3. 3. forms in lift
  4. 4. some facts  Standard GET/POST support  AJAX and JSON forms support  No HTML form tags needed  Implement as snippet in Scala  Form generated by net.liftweb.http.SHtml  Stateless by default  Extend StatefulSnippet  Use RequestVars for statefull purpose
  5. 5. some short dive 1 / 2
  6. 6. some short dive 2 / 2
  7. 7. checkbox - text area - password inputs
  8. 8. text area - password inputs  Mostly like standard HTML input elements  Generated by Shtml  Overload XML with Pair(String, String)  “attribute” → ”value”
  9. 9. checkbox  Not submitted if unchecked
  10. 10. hidden element  Not a plain value like in HTML  Defined function is called on submit  For logging, debugging or other calculations
  11. 11. Link - radio - (multi) selection
  12. 12. Link  Like an HTML anchor  Defined function is invoked on click  Passing objects to linked page
  13. 13. radio button  Set of radio buttons with String labels
  14. 14. (multi) selection  selection  List of options as Pair(value, display)  Only one item may be selected  multiselection  Multiple select or pre-select  Callback function proceeds for every returned value  selectObj  Handles passed objects as Pair(object, display)
  15. 15. file upload 1/2  Special case of form submission  SHtml generates file upload element with callback  File is typed as FileParamHolder refenced by SHtml  Whole file is loaded into memory on complete
  16. 16. file upload 2/2
  17. 17. site map
  18. 18. site map - facts  Provides a map for your site  Not only links and basic menu functionality  Nested menus with hierarchies  Grouping menus  Access control mechanism  Request URL rewriting  State-dependent computations for page titles, page-specific snippets, etc.
  19. 19. Basic siteMap 1/2
  20. 20. Basic sitemap 2/2  Creating menu entries with unique Loc reference  Allow access to any pages with help of match  Nested menu holds one or more child menus
  21. 21. customize display of menu  Hide menu entries to restrict access  Control menu item text  Render menu title  Customize menu with help of CSS  Add JavaScript functionality  Build menu groups
  22. 22. access control  Extending binary control of given request  "matches→display" otherwise don't display  If LocParam  Test Function evaluates given path  Message Function returns failure response string  User redirected to page with display for error state  Unless LocParam  Mirrors Test Function of If LocParam
  23. 23. Page-specific Rendering
  24. 24. The mapper and record frameworks - Making things persistent -
  25. 25. What is persistence? “Persistence in computer science refers to the characteristic of data that outlives the execution of the program that created it. Without this capability, data would only exist in RAM, and would be lost when this RAM loses power, such as on computer shutdown. This is achieved in practice by storing the data in non-volatile storage such as a hard drive or flash memory. ” Wikipedia
  26. 26. and mapping? User passwort:String name:String street:String postnr:Longint
  27. 27. record vs. mapper  Mapper is original Lift persistence framework  Closely tied to JDBC  Record is new refactorization of Mapper  Define own Mapping Strategies (XML, JDBC, JPA)  Mapper will be deprecated as soon as Records is stable  Mapper will be used in this presentation
  28. 28. Get connected  Extend net.liftweb.mapper.ConnectionManager trait  Implement methods: newConnection and releaseConnection
  29. 29. Dbvendor extends connectionManager  name parameter used to have multiple databases  newConnection needs to return a Box[java.sql.Connecti on]  releaseConnection used to have complete control of the connection lifecycle  Declare object in Boot.scala  Call the DB.defineConnection Manager to bind Manager into the Mapper
  30. 30. make my class persistent  Class Transaction from the PocketChange application  We want to store:  Date  Description with a max length of 100 chars  Amount of money used in a transaction  And a account we want to reference to (described in detail later)
  31. 31. What have I typed here?  Use LongKeyedMapper trait:  Provide primary key for our entity  Key should be long  IdPK trait implements getPrimaryKeyField-method  Defined as object due to MetaMapper, who needs access for validation  getSingleton defines the MetaObject
  32. 32. how to set fields?  Fields are no instance members:  Use apply method to set:  Use is to get:
  33. 33. Object relationship - one to many
  34. 34. Object relationship - many to many
  35. 35. Indexing  More complex indexing via usage of the Index, IndexField and BoundedIndexField case classes
  36. 36. schema mapping  Create database schema according to your objects  Declared in Boot.scala
  37. 37. Working with entities  MetaMapper is providing a lot of operations for an entity:
  38. 38. how to query data  findAll returns all instances  Use flag to define criterias to limit search result (trait QueryParam)  By is used for direct value comparison  BySQL let us use plain SQL as where clause
  39. 39. Other Queryparams to search for data
  40. 40. Getting things ordered
  41. 41. from mapping to html  asHTML is rendering the mapper instance „as HTML“.  Overriding for own behaviour  eg own date format
  42. 42. from mapping to a form  toForm is making it easy to create a form appropriate to an instance  Third method has a „redo“ snippet parameter to save status  User do not have to re-enter everything
  43. 43. from form to validation  Validate submitted data  User is not allowed to enter dates in the future  Link function to validation
  44. 44. crud support  Create, read, update, delete:  Use CRUDify trait
  45. 45. Doing it oldschool - sql 1/2  findAllByPreparedStatement  findAllByInsecureSql  Executes the String you submit directly without any checks
  46. 46. Doing it oldschool - sql 2/2  DB.runQuery provides support for non- entity SQL queries
  47. 47. Protouser  ProtoUser trait defines some basic fields for a user (email, firstName, lastName, password and superUser)  Already some defs used to format the fields for display or to provide form labels
  48. 48. megaprotouser  Extends the ProtoUser trait  Automatically handle all of the scaffolding for a complete user management system  User registration page  Login page  Lost password page  Change password page  User edit page  Simple method to generate SiteMap
  49. 49. [- Showing some code in eclipse -]
  50. 50. conclusion  Concepts are pretty nice  Getting things done very quick out of box  Scala as scripting language makes it mighty  Would have been better to do presentations AFTER project  Pretty hard to „give control“ to the framework („What is happening in the background?“)  Lift book is bad.  We would still prefer (G)Rails due to better tooling and experience
  51. 51. Disclaimer  All images in this presentation are downloaded from flickr.com and marked with the creativecommons licence for commercial use.  This presentation is restricted with the creativecommons licence (http://creativecommons.org/licenses/by- nd/3.0/de/deed.en):

×