Skip to content
This repository
tree: 6a25dd0d9e
Fetching contributors…

Cannot retrieve contributors at this time

file 3199 lines (2332 sloc) 58.418 kb
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199
#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 _root_.net.liftweb.http._
\end_layout

\begin_layout Plain Layout

import S._
\end_layout

\begin_layout Plain Layout

import _root_.net.liftweb.util._
\end_layout

\begin_layout Plain Layout

import Helpers._
\end_layout

\begin_layout Plain Layout

import _root_.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

\end_inset

 file that matches and use it.
 We'll cover templates, and how they're located, in Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Templates"

\end_inset

.
\end_layout

\begin_layout Enumerate
If no templates files can be located, attempting to locate a view
\begin_inset Index
status open

\begin_layout Plain Layout
Views
\end_layout

\end_inset

 based on implicit dispatch.
 We'll cover views in Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Views"

\end_inset

.
\end_layout

\end_deeper
\begin_layout Enumerate
Process the template, including embedding of other templates (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:embed-tag"

\end_inset

), merging
\family typewriter
<head/>
\family default
 elements from composited templates (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:Head-Merge"

\end_inset

), and executing snippet functions (Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "cha:Snippets"

\end_inset

).
\end_layout

\begin_layout Standard
The rest of this chapter will be devoted in part to the early stages of
 the rendering pipeline, as well as some notes on some general functionality
 in Lift like redirects
\begin_inset Index
status open

\begin_layout Plain Layout
redirect
\end_layout

\end_inset

.
\end_layout

\begin_layout Section
Notices, Warnings, and Error Messages
\begin_inset CommandInset label
LatexCommand label
name "sub:Notices/Warnings/Errors-messages"

\end_inset


\end_layout

\begin_layout Standard
Feedback to the user is important.
 The application must be able to notify the user of errors, warn the user
 of potential problems, and notify the user when system status changes.
 Lift provides a unified model for such messages that can be used for static
 pages as well as for AJAX and Comet calls.
 We cover messaging support in Appendix
\begin_inset CommandInset ref
LatexCommand ref
reference "cha:Message-Handling"

\end_inset

.
\end_layout

\begin_layout Section
URL Rewriting
\begin_inset CommandInset label
LatexCommand label
name "sec:URL-Rewriting"

\end_inset


\end_layout

\begin_layout Standard
\begin_inset Note Note
status open

\begin_layout Plain Layout
In 2.0 this bifurcates into stateful vs stateless rewrite, so we need to
 discuss both
\end_layout

\end_inset

Now that we've gone over Templates, Views, Snippets, and how requests are
 dispatched to a Class.method, we can discuss how to intercept requests and
 handle them the way we want to.
 URL rewriting
\begin_inset Index
status open

\begin_layout Plain Layout
rewriting
\end_layout

\end_inset


\begin_inset Index
status open

\begin_layout Plain Layout
URL rewriting
\end_layout

\end_inset

 is the mechanism that allows you to modify the incoming request so that
 it dispatches to a different URL.
 It can be used, among other things, to allow you to:
\end_layout

\begin_layout Itemize
Use user-friendly, bookmarkable URLs like
\family typewriter
http://www.example.com/budget/2008
\end_layout

\begin_layout Itemize
Use short URLs instead of long, hard to remember ones, similar to
\begin_inset CommandInset href
LatexCommand href
target "http://tinyurl.com"

\end_inset


\end_layout

\begin_layout Itemize
Use portions of the URL to determine how a particular snippet or view responds.
 For example, you could make it so that a user's profile is displayed via
 a URL such as
\begin_inset Newline linebreak
\end_inset


\family typewriter
http://someplace.com/user/derek
\family default
 instead of having the username sent as part of a query string.
\end_layout

\begin_layout Standard
The mechanism is fairly simple to set up.
 We need to write a partial function from a RewriteRequest to a RewriteResponse
 to determine if and how we want to rewrite particular requests.
 Once we have the partial function, we modify the
\family typewriter
LiftRules.rewrite
\family default
 configuration to hook into Lift's processing chain.
 The simplest way to write a partial function is with Scala's match statement,
 which will allow us to selectively match on some or all of the request
 information.
 (Recall that for a partial function, the matches do not have to be exhaustive.
 In the instance that no RewriteRequest matches, no RewriteResponse will
 be generated.) It is also important to understand that when the rewrite
 functions run, the Lift session has not yet been created.
 This means that you generally can't set or access properties in the S object.
 RewriteRequest is a
\family typewriter
case
\family default
 object that contains three items: the parsed path, the request type and
 the original HttpServletRequest
\begin_inset Index
status collapsed

\begin_layout Plain Layout
HttpServletRequest
\end_layout

\end_inset

 object.
 (If you are not familiar with case classes, you may wish to review the
 Scala documentation for them.
 Adding the
\family typewriter
case
\family default
 modifier to a class results in some nice syntactic conveniences.)
