lift framework

51
The Lift framework at least some parts of

Upload: jeffrey-groneberg

Post on 15-May-2015

3.664 views

Category:

Education


2 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Lift Framework

The Lift frameworkat least some parts of

Page 2: Lift Framework

agenda - How we divide

Page 3: Lift Framework

forms in lift

Page 4: Lift Framework

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

Page 5: Lift Framework

some short dive 1 / 2

Page 6: Lift Framework

some short dive 2 / 2

Page 7: Lift Framework

checkbox - text area - password inputs

Page 8: Lift Framework

text area - password inputs

Mostly like standard HTML input elements Generated by Shtml Overload XML with Pair(String, String)

“attribute” → ”value”

Page 9: Lift Framework

checkbox

Not submitted if unchecked

Page 10: Lift Framework

hidden element

Not a plain value like in HTML Defined function is called on submit For logging, debugging or other

calculations

Page 11: Lift Framework

Link - radio - (multi) selection

Page 12: Lift Framework

Link

Like an HTML anchor Defined function is invoked on click Passing objects to linked page

Page 13: Lift Framework

radio button

Set of radio buttons with String labels

Page 14: Lift Framework

(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)

Page 15: Lift Framework

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

Page 16: Lift Framework

file upload 2/2

Page 17: Lift Framework

site map

Page 18: Lift Framework

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.

Page 19: Lift Framework

Basic siteMap 1/2

Page 20: Lift Framework

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

Page 21: Lift Framework

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

Page 22: Lift Framework

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

Page 23: Lift Framework

Page-specific Rendering

Page 24: Lift Framework

The mapper and record

frameworks- Making things persistent -

Page 25: Lift Framework

What is persistence?

Wikipedia

“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.

Page 26: Lift Framework

and mapping?

User

passwort:Stringname:Stringstreet:Stringpostnr:Longint

Page 27: Lift Framework

record vs. mapper

Mapper is original Lift persistenceframework 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

Page 28: Lift Framework

Extendnet.liftweb.mapper.ConnectionManagertrait

Implement methods: newConnection andreleaseConnection

Get connected

Page 29: Lift Framework

Dbvendor extends connectionManager

name parameterused to havemultiple databases

newConnectionneeds to return a Box[java.sql.Connection]

releaseConnectionused to havecomplete control ofthe connectionlifecycle

Declare object in Boot.scala

Call theDB.defineConnectionManager to bind Manager into theMapper

Page 30: Lift Framework

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)

Page 31: Lift Framework

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 forvalidation

getSingleton defines the MetaObject

Page 32: Lift Framework

how to set fields?

Fields are no instance members: Use apply method to set:

Use is to get:

Page 33: Lift Framework

Object relationship - one to many

Page 34: Lift Framework

Object relationship - many to many

Page 35: Lift Framework

Indexing

More complex indexing via usage of theIndex, IndexField and BoundedIndexFieldcase classes

Page 36: Lift Framework

schema mapping

Create database schema according toyour objects

Declared in Boot.scala

Page 37: Lift Framework

Working with entities

MetaMapper is providing a lot ofoperations for an entity:

Page 38: Lift Framework

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

Page 39: Lift Framework

Other Queryparams to search for data

Page 40: Lift Framework

Getting things ordered

Page 41: Lift Framework

from mapping to html

asHTML is rendering the mapper instance„as HTML“.

Overriding for own behaviour eg own date format

Page 42: Lift Framework

from mapping to a form

toForm is making it easy to create a form appropriate to an instance

Third method has a „redo“ snippet parameterto save status User do not have to re-enter everything

Page 43: Lift Framework

from form to validation

Validate submitted data User is not allowed to enter dates in the future

Link function to validation

Page 44: Lift Framework

crud support

Create, read, update, delete: Use CRUDify trait

Page 45: Lift Framework

Doing it oldschool - sql 1/2

findAllByPreparedStatement

findAllByInsecureSql Executes the String you submit directly without

any checks

Page 46: Lift Framework

Doing it oldschool - sql 2/2

DB.runQuery provides support for non-entity SQL queries

Page 47: Lift Framework

Protouser

ProtoUser trait defines some basic fields fora user (email, firstName, lastName, password and superUser)

Already some defs used to format thefields for display or to provide form labels

Page 48: Lift Framework

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

Page 49: Lift Framework

[- Showing some code in eclipse -]

Page 50: Lift Framework

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

Page 51: Lift Framework

Disclaimer

All images in this presentation are downloaded from flickr.com and marked with the creativecommons licence forcommercial use.

This presentation is restricted with the creativecommons licence (http://creativecommons.org/licenses/by-nd/3.0/de/deed.en):