lift framework

Post on 15-May-2015

3.661 Views

Category:

Education

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

The Lift frameworkat least some parts of

agenda - How we divide

forms in lift

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

some short dive 1 / 2

some short dive 2 / 2

checkbox - text area - password inputs

text area - password inputs

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

“attribute” → ”value”

checkbox

Not submitted if unchecked

hidden element

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

calculations

Link - radio - (multi) selection

Link

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

radio button

Set of radio buttons with String labels

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

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

file upload 2/2

site map

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.

Basic siteMap 1/2

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

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

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-specific Rendering

The mapper and record

frameworks- Making things persistent -

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.

and mapping?

User

passwort:Stringname:Stringstreet:Stringpostnr:Longint

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

Extendnet.liftweb.mapper.ConnectionManagertrait

Implement methods: newConnection andreleaseConnection

Get connected

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

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)

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

how to set fields?

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

Use is to get:

Object relationship - one to many

Object relationship - many to many

Indexing

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

schema mapping

Create database schema according toyour objects

Declared in Boot.scala

Working with entities

MetaMapper is providing a lot ofoperations for an entity:

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

Other Queryparams to search for data

Getting things ordered

from mapping to html

asHTML is rendering the mapper instance„as HTML“.

Overriding for own behaviour eg own date format

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

from form to validation

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

Link function to validation

crud support

Create, read, update, delete: Use CRUDify trait

Doing it oldschool - sql 1/2

findAllByPreparedStatement

findAllByInsecureSql Executes the String you submit directly without

any checks

Doing it oldschool - sql 2/2

DB.runQuery provides support for non-entity SQL queries

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

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

[- Showing some code in eclipse -]

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

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

top related