\end_layout

\begin_layout Standard
The parsed path of the request is in a ParsePath
\begin_inset Index
status collapsed

\begin_layout Plain Layout
ParsePath
\end_layout

\end_inset

 
\family typewriter
case
\family default
 class instance.
 The ParsePath class contains
\end_layout

\begin_layout Enumerate
The parsed path as a List[String]
\end_layout

\begin_layout Enumerate
The suffix of the request (i.e.
 
\begin_inset Quotes eld
\end_inset

html
\begin_inset Quotes erd
\end_inset

,
\begin_inset Quotes eld
\end_inset

xml
\begin_inset Quotes erd
\end_inset

, etc)
\end_layout

\begin_layout Enumerate
Whether this path is root-relative path.
 If true, then it will start with /<context-path>, followed by the rest
 of the path.
 For example, if your application is deployed on the app context path (
\begin_inset Quotes eld
\end_inset

/app
\begin_inset Quotes erd
\end_inset

) and we want to reference the file <webapp-folder>/pages/index.html, then
 the root-relative path will be /app/pages/index.html.
 
\end_layout

\begin_layout Enumerate
Whether the path ends in a slash (
\begin_inset Quotes eld
\end_inset

/
\begin_inset Quotes erd
\end_inset

)
\end_layout

\begin_layout Standard
The latter three properties are useful only in specific circumstances, but
 the parsed path is what lets us work magic.
 The path of the request is defined as the parts of the URI between the
 context path and the query string.
 The following table shows examples of parsed paths for a Lift application
 under the
\begin_inset Quotes eld
\end_inset

myapp
\begin_inset Quotes erd
\end_inset

 context path:
\end_layout

\begin_layout Standard
\align center
\begin_inset Tabular
<lyxtabular version="3" rows="4" columns="2">
<features>
<column alignment="left" valignment="top" width="0">
<column alignment="left" valignment="top" width="0">
<row>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout

\series bold
\size footnotesize
Requested URL
\end_layout

\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout

\series bold
\size footnotesize
Parsed Path
\end_layout

\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout

\size footnotesize
http://foo.com/myapp/home?test_this=true
\end_layout

\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout

\size footnotesize
List[String](
\begin_inset Quotes eld
\end_inset

home
\begin_inset Quotes erd
\end_inset

)
\end_layout

\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" leftline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout

\size footnotesize
http://foo.com/myapp/user/derek
\end_layout

\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout

\size footnotesize
List[String](
\begin_inset Quotes eld
\end_inset

user
\begin_inset Quotes erd
\end_inset

,
\begin_inset Quotes eld
\end_inset

derek
\begin_inset Quotes erd
\end_inset

)
\end_layout

\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout

\size footnotesize
http://foo.com/myapp/view/item/14592
\end_layout

\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout

\size footnotesize
List[String](
\begin_inset Quotes eld
\end_inset

view
\begin_inset Quotes erd
\end_inset

,
\begin_inset Quotes erd
\end_inset

item
\begin_inset Quotes erd
\end_inset

,
\begin_inset Quotes erd
\end_inset

14592
\begin_inset Quotes erd
\end_inset

)
\end_layout

\end_inset
</cell>
</row>
</lyxtabular>

\end_inset


\end_layout

\begin_layout Standard
The RequestType maps to one of the five HTTP methods: GET, POST, HEAD, PUT
 and DELETE.
 These are represented by the corresponding GetRequest, PostRequest, etc.
 case classes, with an UnknownRequest case class to cover anything strange.
\end_layout

\begin_layout Standard
The flexibility of Scala's matching system is what really makes this powerful.
 In particular, when matching on Lists, we can match parts of the path and
 capture others.
 For example, suppose we'd like to rewrite the
\family typewriter
/account/<account name>
\family default
 path so that it's handled by the
\family typewriter
/viewAcct
\family default
 template as shown in Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Simple-rewrite-example"

\end_inset

.
 In this case we provide two rewrites.
 The first matches /account/<account name> and redirects it to the /viewAcct
 template, passing the acctName as a
\begin_inset Quotes eld
\end_inset

name
\begin_inset Quotes erd
\end_inset

 parameter.
 The second matches /account/<account name>/<tag>, redirecting it to /viewAcct
 as before, but passing both the
\begin_inset Quotes eld
\end_inset

name
\begin_inset Quotes erd
\end_inset

 and a
\begin_inset Quotes eld
\end_inset

tag
\begin_inset Quotes erd
\end_inset

 parameter with the acctName and tag matches from the ParsePath, respectively.
 Remember that the underscore (_) in these matching statements means that
 we don't care what that parameter is, i.e., match anything in that spot.
\end_layout

\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

\begin_inset Caption

\begin_layout Plain Layout
A Simple Rewrite Example
\begin_inset CommandInset label
LatexCommand label
name "lst:Simple-rewrite-example"

