-
Notifications
You must be signed in to change notification settings - Fork 16
/
chap-lift_architecture.lyx
3216 lines (2341 loc) · 57.2 KB
/
chap-lift_architecture.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.7 created this file. For more info see http://www.lyx.org/
\lyxformat 345
\begin_document
\begin_header
\textclass book
\use_default_options false
\language english
\inputencoding auto
\font_roman default
\font_sans default
\font_typewriter default
\font_default_family default
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\graphics default
\paperfontsize default
\spacing single
\use_hyperref false
\papersize default
\use_geometry false
\use_amsmath 1
\use_esint 1
\cite_engine basic
\use_bibtopic false
\paperorientation portrait
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\defskip medskip
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\author ""
\author ""
\end_header
\begin_body
\begin_layout Chapter
Lift Fundamentals
\begin_inset CommandInset label
LatexCommand label
name "cha:Lift-Architecture"
\end_inset
\end_layout
\begin_layout Standard
In this chapter we will cover some of the fundamental aspects of writing
a lift application, including the architecture of the Lift library and
how it processes requests.
\end_layout
\begin_layout Section
Entry into Lift
\begin_inset CommandInset label
LatexCommand label
name "sec:Entry-into-Lift"
\end_inset
\end_layout
\begin_layout Standard
The first step in Lift's request processing is intercepting the HTTP request.
Originally, Lift used a
\family typewriter
java.servlet.Servlet
\family default
instance to process incoming requests.
This was changed to use a
\family typewriter
java.servlet.Filter
\family default
instance
\begin_inset Foot
status open
\begin_layout Plain Layout
You can see the discussion on the Lift mailing list that led to this change
here:
\begin_inset Flex URL
status open
\begin_layout Plain Layout
http://tinyurl.com/dy9u9d
\end_layout
\end_inset
\end_layout
\end_inset
because this allows the container to handle any requests that Lift does
not (in particular, static content).
The filter acts as a thin wrapper on top of the existing
\family typewriter
LiftServlet
\family default
(which still does all of the work), so don't be confused when you look
at the Lift API and see both classes (
\family typewriter
LiftFilter
\family default
and
\family typewriter
LiftServlet
\family default
).
The main thing to remember is that your
\family typewriter
web.xml
\family default
\begin_inset Index
status open
\begin_layout Plain Layout
web.xml
\end_layout
\end_inset
should specify the filter and not the servlet, as shown in Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:LiftFilter-setup-in-web.xml"
\end_inset
.
\end_layout
\begin_layout Standard
\begin_inset listings
lstparams "language=XML"
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
\begin_inset CommandInset label
LatexCommand label
name "lst:LiftFilter-setup-in-web.xml"
\end_inset
LiftFilter Setup in web.xml
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
<?xml version="1.0" encoding="ISO-8859-1"?>
\end_layout
\begin_layout Plain Layout
<!DOCTYPE web-app
\end_layout
\begin_layout Plain Layout
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
\end_layout
\begin_layout Plain Layout
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">
\end_layout
\begin_layout Plain Layout
<web-app>
\end_layout
\begin_layout Plain Layout
<filter>
\end_layout
\begin_layout Plain Layout
<filter-name>LiftFilter</filter-name>
\end_layout
\begin_layout Plain Layout
<display-name>Lift Filter</display-name>
\end_layout
\begin_layout Plain Layout
<description>The Filter that intercepts lift calls</description>
\end_layout
\begin_layout Plain Layout
<filter-class>net.liftweb.http.LiftFilter</filter-class>
\end_layout
\begin_layout Plain Layout
</filter>
\end_layout
\begin_layout Plain Layout
<filter-mapping>
\end_layout
\begin_layout Plain Layout
<filter-name>LiftFilter</filter-name>
\end_layout
\begin_layout Plain Layout
<url-pattern>/*</url-pattern>
\end_layout
\begin_layout Plain Layout
</filter-mapping>
\end_layout
\begin_layout Plain Layout
</web-app>
\end_layout
\end_inset
\end_layout
\begin_layout Standard
A full
\family typewriter
web.xml
\family default
example is shown in Section
\begin_inset CommandInset ref
LatexCommand vref
reference "lst:JPA-web.xml"
\end_inset
.
In particular, the filter-mapping (lines 13-16) specifies that the Filter
is responsible for everything.
When the filter receives the request, it checks a set of rules to see if
it can handle it.
If the request is one that Lift handles, it passes it on to an internal
\family typewriter
LiftServlet
\family default
instance for processing; otherwise, it chains the request and allows the
container to handle it.
\end_layout
\begin_layout Section
Bootstrap
\begin_inset CommandInset label
LatexCommand label
name "sec:Bootstrap"
\end_inset
\begin_inset Index
status open
\begin_layout Plain Layout
Bootstrap
\end_layout
\end_inset
\end_layout
\begin_layout Standard
When Lift starts up an application there are a number of things that you'll
want to set up before any requests are processed.
These things include setting up a site Menu (called SiteMap, Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "cha:SiteMap"
\end_inset
), URL rewriting (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:URL-Rewriting"
\end_inset
), custom dispatch (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Custom-dispatch-func"
\end_inset
), and classpath search (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:Class-Resolution"
\end_inset
).
The Lift servlet looks for the
\family typewriter
bootstrap.liftweb.Boot
\family default
\begin_inset Index
status open
\begin_layout Plain Layout
Boot
\end_layout
\end_inset
class and executes the
\family typewriter
boot
\family default
method in the class.
You can also specify your own Boot instance by using the
\family typewriter
bootloader
\begin_inset Index
status open
\begin_layout Plain Layout
bootloader
\end_layout
\end_inset
\family default
init param for the
\family typewriter
LiftFilter
\family default
as shown in Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Overriding-bootloader"
\end_inset
\end_layout
\begin_layout Standard
\begin_inset listings
lstparams "language=XML"
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Overriding the Boot Loader Class
\begin_inset CommandInset label
LatexCommand label
name "lst:Overriding-bootloader"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
<filter>
\end_layout
\begin_layout Plain Layout
...
filter setup here ...
\end_layout
\begin_layout Plain Layout
<init-param>
\end_layout
\begin_layout Plain Layout
<param-name>bootloader</param-name>
\end_layout
\begin_layout Plain Layout
<param-value>foo.bar.baz.MyBoot</param-value>
\end_layout
\begin_layout Plain Layout
</init-param>
\end_layout
\begin_layout Plain Layout
</filter>
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Your custom boot class class must subclass
\family typewriter
Bootable
\family default
\begin_inset Foot
status open
\begin_layout Plain Layout
\family typewriter
net.liftweb.http.Bootable
\end_layout
\end_inset
and implement the
\family typewriter
boot
\family default
method.
The
\family typewriter
boot
\family default
method will only be run once, so you can place any initialization calls
for other libraries here as well.
\end_layout
\begin_layout Subsection
Class Resolution
\begin_inset CommandInset label
LatexCommand label
name "sub:Class-Resolution"
\end_inset
\end_layout
\begin_layout Standard
As part of our discussion of the Boot class, it's also important to explain
how Lift determines where to find classes for Views and Snippet rendering
when using implicit dispatch (we'll cover this in Section
\begin_inset CommandInset ref
LatexCommand vref
reference "sub:Implicit-Dispatch-Via"
\end_inset
).
The
\family typewriter
LiftRules.addToPackages
\family default
method tells lift which Scala packages to look in for a given class.
Lift has implicit extensions to the paths you enter: in particular, if
you tell Lift to use the
\family typewriter
com.pocketchangeapp
\family default
package, Lift will look for View classes (Section ???)under
\family typewriter
com.pocketchangeapp.view
\family default
, Comet classes (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:COMET"
\end_inset
) under
\family typewriter
com.pocketchange.comet
\family default
, and Snippet classes (Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "cha:Snippets"
\end_inset
) under
\family typewriter
com.pocketchangeapp.snippet
\family default
.
The
\family typewriter
addToPackages
\family default
method should almost always be executed in your Boot class.
A minimal Boot class would look like:
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
A Minimal Boot Class
\begin_inset CommandInset label
LatexCommand label
name "lst:Minimal-Boot-class"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
class Boot {
\end_layout
\begin_layout Plain Layout
def boot = {
\end_layout
\begin_layout Plain Layout
LiftRules.addToPackages("com.pocketchangeapp")
\end_layout
\begin_layout Plain Layout
}
\end_layout
\begin_layout Plain Layout
}
\end_layout
\end_inset
\end_layout
\begin_layout Section
A Note on Standard Imports
\end_layout
\begin_layout Standard
For the sake of saving space, the following import statements are assumed
for all example code throughout the rest of the book:
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
\begin_inset Caption
\begin_layout Plain Layout
Standard Import Statements
\begin_inset CommandInset label
LatexCommand label
name "lst:Standard-import-statements"
\end_inset
\end_layout
\end_inset
\end_layout
\begin_layout Plain Layout
import net.liftweb.common._
\end_layout
\begin_layout Plain Layout
\end_layout
\begin_layout Plain Layout
import net.liftweb.http._
\end_layout
\begin_layout Plain Layout
import S._
\end_layout
\begin_layout Plain Layout
\end_layout
\begin_layout Plain Layout
import net.liftweb.util._
\end_layout
\begin_layout Plain Layout
import Helpers._
\end_layout
\begin_layout Plain Layout
\end_layout
\begin_layout Plain Layout
import scala.xml._
\end_layout
\end_inset
\end_layout
\begin_layout Section
Lift's Main Objects
\end_layout
\begin_layout Standard
Before we dive into Lift's fundamentals, we want to briefly discuss three
objects you will use heavily in your Lift code.
We'll be covering these in more detail later in this chapter and in further
chapters, so feel free to skip ahead if you want more details.
\end_layout
\begin_layout Subsection
S
\begin_inset Index
status open
\begin_layout Plain Layout
S
\end_layout
\end_inset
object
\begin_inset CommandInset label
LatexCommand label
name "sub:S-object"
\end_inset
\end_layout
\begin_layout Standard
The
\family typewriter
net.liftweb.http.S
\family default
object represents the state of the current request (according to David
Pollak,
\begin_inset Quotes eld
\end_inset
S
\begin_inset Quotes erd
\end_inset
is for Stateful).
As such, it is used to retrieve information about the request and modify
information that is sent in the response.
Among other things, it can be used for notices (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "cha:Message-Handling"
\end_inset
) , cookie management (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Cookies"
\end_inset
), localization/internationalization (Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "cha:Internationalization"
\end_inset
) and redirection (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:HTTP-redirects"
\end_inset
).
\end_layout
\begin_layout Subsection
SHtml
\begin_inset Index
status open
\begin_layout Plain Layout
SHtml
\end_layout
\end_inset
\begin_inset CommandInset label
LatexCommand label
name "sub:SHtml"
\end_inset
\end_layout
\begin_layout Standard
The
\family typewriter
net.liftweb.http.SHtml
\family default
object's main purpose is to define HTML generation functions, particularly
those having to do with form elements.
We cover forms in detail in Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "cha:Forms-in-Lift"
\end_inset
).
In addition to normal form elements, SHtml defines functions for AJAX and
JSON form elements (Chapters
\begin_inset CommandInset ref
LatexCommand ref
reference "cha:AJAX-and-COMET"
\end_inset
and
\begin_inset CommandInset ref
LatexCommand ref
reference "cha:Lift-and-Javascript"
\end_inset
, respectively).
\end_layout
\begin_layout Subsection
LiftRules
\begin_inset Index
status open
\begin_layout Plain Layout
LiftRules
\end_layout
\end_inset
\end_layout
\begin_layout Standard
The
\family typewriter
net.liftweb.http.LiftRules
\family default
object is where the vast majority of Lift's global configuration is handled.
Almost everything that is configurable about Lift is set up based on variables
in
\family typewriter
LiftRules
\family default
.
Because LiftRules spans such a diverse range of functionality, we won't
be covering
\family typewriter
LiftRules
\family default
directly, but as we discuss each Lift mechanism we'll touch on the
\family typewriter
LiftRules
\family default
variables and methods related to the configuration of that mechanism.
\end_layout
\begin_layout Section
The Rendering Process
\begin_inset CommandInset label
LatexCommand label
name "sec:The-Rendering-Process"
\end_inset
\end_layout
\begin_layout Standard
The rest of this chapter, as well as the next few chapters, are dedicated
to the stages of rendering in Lift.
We'll start here by giving a brief overview of the processes by which Lift
transforms a request into a response
\begin_inset Index
status open
\begin_layout Plain Layout
rendering pipeline
\end_layout
\end_inset
.
We're only going to touch on the major points here, although the steps
we do discuss will be in the order that Lift performs them.
A much more detailed tour of the pipeline is given in Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Request/Response-Lifecycle"
\end_inset
.
Starting from the initial invocation on a request, Lift will:
\end_layout
\begin_layout Enumerate
Perform any configured URL rewriting.
This is covered in Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:URL-Rewriting"
\end_inset
.
\end_layout
\begin_layout Enumerate
Execute any matching custom dispatch functions.
This is split into both stateless and stateful dispatch, and will be covered
in more detail in Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Custom-dispatch-func"
\end_inset
.
\end_layout
\begin_layout Enumerate
Perform automatic processing of Comet and AJAX requests (Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "cha:AJAX-and-COMET"
\end_inset
).
\end_layout
\begin_layout Enumerate
Perform SiteMap setup and matching.
SiteMap, covered in Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "cha:SiteMap"
\end_inset
, not only provides a nice site-wide menu system, but can also perform security
control, URL rewrite, and other custom functionality.
\end_layout
\begin_layout Enumerate
Locate the template XHTML to use for the request.
This is handled via three mechanisms:
\end_layout
\begin_deeper
\begin_layout Enumerate
Checking the
\family typewriter
LiftRules.viewDispatch
\begin_inset Index
status open
\begin_layout Plain Layout
viewDispatch
\end_layout
\end_inset
\begin_inset Index
status open
\begin_layout Plain Layout
Views ! Explicit dispatch
\end_layout
\end_inset
\family default
\family typewriter
RulesSeq
\family default
to see if any custom dispatch rules have been defined.
We cover custom view dispatch in Section
\begin_inset CommandInset ref
LatexCommand vref
reference "sec:Request/Response-Lifecycle"
\end_inset
.
\end_layout
\begin_layout Enumerate
If there is no matching
\family typewriter
viewDispatch
\family default
, locate a template
\begin_inset Index
status open
\begin_layout Plain Layout
template
\end_layout