forked from dpp/simply_lift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simply_Lift.lyx
4781 lines (3583 loc) · 95.4 KB
/
Simply_Lift.lyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#LyX 1.6.9 created this file. For more info see http://www.lyx.org/
\lyxformat 345
\begin_document
\begin_header
\textclass book
\begin_preamble
\usepackage[notbib]{tocbibind}
\usepackage{breakurl}
\usepackage{textcomp}
\usepackage[htt]{hyphenat}
% redefine the \LyX macro for PDF bookmarks
\def\LyX{\texorpdfstring{%
L\kern-.1667em\lower.25em\hbox{Y}\kern-.125emX\@}
{LyX }}
% "define" Scala
\lstdefinelanguage{scala}{morekeywords={abstract,case,catch,class,def,do,else,extends,false,final,finally,for,forSome,if,implicit,import,lazy,match,new,null,object,override,package,private,protected,return,sealed,super,this,throw,trait,try,true,type,val,var,while,with,yield},
otherkeywords={=>,<-,<:,<\%,>:},
sensitive=true,
morecomment=[l]{//},
morecomment=[s]{/*}{*/},
morestring=[b]"}
\definecolor{darkgreen}{rgb}{0,0.4,0}
% Default settings for code listings
\lstset{frame=tb,
language=scala,
aboveskip=3mm,
belowskip=3mm,
showstringspaces=false,
columns=flexible,
basicstyle={\small\ttfamily},
keywordstyle=\color{blue},
commentstyle=\color{darkgreen},
stringstyle=\color{red},
upquote=true,
showtabs=true,
tab=BADTAB,
numbers=left,
numberstyle=\tiny}
% Fix the List of Listings title
\renewcommand{\lstlistlistingname}{List of Listings}
% Be less rigorous with line breaking
\sloppy
\end_preamble
\use_default_options true
\begin_modules
theorems-ams
eqs-within-sections
figs-within-sections
\end_modules
\language english
\inputencoding auto
\font_roman palatino
\font_sans default
\font_typewriter courier
\font_default_family default
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\graphics default
\paperfontsize 11
\spacing single
\use_hyperref true
\pdf_title "Simply Lift"
\pdf_author "David Pollak"
\pdf_bookmarks true
\pdf_bookmarksnumbered true
\pdf_bookmarksopen false
\pdf_bookmarksopenlevel 1
\pdf_breaklinks true
\pdf_pdfborder true
\pdf_colorlinks true
\pdf_backref false
\pdf_pdfusetitle false
\pdf_quoted_options "urlcolor=blue,linkcolor=blue"
\papersize letterpaper
\use_geometry true
\use_amsmath 1
\use_esint 1
\cite_engine basic
\use_bibtopic false
\paperorientation portrait
\leftmargin 1in
\topmargin 1in
\rightmargin 1in
\bottommargin 1in
\secnumdepth 2
\tocdepth 2
\paragraph_separation skip
\defskip medskip
\quotes_language english
\papercolumns 1
\papersides 2
\paperpagestyle headings
\tracking_changes false
\output_changes false
\author ""
\author ""
\end_header
\begin_body
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
\backslash
frontmatter
\end_layout
\end_inset
\end_layout
\begin_layout Title
Simply Lift
\end_layout
\begin_layout Author
David Pollak
\end_layout
\begin_layout Date
September 8, 2011
\end_layout
\begin_layout Standard
\noindent
Copyright © 2010-2011 by David Pollak
\begin_inset Newline newline
\end_inset
This document is licensed Creative Commons Attribution, Non Commercial,
No Derivatives:
\begin_inset Flex URL
status open
\begin_layout Plain Layout
http://creativecommons.org/licenses/by-nc-nd/3.0/
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset CommandInset toc
LatexCommand tableofcontents
\end_inset
\end_layout
\begin_layout Standard
\begin_inset FloatList figure
\end_inset
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
\backslash
lstlistoflistings
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
\backslash
mainmatter
\end_layout
\end_inset
\end_layout
\begin_layout Part
The Lift Web Framework
\end_layout
\begin_layout Chapter
Introduction
\end_layout
\begin_layout Standard
The Lift Web Framework provides web application developers tools to make
writing security, interacting, scalable web applications easier than with
any other web framework.
After reading Part I of this book, you should understand Lift's core concepts
and be able to write Lift applications.
But with anything, practice is important.
I have been writing Lift and Scala for 4 years, and even I learn new things
about the language and the framework on a weekly basis.
Please consider Lift an path and an exploration, rather than an end point.
\end_layout
\begin_layout Standard
\begin_inset Quotes eld
\end_inset
Yo, David, stop yer yappin'.
I'm coming from Rails|Spring|Struts|Django and I want to get started super
fast with Lift.
\begin_inset Quotes erd
\end_inset
See From MVC (
\begin_inset CommandInset ref
LatexCommand vref
reference "cha:From-MVC"
\end_inset
).
\end_layout
\begin_layout Standard
Lift is built on top of the
\begin_inset CommandInset href
LatexCommand href
name "Scala"
target "http://scala-lang.org"
\end_inset
programming language.
Scala runs on the
\begin_inset CommandInset href
LatexCommand href
name "Java Virtual Machine"
target "http://www.oracle.com/technetwork/java/index.html"
\end_inset
.
Lift applications are typically packaged as
\begin_inset CommandInset href
LatexCommand href
name "WAR"
target "http://en.wikipedia.org/wiki/WAR_(Sun_file_format)"
\end_inset
files and run as a
\begin_inset CommandInset href
LatexCommand href
name "J/EE Servlets"
target "http://www.oracle.com/technetwork/java/index-jsp-135475.html"
\end_inset
or Servlet Filters.
This book will provide you with the core concepts you need to successfully
write Lift web applications.
The book assumes knowledge of Servlets and Servlet containers, the Scala
Language (Chapters 1-6 of
\shape italic
\begin_inset CommandInset href
LatexCommand href
name "Beginning Scala"
target "http://apress.com/book/view/9781430219897"
\end_inset
\shape default
gives you a good grounding in the language), build tools, program editors,
web development including HTML and JavaScript, etc.
Further, this book will not explore persistence.
Lift has additional modules for persisting to relational and non-relational
data stores.
Lift doesn't distinguish as to how an object is materialized into the address
space...
Lift can treat any object any old way you want.
There are many resources (including
\begin_inset CommandInset href
LatexCommand href
name "Exploring Lift"
target "http://exploring.liftweb.net/"
\end_inset
) that cover ways to persist data from a JVM.
\end_layout
\begin_layout Standard
Lift is different from most web frameworks and it's likely that Lift's differenc
es will present a challenge and a friction if you are familiar with the
MVC
\begin_inset Index
status open
\begin_layout Plain Layout
MVC
\end_layout
\end_inset
school of web frameworks
\begin_inset Foot
status open
\begin_layout Plain Layout
This includes Ruby on Rails, Struts, Java Server Faces, Django, TurboGears,
etc.
\end_layout
\end_inset
.
But Lift is different and Lift's differences give you more power to create
interactive applications.
Lift's differences lead to more concise web applications.
Lift's differences result in more secure and scalable applications.
Lift's differences let you be more productive and make maintaining applications
easier for the future you or whoever is writing your applications.
Please relax and work to understand Lift's differences...
and see how you can make best use of Lift's features to build your web
applications.
\end_layout
\begin_layout Standard
Lift creates abstractions that allow easier expression of business logic
and then maps those abstractions to HTTP and HTML.
This approach differs from traditional web frameworks which build abstractions
on top of HTTP and HTML and require the developer to bridge between common
business logic patterns and the underlying protocol.
The difference means that you spend more time thinking about your application
and less time thinking about the plumbing.
\end_layout
\begin_layout Standard
I am a
\begin_inset Quotes eld
\end_inset
concept learner.
\begin_inset Quotes erd
\end_inset
I learn concepts and then apply them over and over again as situations
come up.
This book focuses a lot on the concepts.
If you're a concept learner and like my stream on conciousness style, this
book will likely suit you well.
On the other hand, it may not.
\end_layout
\begin_layout Standard
Up to date versions of this book are available in PDF form at
\begin_inset Flex URL
status collapsed
\begin_layout Plain Layout
http://simply.liftweb.net/Simply_Lift.pdf
\end_layout
\end_inset
.
The source code for this book is available at
\begin_inset CommandInset href
LatexCommand href
name "https://github.com/dpp/simply_lift"
target "https://github.com/dpp/simply_lift"
\end_inset
.
\end_layout
\begin_layout Standard
If you've got questions, feedback, or improvements to this document, please
join the conversation on the
\begin_inset CommandInset href
LatexCommand href
name "Lift Google Group"
target "http://groups.google.com/group/liftweb"
\end_inset
.
\end_layout
\begin_layout Standard
I'm a
\begin_inset Quotes eld
\end_inset
roll up your sleaves and get your hands dirty with code
\begin_inset Quotes erd
\end_inset
kinda guy...
so let's build a simple Chat application in Lift.
This application will allow us to demonstrate some of Lift's core features
as well as giving a
\begin_inset Quotes eld
\end_inset
smack in the face
\begin_inset Quotes erd
\end_inset
demonstration of how Lift is different.
\end_layout
\begin_layout Chapter
\begin_inset CommandInset label
LatexCommand label
name "cha:The-ubiquitous-Chat"
\end_inset
The ubiquitous Chat app
\end_layout
\begin_layout Standard
Writing a multi-user chat application in Lift is super-simple and illustrates
many of Lift's core concepts.
\end_layout
\begin_layout Standard
The Source Code can be found at
\begin_inset CommandInset href
LatexCommand href
target "https://github.com/dpp/simply_lift/tree/master/chat"
\end_inset
.
\end_layout
\begin_layout Section
The View
\end_layout
\begin_layout Standard
When writing a Lift app, it's often best to start off with the user interface...
build what the user will see and then add behavior to the HTML page.
So, let's look at the Lift template that will make up our chat application.
\end_layout
\begin_layout Standard
\begin_inset CommandInset include
LatexCommand lstinputlisting
filename "chat/src/main/webapp/index.html"
lstparams "caption={index.html},language=xml"
\end_inset
\end_layout
\begin_layout Standard
It's a valid HTML page, but there are some hinky looking class attributes.
The first one is
\family typewriter
<body class="lift:content_id=main">
\family default
.
The class in this case says
\begin_inset Quotes eld
\end_inset
the actual page content is contained by the element with id='main'.
\begin_inset Quotes erd
\end_inset
This allows you to have valid HTML pages for each of your templates, but
dynamically add
\begin_inset Quotes eld
\end_inset
chrome
\begin_inset Quotes erd
\end_inset
around the content based on one or more chrome templates.
\end_layout
\begin_layout Standard
Let's look at the
\family typewriter
<div id="main">
\family default
.
It's got a funky class as well:
\family typewriter
lift:surround?with=default;at=content
\family default
.
This class invokes a snippet which surrounds the
\family typewriter
<div>
\family default
with the default template and inserts the
\family typewriter
<div>
\family default
and its children at the element with id
\begin_inset Quotes eld
\end_inset
content
\begin_inset Quotes erd
\end_inset
in the default template.
Or, it wraps the default chrome around the
\family typewriter
<div>
\family default
.
For more on snippets, see
\begin_inset CommandInset ref
LatexCommand vref
reference "sec:Snippets"
\end_inset
.
\end_layout
\begin_layout Standard
Next, we define how we associate dynamic behavior with the list of chat
elements:
\family typewriter
<div class="lift:comet?type=Chat">
\family default
.
The
\begin_inset Quotes eld
\end_inset
comet
\begin_inset Quotes erd
\end_inset
snippet looks for a class named
\family typewriter
Chat
\family default
that extends
\family typewriter
CometActor
\family default
and enables the mechanics of pushing content from the
\family typewriter
CometActor
\family default
to the browser when the state of the
\family typewriter
CometActor
\family default
changes.
\end_layout
\begin_layout Section
The Chat Comet component
\end_layout
\begin_layout Standard
The
\begin_inset CommandInset href
LatexCommand href
name "Actor Model"
target "http://en.wikipedia.org/wiki/Actor_model"
\end_inset
provides state in functional languages include Erlang.
Lift has an Actor library and LiftActors (see
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:LiftActor"
\end_inset
) provides a powerful state and concurrency model.
This may all seem abstract, so let's look at the
\family typewriter
Chat
\family default
class.
\end_layout
\begin_layout Standard
\begin_inset CommandInset include
LatexCommand lstinputlisting
filename "chat/src/main/scala/code/comet/Chat.scala"
lstparams "caption={Chat.scala},label={Chat}"
\end_inset
\end_layout
\begin_layout Standard
The
\family typewriter
Chat
\family default
component has private state, registers with the
\family typewriter
ChatServer
\family default
, handles incoming messages and can render itself.
Let's look at each of those pieces.
\end_layout
\begin_layout Standard
The private state, like any private state in prototypical object oriented
code, is the state that defines the object's behavior.
\end_layout
\begin_layout Standard
registerWith is a method that defines what component to register the Chat
component with.
Registration is a part of the Listener (or
\begin_inset CommandInset href
LatexCommand href
name "Observer"
target "http://en.wikipedia.org/wiki/Observer_pattern"
\end_inset
) pattern.
We'll look at the definition of the
\family typewriter
ChatServer
\family default
in a minute.
\end_layout
\begin_layout Standard
The
\family typewriter
lowPriority
\family default
method defines how to process incoming messages.
In this case, we're Pattern Matching (see
\begin_inset CommandInset ref
LatexCommand prettyref
reference "sec:Pattern-Matching"
\end_inset
) the incoming message and if it's a
\family typewriter
Vector[String]
\family default
, then we perform the action of setting our local state to the
\family typewriter
Vector
\family default
and re-rendering the component.
The re-rendering will force the changes out to any browser that is displaying
the component.
\end_layout
\begin_layout Standard
We define how to
\family typewriter
render
\family default
the component by defining the CSS to match and the replacement (See
\begin_inset CommandInset ref
LatexCommand prettyref
reference "sec:CSS-Selector-Transforms"
\end_inset
).
We match all the
\family typewriter
<li>
\family default
tags of the template and for each message, create an
\family typewriter
<li>
\family default
tag with the child nodes set to the message.
Additionally, we clear all the elements that have the
\family typewriter
clearable
\family default
in the
\family typewriter
class
\family default
attribute.
\end_layout
\begin_layout Standard
That's it for the Chat CometActor component.
\end_layout
\begin_layout Section
The
\family typewriter
ChatServer
\end_layout
\begin_layout Standard
The ChatServer code is:
\end_layout
\begin_layout Standard
\begin_inset CommandInset include
LatexCommand lstinputlisting
filename "chat/src/main/scala/code/comet/ChatServer.scala"
lstparams "caption={ChatServer.scala},label={ChatServer.scala}"
\end_inset
\end_layout
\begin_layout Standard
The
\family typewriter
ChatServer
\family default
is defined as an
\family typewriter
object
\family default
rather than a
\family typewriter
class
\family default
.
This makes it a singleton which can be referenced by the name
\family typewriter
ChatServer
\family default
anywhere in the application.
Scala's singletons differ from Java's
\family typewriter
static
\family default
in that the singleton is an instance of an object and that instance can
be passed around like any other instance.
This is why we can return the
\family typewriter
ChatServer
\family default
instance from the
\family typewriter
registerWith
\family default
method in that
\family typewriter
Chat
\family default
component.
\end_layout
\begin_layout Standard
The ChatServer has private state, a
\family typewriter
Vector[String]
\family default
representing the list of chat messages.
Note that Scala's type inferencer infers the type of
\family typewriter
msgs
\family default
so you do not have to explicitly define it.
\end_layout
\begin_layout Standard
The
\family typewriter
createUpdate
\family default
method generates an update to send to listeners.
This update is sent when a listener registers with the
\family typewriter
ChatServer
\family default
or when the
\family typewriter
updateListeners()
\family default
method is invoked.
\end_layout
\begin_layout Standard
Finally, the
\family typewriter
lowPriority
\family default
method defines the messages that this component can handle.
If the
\family typewriter
ChatServer
\family default
receives a
\family typewriter
String
\family default
as a message, it appends the
\family typewriter
String
\family default
to the
\family typewriter
Vector
\family default
of messages and updates listeners.
\end_layout
\begin_layout Section
User Input
\end_layout
\begin_layout Standard
Let's go back to the view and see how the behavior is defined for adding
lines to the chat.
\end_layout
\begin_layout Standard
\family typewriter
<form class="lift:form.ajax">
\family default
defines an input form and the
\family typewriter
form.ajax
\family default
snippet turns a form into an Ajax (see
\begin_inset CommandInset ref
LatexCommand prettyref
reference "sec:Ajax"
\end_inset
) form that will be submitted back to the server without causing a full
page load.
\end_layout
\begin_layout Standard
Next, we define the input form element:
\family typewriter
<input class="lift:ChatIn" id="chat_in"/>
\family default
.
It's a plain old input form, but we've told Lift to modify the
\family typewriter
<input>
\family default
's behavior by calling the
\family typewriter
ChatIn
\family default
snippet.
\end_layout
\begin_layout Section
Chat In
\end_layout
\begin_layout Standard
The
\family typewriter
ChatIn
\family default
snippet (See
\begin_inset CommandInset ref
LatexCommand prettyref
reference "sec:Snippets"
\end_inset
) is defined as:
\end_layout
\begin_layout Standard
\begin_inset CommandInset include
LatexCommand lstinputlisting
filename "chat/src/main/scala/code/snippet/ChatIn.scala"
lstparams "caption={ChatIn.scala},label={ChatIn.scala}"
\end_inset
\end_layout
\begin_layout Standard
The code is very simple.
The snippet is defined as a method that associates a function with form
element submission,
\family typewriter
onSubmit
\family default
.
When the element is submitted, be that normal form submission, Ajax, or
whatever, the function is applied to the value of the form.
In English, when the user submits the form, the function is called with
the user's input.
\end_layout
\begin_layout Standard
The function sends the input as a message to the
\family typewriter
ChatServer
\family default
and returns JavaScript that sets the value of the input box to a blank
string.
\end_layout
\begin_layout Section
Running it
\end_layout
\begin_layout Standard
Running the application is easy.
Make sure you've got Java 1.6 or better installed on your machine.
Change directories into the
\family typewriter
chat
\family default
directory and type
\family typewriter
sbt
\family default
and inside the sbt prompt, type
\family typewriter
container:start
\family default
.
The Simple Build Tool will download all necessary dependencies, compile
the program and run it.
\end_layout
\begin_layout Standard
You can point a couple of browsers to
\begin_inset Flex URL
status collapsed
\begin_layout Plain Layout
http://localhost:8080
\end_layout
\end_inset
and start chatting.
\end_layout
\begin_layout Standard
Oh, and for fun, try entering
\family typewriter
<script>alert('I ownz your browser');<script>
\family default
and see what happens.
You'll note it's what you want to happen.
\end_layout
\begin_layout Section
What you don't see
\end_layout