\end_inset


\end_layout

\end_inset


\end_layout

\begin_layout Plain Layout

LiftRules.rewrite.append {
\end_layout

\begin_layout Plain Layout

  case RewriteRequest(
\end_layout

\begin_layout Plain Layout

         ParsePath(List("account",acctName),_,_,_),_,_) =>
\end_layout

\begin_layout Plain Layout

         RewriteResponse("viewAcct" :: Nil, Map("name" -> acctName))
\end_layout

\begin_layout Plain Layout

  case RewriteRequest(
\end_layout

\begin_layout Plain Layout

         ParsePath(List("account",acctName, tag),_,_,_),_,_) =>
\end_layout

\begin_layout Plain Layout

         RewriteResponse("viewAcct" :: Nil, Map("name" -> acctName,
\end_layout

\begin_layout Plain Layout

                                                "tag" -> tag)))
\end_layout

\begin_layout Plain Layout

}
\end_layout

\end_inset


\end_layout

\begin_layout Standard
The RewriteResponse
\begin_inset Index
status collapsed

\begin_layout Plain Layout
RewriteResponse
\end_layout

\end_inset

 simply contains the new path to follow.
 It can also take a Map
\begin_inset Index
status collapsed

\begin_layout Plain Layout
Map
\end_layout

\end_inset

 that contains parameters that will be accessible via S.param
\begin_inset Index
status collapsed

\begin_layout Plain Layout
S.param
\end_layout

\end_inset


\begin_inset Index
status collapsed

\begin_layout Plain Layout
S
\end_layout

\end_inset

 in the snippet or view.
 As we stated before, the LiftSession (and therefore most of S) isn't available
 at this time, so the Map is the only way to pass information on to the
 rewritten location.
 
\end_layout

\begin_layout Standard
We can combine the ParsePath matching with the RequestType and HttpServletReques
t to be very specific with our matches.
 For example, if we wanted to support the DELETE HTTP verb for a RESTful
\begin_inset Foot
status open

\begin_layout Plain Layout
\begin_inset ERT
status open

\begin_layout Plain Layout


\backslash
url{http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm}
\end_layout

\end_inset


\end_layout

\end_inset


\begin_inset Index
status open

\begin_layout Plain Layout
RESTful
\end_layout

\end_inset

 interface through an existing template, we could redirect as shown in Listing
 
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Complex-rewrite-example"

\end_inset

.
\end_layout

\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

\begin_inset Caption

\begin_layout Plain Layout
A Complex Rewrite Example
\begin_inset CommandInset label
LatexCommand label
name "lst:Complex-rewrite-example"

\end_inset


\end_layout

\end_inset


\end_layout

\begin_layout Plain Layout

LiftRules.rewrite.append {
\end_layout

\begin_layout Plain Layout

  case RewriteRequest(ParsePath("username" :: Nil, _, _, _),
\end_layout

\begin_layout Plain Layout

                      DeleteRequest,
\end_layout

\begin_layout Plain Layout

                      httpreq)
\end_layout

\begin_layout Plain Layout

                      if isMgmtSubnet(httpreq.getRemoteHost()) =>
\end_layout

\begin_layout Plain Layout

       RewriteResponse("deleteUser" :: Nil, Map("username" -> username))
\end_layout

\begin_layout Plain Layout

}
\end_layout

\end_inset


\end_layout

\begin_layout Standard
We'll go into more detail about how you can use this in the following sections.
 In particular, SiteMap
\begin_inset Index
status collapsed

\begin_layout Plain Layout
SiteMap
\end_layout

\end_inset

 (Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "cha:SiteMap"

\end_inset

) provides a mechanism for doing rewrites combined with menu entries.
\end_layout

\begin_layout Section
Custom Dispatch Functions
\begin_inset CommandInset label
LatexCommand label
name "sec:Custom-dispatch-func"

\end_inset


\end_layout

\begin_layout Standard
Once the rewriting phase is complete (whether we pass through or are redirected)
, the next phase is to determine whether there should be a custom dispatch
 for the request.
 A custom dispatch allows you to handle a matching request directly by a
 method instead of going through the template lookup system.
 Because it bypasses templating, you're responsible for the full content
 of the response.
 A typical use case would be a web service
\begin_inset Index
status collapsed

\begin_layout Plain Layout
web service
\end_layout

\end_inset

 returning XML or a service to return, say, a generated image or PDF.
 In that sense, the custom dispatch mechanism allows you to write your own
 
\begin_inset Quotes eld
\end_inset

sub-servlets
\begin_inset Quotes erd
\end_inset

 without all the mess of implementing the interface and configuring them
 in web.xml
\begin_inset Index
status collapsed

\begin_layout Plain Layout
web.xml
\end_layout

\end_inset

.
\end_layout

\begin_layout Standard
As with rewriting, custom dispatch is realized via a partial function.
 In this case, it's a function of type
\family typewriter
PartialFunction[Req,
\begin_inset Formula $()\Rightarrow Box[LiftResponse]$
\end_inset

]
\family default
 that does the work.
 The
\family typewriter
Req
\family default
 is similar to the
\family typewriter
RewriteRequest
\family default
 case class: it provides the path as a
\family typewriter
List[String]
\family default
, the suffix of the request, and the
\family typewriter
RequestType
\family default
.
 There are three ways that you can set up a custom dispatch function:
\end_layout

\begin_layout Enumerate
Globally, via
\family typewriter
LiftRules.dispatch
\family default

\begin_inset Index
status open

\begin_layout Plain Layout
LiftRules ! dispatch
\end_layout

\end_inset


\begin_inset Index
status open

\begin_layout Plain Layout
Custom dispatch ! stateful
\end_layout

\end_inset


\end_layout

\begin_layout Enumerate
Globally, via
\family typewriter
LiftRules.statelessDispatchTable
\family default

\begin_inset Index
status open

\begin_layout Plain Layout
LiftRules ! statelessDispatchTable
\end_layout

\end_inset


\begin_inset Index
status open

\begin_layout Plain Layout
Custom dispatch ! stateless
\end_layout

\end_inset


\end_layout

\begin_layout Enumerate
Per-Session, via
\family typewriter
S.addHighLevelSessionDispatcher
\family default

\begin_inset Index
status open

\begin_layout Plain Layout
S ! addHighLevelSessionDispatcher
\end_layout

\end_inset


\begin_inset Index
status open

\begin_layout Plain Layout
Custom dispatch ! per-session
\end_layout

\end_inset


\end_layout

\begin_layout Standard
If you attach the dispatch function via
\family typewriter
LiftRules.dispatch
\family default
 or
\family typewriter
S.addHighLevelSessionDispatcher
\family default
, then you'll have full access to the
\family typewriter
S
\family default
 object,
\family typewriter
SessionVar
\family default
s and
\family typewriter
LiftSession
\family default
; if you use
\family typewriter
LiftRules.statelessDispatchTable
\family default
 instead, then these aren't available.
 The result of the dispatch should be a function that returns a
\family typewriter
Box[LiftResponse]
\family default
.
 If the function returns
\family typewriter
Empty
\family default
, then Lift returns a
\begin_inset Quotes eld
\end_inset

404 Not Found
\begin_inset Quotes erd
\end_inset

 response.
\end_layout

\begin_layout Standard
As a concrete example, let's look at returning a generated chart image from
 our application.
 There are several libraries for charting, but we'll take a look at JFreeChart
 in particular.
 First, let's write a method that will chart our account balances by month
 for the last year:
\end_layout

\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

\begin_inset Caption

\begin_layout Plain Layout
A Charting Method
\begin_inset CommandInset label
LatexCommand label
name "lst:Charting-method"

\end_inset


\end_layout

\end_inset


\end_layout

\begin_layout Plain Layout

def chart (endDate : String) : Box[LiftResponse] = {
\end_layout

\begin_layout Plain Layout

  // Query, set up chart, etc...
\end_layout

\begin_layout Plain Layout

  val buffered = balanceChart.createBufferedImage(width,height)
\end_layout

\begin_layout Plain Layout

  val chartImage = ChartUtilities.encodeAsPNG(buffered)
\end_layout

\begin_layout Plain Layout

  // InMemoryResponse is a subclass of LiftResponse
\end_layout

\begin_layout Plain Layout

  // it takes an Array of Bytes, a List[(String,String)] of
\end_layout

\begin_layout Plain Layout

  // headers, a List[Cookie] of Cookies, and an integer
\end_layout

\begin_layout Plain Layout

  // return code (here 200 for HTTP 200: OK)
\end_layout

\begin_layout Plain Layout

  Full(InMemoryResponse(chartImage,
\end_layout

\begin_layout Plain Layout

                        ("Content-Type" -> "image/png") :: Nil,
\end_layout

\begin_layout Plain Layout

                        Nil,
\end_layout

\begin_layout Plain Layout

                        200))
\end_layout

\begin_layout Plain Layout

}
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Once we've set up the chart, we use the ChartUtilities helper class from
 JFreeChart to encode the chart into a PNG byte array.
 We can then use Lift's InMemoryResponse
\begin_inset Index
status collapsed

\begin_layout Plain Layout
InMemoryResponse
\end_layout

\end_inset

 to pass the encoded data back to the client with the appropriate Content-Type
\begin_inset Index
status collapsed

\begin_layout Plain Layout
Type
\end_layout

\end_inset

 header.
 Now we just need to hook the request into the dispatch table from the Boot
 class as shown in Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Hooking-dispatch-into-Boot"

\end_inset

.
 In this instance, we want state so that we can get the current user's chart.
 For this reason, we use
\family typewriter
LiftRules.dispatch
\family default
 as opposed to
\family typewriter
LiftRules.statelessDispatch
\family default
.
 Because we're using a partial function to perform a Scala match operation,
 the case that we define here uses the
\family typewriter
Req
\family default
 object's
\family typewriter
unapply
\family default
 method, which is why we only need to provide the
\family typewriter
List[String]
\family default
 argument.
 
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

\begin_inset Caption

\begin_layout Plain Layout
Hooking Dispatch into Boot
\begin_inset CommandInset label
LatexCommand label
name "lst:Hooking-dispatch-into-Boot"

\end_inset


\end_layout

\end_inset


\end_layout

\begin_layout Plain Layout

LiftRules.dispatch.append {
\end_layout

\begin_layout Plain Layout

  case Req("chart" :: "balances" :: endDate :: Nil, _, _) =>
\end_layout

\begin_layout Plain Layout

    Charting.chart(endDate) _
\end_layout

\begin_layout Plain Layout

}
\end_layout

\end_inset


\end_layout

\begin_layout Standard
As you can see, we capture the endDate parameter from the path and pass
 it into our chart method.
 This means that we can use a URL like
\family typewriter
http://foo.com/chart/balances/20080401
\family default
 to obtain the image.
 Since the dispatch function has an associated Lift session, we can also
 use the S.param method to get query string parameters, if, for example,
 we wanted to allow someone to send an optional width and height:
\end_layout

\begin_layout LyX-Code
val width = S.param(
\begin_inset Quotes eld
\end_inset

width
\begin_inset Quotes erd
\end_inset

).map(_.toInt) openOr 400
\end_layout

\begin_layout LyX-Code
val height = S.param(
\begin_inset Quotes eld
\end_inset

height
\begin_inset Quotes erd
\end_inset

).map(_.toInt) openOr 300
\end_layout

\begin_layout Standard
Or you can use a slightly different approach by using the
\family typewriter
Box.dmap
\family default
 method:
\end_layout

\begin_layout LyX-Code
val width = S.param(
\begin_inset Quotes eld
\end_inset

width
\begin_inset Quotes erd
\end_inset

).dmap(400)(_.toInt)
\end_layout

\begin_layout LyX-Code
val height = S.param(
\begin_inset Quotes eld
\end_inset

height
\begin_inset Quotes erd
\end_inset

).dmap(300)(_.toInt)
\end_layout

\begin_layout Standard
Where dmap is identical with map function except that the first argument
 is the default value to use if the
\family typewriter
Box
\family default
 is
\family typewriter
Empty
\family default
.
 There are a number of other ListResponse subclasses to cover your needs,
 including responses for XHTML, XML, Atom, Javascript, CSS, and JSON.
 We cover these in more detail in Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:LiftResponse-in-Detail"

\end_inset

.
\end_layout

\begin_layout Standard
\begin_inset Note Note
status open

\begin_layout Plain Layout
Make note of multipart mime handling, Req.uploadedFiles
\end_layout

\end_inset


\end_layout

\begin_layout Section
HTTP Redirects
\begin_inset CommandInset label
LatexCommand label
name "sec:HTTP-redirects"

\end_inset


\end_layout

\begin_layout Standard
HTTP redirects are an important part of many web applications.
 In Lift there are two main ways of sending a redirect to the client:
\end_layout

\begin_layout Enumerate
Call S.redirectTo.
 When you do this, Lift throws an exception and catches it later on.
 This means that any code following the redirect is skipped.
 If you're using a StatefulSnippet (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sub:Stateless-versus-Stateful"

\end_inset

), use
\family typewriter
this.redirectTo
\family default
 so that your snippet instance is used when the redirect is processed.
\end_layout

\begin_deeper
\begin_layout Standard
\begin_inset Box Shadowbox
position "t"
hor_pos "c"
has_inner_box 1
inner_pos "t"
use_parbox 0
width "80col%"
special "none"
height "1in"
height_special "totalheight"
status open

\begin_layout Plain Layout

\series bold
Important:
\series default
 if you use S.redirectTo within a try/catch block, you'll need to make sure
 that you aren't catching the redirect exception (Scala uses unchecked exception
s), or test for the redirect's exception and rethrow it.
 Ifyou mistakenly catch the redirect exception, then no redirect will occur.
 
\end_layout

\end_inset


\end_layout

\end_deeper
\begin_layout Enumerate
When you need to return a LiftResponse, you can simply return a RedirectResponse
 or a RedirectWithState response.
\end_layout

\begin_layout Standard
The RedirectWithState response allows you to specify a function to be executed
 when the redirected request is processed.
 You can also send Lift messages (notices, warnings, and errors) that will
 be rendered in the redirected page, as well as cookies to be set on redirect.
 Similarly, there is an overloaded version of S.redirectTo that allows you
 to specify a function to be executed when the redirect is processed.
\end_layout

\begin_layout Section
Cookies
\begin_inset CommandInset label
LatexCommand label
name "sec:Cookies"

\end_inset


\end_layout

\begin_layout Standard
Cookies
\begin_inset Foot
status open

\begin_layout Plain Layout
\begin_inset CommandInset href
LatexCommand href
target "http://java.sun.com/products/servlet/2.2/javadoc/javax/servlet/http/Cookie.html"

\end_inset


\end_layout

\end_inset

 are a useful tool when you want data persisted across user sessions.
 Cookies are essentially a token of string data that is stored on the user's
 machine.
 While they can be quite useful, there are a few things that you should
 be aware of:
\end_layout

\begin_layout Enumerate
The user's browser may have cookies disabled, in which case you need to
 be prepared to work without cookies or tell the user that they need to
 enable them for your site
\end_layout

\begin_layout Enumerate
Cookies are relatively insecure
\begin_inset Foot
status open

\begin_layout Plain Layout
See
\begin_inset CommandInset href
LatexCommand href
target "http://www.w3.org/Security/Faq/wwwsf2.html"

\end_inset

 (Q10) and
\begin_inset CommandInset href
LatexCommand href
target "http://www.cookiecentral.com/faq/"

\end_inset

 for details on cookies and their security issues.
\end_layout

\end_inset

.
 There have been a number of browser bugs related to data in cookies being
 read by viruses or other sites
\end_layout

\begin_layout Enumerate
Cookies are easy to fake, so you need to ensure that you validate any sensitive
 cookie data
\end_layout

\begin_layout Standard
Using Cookies in Lift is very easy.
 In a stateful context, everything you need is provided by a few methods
 on the S object:
\end_layout

\begin_layout Description
addCookie Adds a cookie to be sent in the response
\end_layout

\begin_layout Description
deleteCookie Deletes a cookie (technically, this adds a cookie with a maximum
 age of zero so that the browser removes it).
 You can either delete a cookie by name, or with a Cookie object
\end_layout

\begin_layout Description
findCookie Looks for a cookie with a given name and returns a Box[Cookie].
 Empty means that the cookie doesn't exist
\end_layout

\begin_layout Description
receivedCookies Returns a List[Cookie] of all of the cookies sent in the
 request
\end_layout

\begin_layout Description
responseCookies Returns a List[Cookie] of the cookies that will be sent
 in the response
\end_layout

\begin_layout Standard
If you need to work with cookies in a stateless context, many of the ListRespons
e classes (Section
\begin_inset CommandInset ref
LatexCommand ref
reference "sec:LiftResponse-in-Detail"

\end_inset

) include a List[Cookie] in their constructor or
\family typewriter
apply
\family default
 arguments.
 Simply provide a list of the cookies you want to set, and they'll be sent
 in the response.
 If you want to delete a cookie in a LiftResponse, you have to do it manually
 by adding a cookie with the same name and a
\family typewriter
maxage
\family default
 of zero.
 
\end_layout

\begin_layout Section
Session and Request State
\begin_inset CommandInset label
LatexCommand label
name "sec:Session-and-Request"

\end_inset


\end_layout

\begin_layout Standard
Lift provides a very easy way to store per-session and per-request data
 through the SessionVar and RequestVar classes.
 In true Lift fashion, these classes provide:
\end_layout

\begin_layout Itemize
Type-safe access to the data they hold
\end_layout

\begin_layout Itemize
A mechanism for providing a default value if the session or request doesn't
 exist yet
\end_layout

\begin_layout Itemize
A mechanism for cleaning up the data when the variable's lifecycle ends
\end_layout

\begin_layout Standard
Additionally, Lift provides easy access to HTTP request parameters
\begin_inset Index
status open

\begin_layout Plain Layout
request parameters
\end_layout

\end_inset

 via the S.param method, which returns a Box[String].
 Note that HTTP request parameters (sent via GET or POST) differ from RequestVar
s in that query parameters are string values sent as part of the request;
 RequestVars, in contrast, use an internal per-request Map so that they
 can hold any type, and are initialized entirely in code.
 At this point you might ask what RequestVars can be used for.
 A typical example would be sharing state between different snippets, since
 there is no connection between snippets other than at the template level.
\end_layout

\begin_layout Standard
SessionVars and RequestVars are intended to be implemented as singleton
 objects so that they're accessible from anywhere in your code.
 Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Defining-a-RequestVar"

\end_inset

 shows an example definition of a RequestVar used to hold the number of
 entries to show per page.
 We start by defining the object as extending the RequestVar.
 You must provide the type of the RequestVar so that Lift knows what to
 accept and return.
 In this instance, the type is an Int.
 The constructor argument is a by-name parameter which must evaluate to
 the var's type.
 In our case, we attempt to use the HTTP request variable
\begin_inset Quotes eld
\end_inset

pageSize,
\begin_inset Quotes erd
\end_inset

 and if that isn't present or isn't an integer, then we default to 25.
\end_layout

\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

\begin_inset Caption

\begin_layout Plain Layout
Defining a RequestVar
\begin_inset CommandInset label
LatexCommand label
name "lst:Defining-a-RequestVar"

\end_inset


\end_layout

\end_inset


\end_layout

\begin_layout Plain Layout

class AccountOps {
\end_layout

\begin_layout Plain Layout

  object pageSize extends RequestVar[Int](S.param("pageSize").map(_.toInt)
 openOr 25)
\end_layout

\begin_layout Plain Layout

  ...
\end_layout

\begin_layout Plain Layout

}
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Accessing the value of the
\family typewriter
RequestVar
\family default
 is done via the
\family typewriter
is
\family default
 method.
 You can also set the value using the
\family typewriter
apply
\family default
 method, which in Scala is syntactically like using the
\family typewriter
RequestVar
\family default
 as a function.
 Common uses of apply in Scala include array element access by index and
 companion object methods that can approximate custom constructors.
 For example, the
\family typewriter
Loc
\family default
 object (which we'll cover in Chapter
\begin_inset CommandInset ref
LatexCommand ref
reference "cha:SiteMap"

\end_inset

), has an overloaded
\family typewriter
apply
\family default
 method that creates a new
\family typewriter
Loc
\family default
 class instance based on input parameters.
\end_layout

\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

\begin_inset Caption

\begin_layout Plain Layout
Accessing the RequestVar
\begin_inset CommandInset label
LatexCommand label
name "lst:Accessing-the-RequestVar"

\end_inset


\end_layout

\end_inset


\end_layout

\begin_layout Plain Layout

// get the value contained in the AccountOps.pageSize RequestVar
\end_layout

\begin_layout Plain Layout

query.setMaxResults(AccountOps.pageSize.is)
\end_layout

\begin_layout Plain Layout

\end_layout

\begin_layout Plain Layout

// Change the value of the RequestVar.
 The following two lines
\end_layout

\begin_layout Plain Layout

// of code are equivalent:
\end_layout

\begin_layout Plain Layout

AccountOps.pageSize(50)
\end_layout

\begin_layout Plain Layout

AccountOps.pageSize.apply(50)
\end_layout

\end_inset


\end_layout

\begin_layout Standard
In addition to taking a parameter that defines a default value for setup,
 you can also clean up the value when the variable ends it lifecycle.
 Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Defining-a-Cleanup"

\end_inset

 shows an example of opening a socket and closing it at the end of the request.
 This is all handled by passing a function to the
\family typewriter
registerCleanupFunc
\family default
 method.
 The type of the function that you need to pass is
\begin_inset Formula $CleanUpParam\Rightarrow Unit$
\end_inset

, where
\family typewriter
CleanUpParam
\family default
 is defined based on whether you're using a
\family typewriter
RequestVar
\family default
 or a
\family typewriter
SessionVar
\family default
.
 With
\family typewriter
RequestVar
\family default
,
\family typewriter
CleanUpParam
\family default
 is of type
\family typewriter
Box[LiftSession]
\family default
, reflecting that the session may not be in scope when the cleanup function
 executes.
 For a
\family typewriter
SessionVar
\family default
 the
\family typewriter
CleanUpParam
\family default
 is of type
\family typewriter
LiftSession
\family default
, since the session is always in scope for a
\family typewriter
SessionVar
\family default
 (it holds a reference to the session).
 In our example in Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Defining-a-Cleanup"

\end_inset

 we simply ignore the input parameter to the cleanup function, since closing
 the socket is independent of any session state.
 Another important thing to remember is that you're responsible for handling
 any exceptions that might be thrown during either default initialization
 or cleanup.
 
\end_layout

\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

\begin_inset Caption

\begin_layout Plain Layout
Defining a Cleanup Function
\begin_inset CommandInset label
LatexCommand label
name "lst:Defining-a-Cleanup"

\end_inset


\end_layout

\end_inset


\end_layout

\begin_layout Plain Layout

object mySocket extends RequestVar[Socket](new Socket("localhost:23")) {
\end_layout

\begin_layout Plain Layout

  registerCleanupFunc(ignore => this.is.close)
\end_layout

\begin_layout Plain Layout

}
\end_layout

\end_inset


\end_layout

\begin_layout Standard
The information we've covered here is equally applicable to SessionVars;
 the only difference between them is the scope of their respective lifecycles.
\end_layout

\begin_layout Standard
Another common use of
\family typewriter
RequestVar
\family default
 is to pass state around between different page views (requests).
 We start by defining a
\family typewriter
RequestVar
\family default
 on an object so that it's accesible from all of the snippet methods that
 will read and write to it.
 It's also possible to define it on a class if all of the snippets that
 will access it are in that class.
 Then, in the parts of your code that will transition to a new page you
 use the overloaded versions of
\family typewriter
SHtml.link
\family default
 or
\family typewriter
 S.redirectTo
\family default
 that take a function as a second argument to
\begin_inset Quotes eld
\end_inset

inject
\begin_inset Quotes erd
\end_inset

 the value you want to pass via the
\family typewriter
RequestVar
\family default
.
 This is similar to using a query parameter on the URL to pass data, but
 there are two important advantages:
\end_layout

\begin_layout Enumerate
You can pass any type of data via a
\family typewriter
RequestVar
\family default
, as opposed to just string data in a query parameter.
\end_layout

\begin_layout Enumerate
You're really only passing a reference to the injector function, as opposed
 to the data itself.
 This can be important if you don't want the user to be able to tamper with
 the passed data.
 One example would be passing the cost of an item from a
\begin_inset Quotes eld
\end_inset

view item
\begin_inset Quotes erd
\end_inset

 page to an
\begin_inset Quotes eld
\end_inset

add to cart
\begin_inset Quotes erd
\end_inset

 page.
\end_layout

\begin_layout Standard
Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Passing-an-Account"

\end_inset

 shows how we pass an Account from a listing table to a specific Account
 edit page using
\family typewriter
SHtml.link
\family default
, as well as how we could transition from an edit page to a view page using
 
\family typewriter
S.redirectTo
\family default
.
 Another example of passing is shown in Listing
\begin_inset CommandInset ref
LatexCommand vref
reference "lst:Passing-Detached-Instances"

\end_inset

.
\end_layout

\begin_layout Standard
\begin_inset listings
inline false
status open

\begin_layout Plain Layout

\begin_inset Caption

\begin_layout Plain Layout
Passing an Account to View
\begin_inset CommandInset label
LatexCommand label
name "lst:Passing-an-Account"

\end_inset


\end_layout

\end_inset


\end_layout

\begin_layout Plain Layout

class AccountOps {
\end_layout

\begin_layout Plain Layout

  ...
\end_layout

\begin_layout Plain Layout

  object currentAccountVar extends RequestVar[Account](null)
\end_layout

\begin_layout Plain Layout

  ...
\end_layout

\begin_layout Plain Layout

  def manage (xhtml : NodeSeq) ...
 {
\end_layout

\begin_layout Plain Layout

    ...
\end_layout

\begin_layout Plain Layout

    User.currentUser.map({user =>
\end_layout

\begin_layout Plain Layout

      user.accounts.flatMap({acct =>
\end_layout

\begin_layout Plain Layout

        bind("acct", chooseTemplate("account", "entry", xhtml),
\end_layout

\begin_layout Plain Layout

          ...
\end_layout

\begin_layout Plain Layout

          // The second argument injects the "acct" val back
\end_layout

\begin_layout Plain Layout

          // into the RequestVar
\end_layout

\begin_layout Plain Layout

          link("/editAcct", () => currentAccountVar(acct), Text("Edit"))
\end_layout

\begin_layout Plain Layout

      })
\end_layout

\begin_layout Plain Layout

    })
\end_layout

\begin_layout Plain Layout

    ...
\end_layout

\begin_layout Plain Layout

  }
\end_layout

\begin_layout Plain Layout

  def edit (xhtml : NodeSeq) : NodeSeq = {
\end_layout

\begin_layout Plain Layout

    def doSave () {
\end_layout

\begin_layout Plain Layout

      ...
\end_layout

\begin_layout Plain Layout

      val acct = currentAccountVar.is
\end_layout

\begin_layout Plain Layout

      S.redirectTo("/view", () => currentAccountVar(acct))
\end_layout

\begin_layout Plain Layout

    }
\end_layout

\begin_layout Plain Layout

    ...
\end_layout

\begin_layout Plain Layout

  }
\end_layout

\begin_layout Plain Layout

}
\end_layout

\end_inset


\end_layout

\begin_layout Standard
One important thing to note is that the injector variable is called in the
 scope of the
\emph on
following
\emph default
 request.
 This means that if you want the value returned by the function at the point
 where you call the link or redirectTo, you'll need to capture it in a val.
 Otherwise, the function will be called
\emph on
after
\emph default
 the redirect or link, which may result in a different value than you expect.
 As you can see in Listing
\begin_inset CommandInset ref
LatexCommand ref
reference "lst:Passing-an-Account"

\end_inset

, we set up an acct val in our doSave method prior to redirecting.
 If we tried to do something like
\end_layout

\begin_layout LyX-Code
S.redirectTo("/view", () => currentAccountVar(currentAccountVar.is))
\end_layout

\begin_layout Standard
instead, we would get the default value of our RequestVar (null in this
 case).
\end_layout

\begin_layout Section
Conclusion
\end_layout

\begin_layout Standard
We've covered a lot of material and we still have a lot more to go.
 Hopefully this chapter provides a firm basis to start from when exploring
 the rest of the book.
\end_layout

\end_body
\end_document
Something went wrong with that request. Please try again.