Anda di halaman 1dari 211

1X*( D-$(  .

 

+*-#$"*-$
DA"76(:DF?BD?DBCE
P SQRV '"*-&. *!/2- .D - $)( )/*.  -1$:*. /F **. *. $- $/*.
- . -1*.F
 )#0( +-/  ./ '$1-* +* . - - +-*05$ *0 /-).($/$ ( ,0',0 - !*-(D
. % +*- ( $* ' /-?)$* *0 ( 8)$*D . ( + -($..9* +*- .-$/*  '"*-&.D
3 /*+-- .0(*.- 1 . (- 1$.@ . )7'$. .F
'"*-&.*!/2- .D- $)( )/*.  -1$:*. /
222F'"2*-&.F*(
*)//*O'"2*-&.F*(
[VVLRRMSWSWGZURV
$"G)*.).-  ..*$$. B,0 +*- )/-* /0*H
www.algaworks.com 3
*- *0/*-
#$"*-$ )-
#//+.EJJ222F'$)& $)F*(J$)J/#$"*!
0)*-D $)./-0/*- *).0'/*-  '"*-&.F
-0* ( $./ (. 
)!*-(:9*  -/$B*
*(* +-*"-(*- 1 + ' 0)F
)$$*0 . 0 $)/ - ..
+*- +-*"-(:9* ( RZZVD ,0)*  . )1*'1 0 0(
.*!/2- +- )/- / )$( )/* . /*-)*0 0( *. ($.
+*+0'- . )* -.$' *0/-*. +=. .  '=)"0 +*-/0"0 .F 7 !*$ .>$* /-'#*0 (
*0/-. (+- ..  .*!/2- *(* +-*"-(*-D " - )/ $- /*-  / )*'*"$D (.
)0) $3*0 +-*"-(-F
www.algaworks.com 5
)/ . *( :-FFF
)/ . ,0 1*< *(   ' - .. '$1-*D 0 "*./-$  *($)- '"0(. *$.. *(
1*<D+-,0 / )#0( 3 ' )/ +-*1 $/( )/***)/ A*F(*.'7I
,0 1*<+- $.. -I
*< .> *). "0$-7 .*-1 - * *)/ A*  .. '$1-* . %7 *)#  - 1 -$ )/:9*
 % /*.F .* $) )9* *)# :D - *( )* ,0 ./0 .*- .. ..0)/* +*-
'"0(*(0-.* *0'$1-*F
*(**/ -%0I
0-)/ *. . 0. ./0*.D 0 / )#*  -/ 5  0( *$.E 1*< 1$ / - (0$/. A1$.
+-*' (.+-- .*'1 -H
0 "*./-$ (0$/*  / %0- + ..*'( )/ ) .. . +-*' (. .)- /*. . .0.
A1$.D (. $)! '$5( )/ )9* *).$"* !5 - $..* *( /**. *. ' $/*- . * '$1-*D B)'D
0 %7 *0+* "-) +-/ * ( 0 $ %0)* *. ( 0. '0)*.  0-.*. *)'$) )
'"*-&.FFF
. 0 /$1 0( $ $H 0 /' *. ' $/*- . . %0- ( /-1;.  0( .$/  + -"0)/.
- .+*./.I
(*.0.-D*B$'( )/ D* /&1 -C*2 (+*-/0"0<.H.> ..-E
#//+EJJ+/F./&*1 -C*2F*(J
 (+- ,0 .0-"$- '"0( A1$D +*./ '7 )* .$/ * /&1 -C*2 ""('  -
@% 0(+$,@F ..!*-(B-7!7$' )*)/--+ -"0)/.*.*0/-*.' $/*- .F
0)* !*- +*..=1 'D 0 *0/-. + ..*. * /$(  '"*-&. 1$ +..- + '*
/&1 -C*2 +- %0- '"0(. + ..*.D  -/*I > ' (-)* ,0 )9*
*). "0$- (*. %0-  /**.D +*- $..* *)/* /(;( *(  .0 %0 +- - .+*) -
*0/-*.' $/*- .,0 )  ..$/ (F
*(*.0" -$-( '#*-$.*0- +*-/- --*.)*'$1-*I
 1*< )*)/-- '"0( --* )* *)/ A*  .. '$1-* *0 . /$1 - '"0( .0" ./9* +-
( '#*--+->3$( $:9*D1*0B-(0$/*! '$5. 1*<+0 -( $5 -F
)1$ 0( G($'+- '$1-*.O'"2*-&.F*(F
www.algaworks.com 6
)  )*)/--*>$"*G!*)/ *+-*% /*I
 ./ '$1-*D )>.  . )1*'1 (*. 0( +-*% /* +..*  +..*F  '$)& +- $3- * >$"*G
!*)/ !*$ )1$*+-. 0 G($',0)*1*<. $).- 1 0+--   -*'$1-*F
.* 1*< / )# + -$* .. '$)&D  .. #//+EJJ'"F2*-&.J'$1-*G%1 J +- -  <G
'*)*1( )/ F
%0 )*)/$)0$  .. /-'#*
.- 1 - 0( '$1-* *(* .. 7 (0$/* /-'#*D +*- $..*D .. +-*% /* .> !5 . )/$* .
(0$/.+ ..*./$1 - ( ..* ' F
%0  $10'"- .. '$1-* +- . 0. ($"*. ,0 /(;( . $)/ - ..( +*-
+-*"-(:9* 1F*(+-/$'# )* **& 2$// -H
www.algaworks.com 7
0(7-$*
R
)/-*0:9** . )1*'1$( )/*2  RU
RFR ,0 ; 1I RU
RFS +-*/**'*  RV
RFT  . )1*'1$( )/*2 *( 1 RX
RFU *)/$) -. RY
RFV
)./')**+# *(/ RZ
RFW
)/ "-)**'$+. *(*+# *(/ SR
RFX +# 1 ) SW
RFY -$( $-*+-*% /*2 *(+# 1 ) SW
S  -.$./<)$ *.*(  TW
SFR ,0 ;+ -.$./<)$I TW
SFS + ( )/*% /* '$*)'LM TW
SFT *-,0 0.-I TY
SFU 1 -.$./ ) 
  $ -)/ TY
SFV *)B"0-:9*    $ -)/ *(1 ) TZ
SFW -$:9***($)* ' UQ
SFX
(+' ( )/:9** ,0'.LM #.#* LM US
SFY + ( )/*7.$* UT
SFZ -,0$1*+ -.$./ ) F3(' UX
SFRQ  -)*./ '.*)* *. UZ
SFRR ->3$(*.+..*. VQ
T
)/-*0:9**  VR
TFR ,0 ; 1 -1 - .I VR
TFS -$)$+$.*(+*) )/ . VS
TFT $'$*/ . *(+*) )/ . / - $-*. VS
TFU .*'# )*0($(+' ( )/:9*   VT
TFV $$*))* *+-*% /*1 ) VU
TFW )"  ) VU
TFX -$)*0(+7"$)  VW
TFY $")*1'*- . :@ .*( VZ
TFZ .*+*. ()"  ). WR
TFRQ &$)" ) WV
TFRR $'* 1$ WX
TFRS -,0$1*! .G*)B"F3(' XQ
TFRT -,0$1*2 F3(' XQ
U 1 ":9* XT
UFR
)/-*0:9*6)1 ":9* XT
UFS 1 ":9*$(+'=$/ XT
UFT 1 ":9* 3+'=$/ XU
V *(+*) )/ . $)/ -! XW
VFR $'$*/ . XW
VFS  :'#* *-+*+7"$) XX
VFT *-(0'7-$*. XX
VFU -*+-$  .*(0). XY
VFV )/- / 3/*. YS
VFW = / 3/*. YT
VFX
(" ). YV
VFY  )0. $3. '$./" ( YW
VFZ (+*. # " ( */@ .-7$* ZQ
VFRQ
/ ). . ' :9* ZT
VFRR */@ . '$)&. ZU
VFRS $);$. ZV
VFRT  )." ). ZY
VFRU  '. *. ZZ
VFRV -,0$1*. 1-$+/  RQU
W 7"$) *).0'/ '):( )/*. RQW
WFR -$)*)/$/4)" - RQW
WFS  -.$./$)*+ ..*. '):( )/*. RQX
WFT )"  ),0 *).0'/'):( )/*. RQZ
WFU 7"$) - .0'/**).0'/ RRQ
WFV +-9* +*.$/*-4 RRS
X  (+'/ .*( ' /. RRU
XFR 0';*+-*' ( - + /$-I RRU
XFS
)'0$)*0( :'#* -*+; RRU
XFT -$)*0(/ (+'/ RRX
XFU .)**/ (+'/ RRY
Y *)1 -.9* 1'$:9* RSQ
YFR
)/-*0:9* RSQ
YFS *)1 -.*- .+-9* RSS
YFT '/ -)/$1.+- B)$-*)1 -.*- . RSX
YFU 0./*($5)*( )." ).  --* *)1 -.9* RSY
YFV '$*- .+-9* RTR
YFW 0./*($5)*( )." ).  --*. 1'$:9* RTU
YFX -$)**)1 -.*- .+ -.*)'$5*. RTV
YFY -$)*1'$*- .+ -.*)'$5*. RTX
Z 7"$) ./-* '):( )/* RTZ
ZFR
(+' ( )/)**- +*.$/>-$* RTZ
ZFS
(+' ( )/)*.- "-. ) ">$* RUQ
ZFT -*"-()**()"  ) ./-* RUR
ZFU -*"-()***)1 -.*-  ..* RUS
ZFV -$)**!*-(0'7-$* ./-* RUT
RQ  )'$/$*) RUW
RQFR ,0 ; )'$/$*)I RUW
RQFS $$*))**-/ !/*)*+*(F3(' RUX
RQFT $$*))*- ./-$:@ .)*(* '* RUX
RQFU 0./*($5)*( )." ). 1'$:9* RVQ
RQFV *(+*)*0()*1- ./-$:9* RVT
RR )$+0')* 1 )/*. RVV
RRFR
)/-*0:9* RVV
RRFS 1 )/*. :9* RVV
RRFT 1 )/*. (0): 1'*- +-*+-$  $(( $/ RVW
RS 
G*)/ 3/.) + ) )4
)% /$*) RVZ
RSFR
)% :9*  + )<)$. RVZ
RSFS *)B"0-)*
)*+-*% /* RWR
RSFT  ).
D ( . O
)% / RWS
RSFU .*+*.  ).
RWV
RSFV -*0/*- )/$/4)" - RWW
RSFW *)/-*')*./-).:@ .*($)/ - +/*- . RWX
RSFX
)% :9* (*)1 -.*- .  RWZ
RT %3 RXR
RTFR
)/-*0:9* RXR
RTFS  ) -$5:9*+-$' RXR
RTFT +-*+-$   1 )/ RXT
RTFU +-*+-$  '$./ ) - RXT
RTFV  ) -$5:@ .(A'/$+'. RXU
RTFW -* ..( )/*+-$' RXW
RTFX '1-.G#1 +-- ) -  3 0/ RXX
RTFY 7"$) ./-* '):( )/**(%3 RXY
RU -$(  . RYQ
RUFR
)/-*0:9* RYQ
RUFS *)B"0-)**+-*% /* RYR
RUFT 0/+0/  ' 
)+0/ 3/ RYR
RUFU  ' /)  )0 RYS
RUFV  ' /) 0//*) RYT
RUFW ' )- RYV
RUFX 0/**(+' / RYW
RUFY  .." . RYX
RUFZ *(()0//*) RYY
RUFRQ ) '-$ RYY
RUFRR /' RYZ
RUFRS  )0- RZS
RUFRT %3//0. RZU
RUFRU -*"-()*'/ -:9* '):( )/*. RZU
RUFRV -*"-()* 3'0.9* '):( )/*. RZX
RV  "0-):+'$:9* SQQ
RVFR .*'# )*0(.*'0:9* SQQ
RVFS *"$) SQQ
RVFT *"*0/ SQU
RVFU $'/-* 0/*-$5:9* SQV
RW *)'0.9* SQW
RWFR ->3$(*.+..*. SQW
+=/0'*R

)/-*0:9** . )1*'1$( )/*


2 
RFRF ,0 ; 1I
 1  L 1 '/!*-(D )/ -+-$. $/$*)M ; 0( +'/!*-( +-9* +-
 . )1*'1 - +'$:@ . 1  "-) +*-/ J*0 +-  $)/ -) /D ,0 $)'0$ $'$*/ .
!0)$*)'$ . +- $(+' ( )/- .*!/2- 1 $./-$0=*D . * (
*(+*) )/ . (*0'- . ,0 3 0/( ( . -1$*- .  +'$:@ . ,0 .0+*-/(
.'$'$ D . "0-):D $)/ "-$ *0/-*. - ,0$.$/*.  +'$:@ . *-+*-/$1.
*0 "-) +*-/ F
 +'/!*-( 1  +*..0$ 0( .;-$  .+ $B:@ . L/ )*'*"$.M *( *% /$1*.
$./$)/*.D +*- $..* ; *).$ - 0( +'/!*-( "0-G#01F )/- .
.+ $B:@ . 1D.($.*)# $..9*E
K  -1' /.E .9* *(+*) )/ . 1 3 0/*. )* . -1$*- +- " -- *)/ A*
$)8($*+-2 D*(*    F
K  L 1 -1 - " .ME 0( .+ $'$5:9*   -1' /. ,0 + -($/ ,0
+'$:@ . 2   . )1*'1$. ( 1 . %( ($. !7 $.  ()/ -F  .$($'-
6. / )*'*"$. *(*   D +*-;( ($. -*0./ +*- / - /*. .
!$'$ .+'/!*-( 1F
K  L 1 -1 -  .ME ; 0( !-( 2*-& 2  . * ( 1 ,0 / ( *(*
*% /$1* .$(+'$B- *  . )1*'1$( )/*  $)/ -! . L/ '.M  .$./ (. +-
 2 D /-1;.  0( (* '*  *(+*) )/ . - 0/$'$571 $.F  +-*+*./ ; ,0
*. .$./ (. . %(  . )1*'1$*. *(  ( .( !$'$ +-*0/$1$
www.algaworks.com 14
,0 .  . )1*'1 .$./ (.  .&/*+ L/; ( .(* *( ! --( )/. ,0
.0+*-/('$-G G--./-*(+*) )/ .MF
K  L 1  -.$./ ) 
ME ; 0( 
+-9* * 1 +- + -.$./<)$
 *.D ,0 0. 0( *) $/*  (+ ( )/* *% /*G- '$*)'F ..
/ )*'*"$ /-5 '/ +-*0/$1$ +- *  . )1*'1$( )/*  .$./ (. ,0
)  ..$/(  $)/ "-:9* *( )*  *.F > +- $/-D .. 

+*..$$'$/ ,0 1*<  . )1*'1 +'$:@ . 0.)* )*  *. . (


+- $.- .- 1 -0('$)#. ,0 -  F
K   L)/ -+-$. 1  ).ME .9* *(+*) )/ . ,0 3 0/( ( . -1$*- .
 +'$:9* +*..0 ( *(* +-$)$+$. *% /$1*.D !*-)  - !$'$
+-*0/$1$ )*  . )1*'1$( )/*  *(+*) )/ . $./-$0=*.D
/-).$*)*.D. "0-*. +*-/71 $.F
 ./ '$1-*D*-- (*..*-   0(- 1 $)/-*0:9*  F
RFSF +-*/**'* 
 +-*/**'*  ; 0/$'$5* ) )1 ":9*  +7"$). 
)/ -) /F 0)* 1*< -
0( %) '  0( +(0,+D  .. 0( +7"$)   )1 " ( . 0. '$)&.D 1*< ./7D
) 1 - D 0/$'$5)* .. +-*/**'* +- 1$.0'$5-D ( .0 (7,0$)D * *)/ A* ,0
./7-(5 )* J*0;+-* ..* (. -1$*- .- (*/*.F
  ; 0( +-*/**'* ,--%,,  *(0)$:9* '$ )/ G. -1$*-E * '$ )/ )1$
0( - ,0$.$:9* +- * . -1$*-D ,0 +-* ..  - ,0$.$:9*  1*'1 0( - .+*./ +-
* '$ )/ D . )* ,0 D  +-$)=+$*D ) )#0( $)!*-(:9* ; ()/$ )* . -1$*- (
- ':9*6.- ,0$.$:@ .+- 1$( )/ -  $.F
..$(D ,0)* $"$/(*. * ) - :*  0( +7"$) ( 0( +(0,+D ./(*. " -)*
0( - ,0$.$:9*  0( . -1$*-D ,0 $-7D +*- .0 1 5D  1*'1 - +- * +(0,+ * *)/ A*
+7"$)  - ,0$.$/F
www.algaworks.com 15
 - ,0$.$:9* )1$ +*- 0( '$ )/  1 *)/ -D .$( )/ D 0( *()* L/(;(
#(*  (;/**MD * ) - :*  0( - 0-.* )* . -1$*- L/(;( #(* 
)-!M 0($)!*-(:9*.*- 1 -.9**+-*/**'* . )*0/$'$5*F
0+*)*D +*- 3 (+'*D ,0 ,0 - (*. 0.- * *)/ A* * ) - :*
!--)5990006.(%6(&6+9"'16!-&%F /$'$5 (*. * (;/** D * )-! /index.html 
1 -.9* 1.1 *+-*/**'* F (*.. "0$)/ - ,0$.$:9* )1$E
GET /index.html HTTP/1.1
Host: www.uol.com.br
3$./ ( $1 -.*. (;/**.  ,0 +* ( . - .+ $B*. ( - ,0$.$:@ .D . )*
*. ($. *(0). * (;/** D )*-('( )/ 0/$'$5* +- */ - * *)/ A*  0(
-,0$1* )* . -1$*-D * (;/** D 0/$'$5* +- )1$- *.  !*-(0'7-$*.
 *. -1$*-F
( - ,0$.$:9* +* *)/ - +-8( /-*. $$*)$.D #(*. !+,F '"0).
!+, *(0)..9*D+*- 3 (+'*D (,-D ,+7 '- )-F
( 1 5 +-* ..  - ,0$.$:9*D * . -1$*-D +*- .0 1 5D () 0( - .+*./ +-
* '$ )/ D . )* ,0 .. - .+*./ /(;( / ( 0( !*-(/* +-;G / -($)*E 
+-$( $- '$)# *)/;( $)!*-(:@ . .*-  1 -.9* * +-*/**'*D 0( >$"*  .//0.
 - .+*./ 0( ( )." ( ..*$  .. .//0.F ( . "0$D .9* )1$*. *.
!+,  - .+*./D B)'( )/ D ; )1$* * *)/ A*  - .+*./F  % 0( 3 (+'*
.$(+' . - .+*./ E
HTTP/1.1 200 OK
Date: Thu, 26 Sep 2013 15:17:12 GMT
Server: Apache/2.2.15 (CentOS)
www.algaworks.com 16
Content-Type: text/html; charset=utf-8
<html>
<body>
</body>
</html>
* 3 (+'* )/ -$*-D * >$"*  .//0. 200 $)$ ,0 #*01 .0 ..* )* / )$( )/*
 - ,0$.$:9* )1$ + '* '$ )/ D *. !+, $)$(  / #*- * . -1$*-D *
. -1$*-0.*D/$+***)/ A* D+*-B(D/ (*.*>$"*G!*)/ +7"$)  F
0/-*. >$"*.  .//0. ./)/ *(0). .9* * 404D ,0 $)$ ,0 * - 0-.* )9* !*$
'*'$5* )* . -1$*- * >$"* 500D ,0 $)$ ,0 #*01 --* )* +-* ..( )/* 
- ,0$.$:9* )1$F
RFTF  . )1*'1$( )/*2 *( 1
*( * 1):*  / )*'*"$ .*- -  .  *(+0/*- . *( * - .$( )/*
 $)/ -) /D . +7"$). 2  ./9* . /*-))*  1 5 ($. /- )/ . # $. 
- 0-.*.,0 0( )/($)/ -/$1$ *(*0.07-$*F
0)* !'(*. ( +'$:@ . 2 D ./(*. )*. - ! -$)*  .$./ (. *0 .$/ . *)
"-) +-/  +-*"-(:9* B #*.+  ( . -1$*- . ) $)/ -) /D * 0.07-$*
L'$ )/ M )*-('( )/ )9* +- $. / - ) $)./'* ( .0 (7,0$) +- 0/$'$57G'.D
';( 0()1 "*-L-*2. -MF
  ..* 6. +7"$).  .. . .$./ (. ; ! $/ 0/$'$5)* * (* '* #(*  +*.,-7
+,)(',D *0 . %D * '$ )/ .*'$$/ ,0 '"0( :9* . % - '$5 L+*.,-M *
. -1$*-- '$5 - .+*) +-*'$ )/ L+,)(',MF
 +'/!*-( 1D .. (* '* !*$ $(+' ( )/* /-1;.  
  -1' /.F (
 -1' / ./ )  !0)$*)'$  0( . -1$*- 2  +- . -1$- +7"$). $)8($.
*.)1 "*- .D0/$'$5)**+-*/**'* F
* (0)* 1D *. . -1$*- . 2  .9* #(*.   -1' / *)/$) -D +*$.
$(+' ( )/(  .+ $B:9*   -1' /F  . -1$*- *)1 -/  - ,0$.$:9* ( 0(
*% /* * /$+* HttpServletRequestF ./ *% /* ; )/9* +..* *. *(+*) )/ .
2 D ,0 +* ( 3 0/- ,0',0 - >$"* 1 +- ,0 +*.. . - " -* 0(
*)/ A* $)8($*F ( . "0$D * *(+*) )/ 2   1*'1 0( *% /*
HttpServletResponseD ,0 - +- . )/  - .+*./ * '$ )/ F ./ *% /* ; 0/$'$5*
+-,0 **)/ A*" -*. % )1$**)1 "*-*0.07-$*F
www.algaworks.com 17
 . * '):( )/*   -1' /.D *0/-. / )*'*"$. 1 !-( 2*-&. !*-( .0-"$)*
*( * *% /$1*  ( '#*--  +-*0/$1$ - 0-.*. )*  . )1*'1$( )/* 
+'$:@ . 2 F /0'( )/ 1 -1 -  . ;  / )*'*"$ * (*( )/*D - ,0$.$/
) ($*-$ . *+*-/0)$ .  (+- "* +-  . )1*'1 *- . 1F D ..$( *(*
*.*0/-*.!-( 2*-&.2 D!*-(. *. ( -1' /.F
RFUF *)/$) -.
*)/$) -. .9* $)/ -! . )/- *(+*) )/ . !0)$*)'$ .  $3* )=1 '
.+ =B.  0( +'/!*-(F - 0( +'$:9* 2   . )1*'1$ ( 1 *0
0( *(+*) )/ *-+*-/$1* . - 3 0/*D ' . +- $.( . - $(+')/*. ( 0(
*)/$) -F
. *)/$) -. /(;( .9* #(*.  . -1$*- .  *% /*.D *0 . -1$*- . 
+'$:9*D+*$.*! -  (. -1$:*. $)!-G ./-0/0-+- 3 0:9* *(+*) )/ .F
   *)/$) - .0+*-/ )/ -+-$. 1 ). L MD ,0 .9* *(+*) )/ .
*-+*-/$1*. $./-$0=*.F .  -1' /.D D +7"$).  -,0$1*. ./7/$*. L  D
D$(" ).  /M)  ..$/( 0( *)/$) -+-. - 3 0/*F
3$./ ( $1 -.. *-")$5:@ . ,0  . )1*'1 ( *)/$) -. 1 D +*- 3 (+'*E
-' D
D   /D +# D /F + .-  /)/. *! -/. "-/0$/.D '"0(.
(+- .. $) 1 ) ( '$ ):.  . 0. +->+-$*. . -1$*- .D +*$. *! -  ( .0+*-/
$! - )$* * '$ )/ )*-('( )/ $(+' ( )/( !0)$*)'$ . ,0 *.
. -1$*- ."-/0$/*./'1 5)9*+*..0(F
- / ./- )*..*. 3 (+'*.D 0.- (*. * )! (&-D +*$. ; ' 1 D "-/0$/* (0$/*
+*+0'-F
*(* ./ . . -1$*- . .9* . *. ). .+ $B:@ .  / )*'*"$ 1 D
/ *-$( )/ D 1*< +* $(+')/- *. 3 (+'*. ,0  . )1*'1 - (*. ) ./ '$1-* (
,0',0 -*)/$) -*(+/=1 '*( 1F
*2)'**+# *(/+* . -! $/* ( #//+EJJ/*(/F+# F*-"F
www.algaworks.com 18
RFVF
)./')**+# *(/
 +-* ..*  $)./':9* * +# *(/ ; .$(+' .D ./  .*(+/- * -,0$1*
$3*)*'*' . %*F ./ '$1-*D0.- (*.* (&-F<BF
( 1 5 B)'$5*D / (G. 0( *)/$) - +-*)/* +- +-*0:9*F  ,0',0 - !*-(D
* .$/ $.+*)$$'$5 /*  *0( )/:9* )  ..7-$ +- - .*'1 - +-*' (.
)*)/-*. .'-  - A1$. *( - ':9* * +-* ..*  $)./':9* *)B"0-:9*
*. -1$*-F
- )/ ) - 0( +*0* .*- * !0)$*)( )/* * *(/D 3($) *. $- />-$*.
-$*.0-)/ *+-* ..* $)./':9*F.+-$)$+$..9*E
K "'E 3 0/71 $.D $)'0$)* *. +'$/$1*. +- $)$$- +- ) --- 
3 0:9**. -1$*-F
K ('E -,0$1*.  *)B"0-:9* * *(/F  -,0$1* ,+/+61&%D (
+-/$0'-D  B) 0( .;-$  +-8( /-*. +-  3 0:9* * . -1$*-D *(*
+*- 3 (+'*D  +*-/ *) * . -1$*- $-7 -   - - ,0$.$:@ . L .. +*-/
www.algaworks.com 19
;D +*-  !0'/D YQYQMD  1 )* . - 3($)* *( 0$* (*$B*
*)!*-( .)  ..$ .F
K %( ,E -,0$1*.  '*" * . -1$*-F  *(/ /(;( +* " -- -,0$1*. 
'*" *( /0* * ,0 . +'$:@ .  . )1*'1$. )1$( +-  .= +-9*
*.$./ (F
K 0(+$E $- />-$* / (+*-7-$* * *(/F .. $- />-$* ; 0/$'$5*D +*-
3 (+'*D+-- '$5-- *(+$':9*0/*(7/$ +7"$). F
K 0)),E  .. $- />-$* .9* $(+')/. . $1 -.. +'$:@ . 2 
 . )1*'1$.F
- 1 -$B- .  $)./':9* !*$  ( .0 $D )* $)*2.D  .. * $- />-$* "'
3 0/ * -,0$1* ,-+-.)6- +- $)$$- * . -1$*-F *< %7  1 / -   L 1
 1 '*+( )/ $/M$)./'* (. 0*(+0/*-+- 3 0/- ./ +..*F
* $)03*0D .. *$- />-$* "' **(/ $"$/ E
$ chmod +x *.sh
$ ./startup.sh; tail -f ../logs/catalina.out
www.algaworks.com 20
- 0( +(0,+  .. * ) - :* !--);??%(%!(,-;FBFBF   / ' $3* +-  -
+-1*<D+-;).D**(/ ./7$)./'* !0)$*))* (. 0*(+0/*-F
- $)/ --*(+ -  3 0:9* * *(/D 3 0/ * -,0$1* ,!.-(0'6- L$)*2.M
*0 ,!.-(0'6,! L $)03*0MF
RFWF
)/ "-)**'$+. *(*+# *(/
* (*. $)$$- * *(/  )/-* * '$+. +- ")#-(*. ($. +-*0/$1$ D
(. +- $..*D +- $.(*. * +'0"$)  L ((%, %-(+&MF  %"),
 (+
/  /%()+, %7 1 ( *( .. +'0"$)D ,0 ';(  +*..$$'$/-  $)/ "-:9* 
('-"'+, * '$+. D 1 ( *( $1 -.*. $/*- . ,0 03$'$( *  . )1*'1$( )/* 2 
)*1.+ -.+ /$1.F
(*. $)/ "-- * +# *(/ * '$+. D +- +* -(*. $)$$- +-- * *(/D
';( $(+')/-.+'$:@ .+-/$-*($ )/   . )1*'1$( )/*F
 ..  /"0 +/+, '$,0 )* A)$* '$)& ,0 +-  +- $$*)- 0( )*1*
. -1$*-F
www.algaworks.com 21
/ ',0 -$-D )*)/-  . ' $*) (&-F<B+/+F +*$.D'$,0  ( 1-F
'$,0 )* */9* +(0,<<<D . ' $*) * $- />-$* -$5 *) * *(/ !*$
 .*(+/* '$,0  ( "'",!F
www.algaworks.com 22
*<1 -7**(/$$*)*) /"0 +/+,F
< 0( 0+'* '$,0 )* . -1$*- * *(/ $$*)* ) /"0F -,0  *+:9* ,
(&- "',-%%-"(' ( +/+ (-"(', .'1  '/ -:9*F 5 (*. $..* +- ,0
*0. .( .(.*)B"0-:@ .$)./':9***(/F
- $)$$- * *(/  )/-* * '$+. D +-$( $-( )/ D *)B-( ,0 * . -1$*- )9*
./7 -*)* !*- * '$+. F  +*$.D . ' $*)  '$)# ,0 - +- . )/ * . -1$*-
$$*)* '$,0 )*=*) F
 /0* - -/*D1*<1 -7) /"0 +/+, ,0 **(/ ./7$)$$*L-+-MF
-0( +(0,+  .. * ) - :* !--);??%(%!(,-;FBFBF
www.algaworks.com 23
*+:9* (&-F<B+/+ )9*+-  0I
 1*< ./$1 - 0.)* * '$+. +' -  *+:9* (&- F<B +/+ )9* +-  - )
/ ' 0+/+D1*< 1 /0'$5-*+'0"$)*F
 .. #//+EJJ*2)'*F '$+. F*-"J2 /**'.J*2)'*.J $3 0( 1 -.9* * /$+*
%,F
*'$5 * '$)& +- *2)'* *  )) /%()+, ) . :9* +"-"('% ")
"%,F
www.algaworks.com 24
 +*$. ,0 $3- * -,0$1* 
D  .*(+/ *+$ * *)/ A* . +./. %"),9
-.+, %"),9)%. "', +- . +./.  )*( . *-- .+*) )/ . ) .0 $)./':9*
*'$+. F
(*- $..* !0)$*) D * - *( )* ; ,0 1*< $)./'  A'/$( 1 -.9* * '$+. D
,0 %7/ (.0+*-/ **(/YF
-%!(,('9 .+78,)+ '!+)+(.-"/"(&%"),
 1*< $) )9* ./7 (0$/* !($'$-$5* *( * '$+. D .0"$-* ,0 ..$./
6 1= * 0' "-/0$/ ,0 +*./(*. ( )*..* '*" .*- *. /'#*.
*)B"0-:@ .,0 19*$(+0'.$*)-.0+-*0/$1$ *(
F
#//+EJJ'*"F'"2*-&.F*(J/'#*.G G*)B"0-* .G+-G")#-G
+-*0/$1$ G*(G '$+. J
www.algaworks.com 25
RFXF +# 1 )
1 ) ; 0( ! --( )/  +# *!/2- *0)/$*) +- " - )$( )/* 
 + )<)$. 0/*(:9* ."%D+-$)$+'( )/  (+-*% /*. 1F
( +-*% /* ,0 0. 1 ) +*..0$ 0( -,0$1*  L)(&61&%M ,0  .- 1 * +-*% /*D
.0.  + )<)$.D  /'# . * ."%D $- />-$*.D +'0"$). - ,0 -$*.D /F ./ -,0$1*
;*)# $**(*L+(#-#- (%MF
.- (*. 1 ) ) ./ '$1-* +- -$- *. 3 (+'*.F . 1 -.@ . ($. -  )/ . *
'$+. %7+*..0$0(+'0"$)+--$-+-*% /*.*(1 )F
RFYF -$( $-*+-*% /*2 *(+# 1 )
--$-0()*1*+-*% /**(1 )D .. *( )0 "%D 0D /'+(#-F
-,0 *+:9* +-,"&)%)+(#- '$,0  ( 1-GF
www.algaworks.com 26
- )# * (+* +(.)
 *( 0( )*( A)$* ,0 $ )/$B .0 *-")$5:9*
L*(=)$* * *)/-7-$*M +-"-
 0( $ )/$B*- A)$* * +-*% /*  )/-* 
*-")$5:9*F ' $*) 0+ ). ' :9* $ "' F +*$.D'$,0  ( "'",!F
www.algaworks.com 27
( +-*% /* 2  . -7 -$*  )/-* * 2*-&.+ * '$+. D *( 0( -,0$1* )(&61&%
7.$*E
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.algaworks</groupId>
<artifactId>Financeiro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
</project>
(*. *)B"0-- 0( +-*+-$  * +-*% /* +- ,0 * +-* ..* ."% 0. 
*$B:9* GY +- *+$- -,0$1*. D /(;(D *)B"0-- * +'0"$)  *(+$':9*
+-$5 -,0 )*..*+-*% /* 1 0.- 1YF
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
www.algaworks.com 28
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.algaworks</groupId>
<artifactId>Financeiro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
- $.(*. /0'$5- * +-*% /* )* '$+. . * ) ... '/ -:@ .F - $..*D
'$(*. *( * */9* $- $/* )* +-*% /*  ..(*. * ( )0 /'D )-
+(#-<<<F
+ ).1 -$B,0 . *+-*% /* ./7. ' $*)* '$,0  (  F
www.algaworks.com 29
- $.(*. 1 -$B- *. +(#- -, $)./'*. )* +-*% /* + '* '$+. F  .. .
+-*+-$  . * +-*% /* )*)/- * ( )0 '/ -' +(#- -,F  ' $*)  1 -.9*
3.1 )* - 2'&"(.%D +*$.D'$,0  (  F
www.algaworks.com 30
 * -,0$1* 061&% !*- -$* )* $- />-$* ,+9&"'90))97
D +* (*.
3'0$-D+*$.)9*+- $.- (*. ' "*-F
(*. -$- 0( . -1' / (0$/* .$(+' .D ,0 + ). 3$ N$ 0)*N +- * 0.07-$*F
'$,0 *( * */9* $- $/* )* +-*% /* -$*D  ..  *+:9* 0 '$,0 ( +/%-F
 / ' ,0 -$-7D $)!*-( * )*( * +*/  '..  . -1' /F  +*$.D '$,0 (
"'",!F
www.algaworks.com 31
 '.. OiMundoServlet . -7 -$ )* +*/ com.algaworks.servlet *( 17-$*.
--*.F
www.algaworks.com 32
 
  -1' /. )9* !*$ )*)/-D +*$. )9* !*$  '- *(* 0(  + )<)$
* +-*% /*F - $.(*. $$*)- ..  + )<)$ )* )(&61&%D $)'0$)* * /- #* 
>$"*$3*E
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
1 )$-7$3- + )<)$ $)./'-)*- +*.$/>-$*'*'(7,0$)F
-*"-((*.. -1' / OiMundoServletD $3)**>$"**(*$3*E
package com.algaworks.servlet;
// imports...
@WebServlet("/oi-mundo")
public class OiMundoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
PrintWriter out = response.getWriter();
out.print("<html>");
out.print("<body><h1>Oi Mundo</h1></body>");
out.print("</html>");
}
}
"*-D +- $.(*. $$*)- * +-*% /* * *(/D +- ,0 ' !:  $(+')/:9*
. (+- ,0 #*01 - '"0( (*$B:9*F  /"0 +/+,D '$,0 *( * */9* $- $/*
)*. -1$*-**(/  .. *+:9* '&(/<<<
www.algaworks.com 33
-,0 * +-*% /* ) '$./" (  .,0 - /-).B- +-  '$./" (  $- $/D
'$)*)**/9* GF +*$.D'$,0  ( "'",!F

)$$ * *(/D . ' )9* ./$1 - . )* 3 0/*D  +*$.D  ..


!--);??%(%!(,-;FBFB?"''"+(?("=&.'(F
 . -1' / - .+*) -7  - ,0$.$:9* +- . )/-7  ( )." ( N$ 0)*N )*
)1 "*-F
www.algaworks.com 34
www.algaworks.com 35
+=/0'*S
 -.$./<)$ *.*( 
SFRF ,0 ;+ -.$./<)$I
 ($*-$ *. .$./ (.  . )1*'1$*. ( 0( (+- . +- $.  *. + -.$./ )/ .D
+*-/)/* + -.$./<)$ ; 0( *) $/* !0)( )/' )*  . )1*'1$( )/*  +'$:@ .F
 0( .$./ (  $)!*-(:9* )9* +- . -1.. *. *. ,0)* ' !*.. ) --*D *
.$./ ()9*. -$+-7/$* 0.0'F
0)* !'(*.  + -.$./<)$  *. *( 1D )*-('( )/ !'(*. * 0.* 
.$./ (. " - )$*- .  )*  *. - '$*)$.  D +*-;( 3$./ ( $1 -..
*0/-.'/ -)/$1.+-+ -.$./$-*.D*(* (-,0$1*. D-,0$1*./ 3/*  /F
SFSF + ( )/*% /* '$*)'LM
+ ( )/* *% /* - '$*)' L(#-7+%-"('% &))"' D D J *0 J
(++$)"M ; 0( /;)$  +-*"-(:9* +- *)1 -.9*  *. )/- )* 
*.- '$*)$. '$)"0" ). +-*"-(:9**-$ )/*% /*.F
( )*  *.D )/$ . .9* - +- . )/. +*- / '.D ,0 +*..0 ( *'0).
,0 -(5 )( +-*+-$  .  $1 -.*. /$+*.F ( / ' +* . ..*$- *(
*0/-. -$-- '$*)( )/*.$1 -.*.F
( 0( '$)"0" ( *-$ )/  *% /*.D *(* 1D )/$ . .9* '.. .D *% /*.
 ... '.. . - +- . )/( ' ( )/*. ,0 3$./ ( )* (0)* - 'F *- 3 (+'*D 0(
.$./ (  !/0-( )/* +*..0$  '.. NotaFiscalD ,0 )* (0)* - ' 3$./ /**
(0)* %7 1$0 '"0( + '* ( )*. 0( 1 5D ';(  +*..0$- 0( '.. ,0 +*
. #(- ImpostoD ,0 $)! '$5( )/ /** (0)* . )/ )* *'.*F ... '.. . .9*
www.algaworks.com 36
#(.  '.. .  *(=)$* * .$./ (D +*$. !5 ( +-/ * ) ">$* ,0 ./7
. )* . )1*'1$*F
( )*  *.D +* (*. / - . / '. '(-;3,% /(;( "&)(,-(D (. 
./-0/0-  )*  *. - '$*)' ./7 '*)"  . - *-$ )/*  *% /*.D +*-
$..*   !*$ $)1 )/ +- .0+-$-  )  ..$ ,0 *.  . )1*'1 *- . /<( 
1$.0'$5-/0**(**% /*.+-+-*"-(- (*(($.!$'$ F
* (*. *(+-- * (* '* - '$*)' *( * (* '* *-$ )/*  *% /*. *)!*-(
/ '$3*E
(%(+%"('% (%(
 ' '..
$)# % /*
*'0) /-$0/*
G ;/**
#1  ./-)" $- ..*$:9*
.. *(+-:9* ; ! $/ ( /** * / (+* ,0 . ./7  . )1*'1 )* 0.)* '"0(
( )$.(*  F  (+ ( )/* ; ! $/* 0.)* ( /*. ,0  .- 1 ( 
- ':9* )/- *% /*. )* *.F
( .*'0:9*  *).$./  0( 
+- 3 0/- *+ -:@ .  .$(+' . (
*% /*.  '.. . + -.$./ )/ .D 0( '$)"0" ( *0 
+- .+ $B- ,0 -$ . ,0 .
- ! - (  '.. . +-*+-$  .  '.. .D !$'$ . +- .+ $B- ( /*. 
(+ ( )/* /;)$. +- $)/ -"$- *( *% /*. /-).$*)$. +- $ )/$B- (
0/*(/$( )/ '/ -:@ . - '$5.D -- "( )/*  ..*$:@ . +*-  ()
*0/-.!0):@ . */$($5:9*F
( 0( ($ )/ D . +'$:@ . $)/ -" ( *( 
. * (* '*  '.. .
 *(=)$* *. >$"*.  J  .9* ./-=*.F . *()*.  .9*
0/*(/$( )/ " -*.  +-/$- *. ( /*. ,0 - '$*)( *% /*.  )* 
*.F
www.algaworks.com 37
SFTF *-,0 0.-I
( $(+' ( )/:9*  ; ($. *(+' 3 ,0 *0/-* !-( 2*-& ,0',0 - +-
 . )1*'1$( )/* 2 D +*-;( *.  ) !=$*.   . )1*'1 - 0/$'$5)* ./ / )*'*"$
.9*"-) .F
>$"*.   ..*  )*  *. *( ,0 -$ .  .9* #/*.  .  . )1*'1 -F
 '$($) (0$/* * /-'#*  $3 1*< . *) )/-- ) '>"$  ) ">$*F 
/--70(+-*0/$1$ $( ).+-1*<F
 ()0/ )$'$  .$./ (.  . )1*'1$*. *(  ; 3 ' )/ D +*$. *
( )$.(* !5 *( ,0 ( )*. '$)#.  >$"* . %( )  ..7-$.F ';(  !$'$/- *
)/ )$( )/*D( )*.'$)#. >$"* $3(*.$./ (($.!7$' . -'/ -*F
3$./ ( *0/-. -5@ . ,0 !5 ( *( ,0 0( .$./ (  . )1*'1$* 0/$'$5)* 
. % ( '#*-  . - ()/$*F ( .$./ (. *(  (  + -.$./<)$  . )1*'1$
0.)*   D 3$./ 0( /-'#* ) $(+' ( )/:9* +- - +- . )/- / '.
*(* *% /*.  *(=)$*D '/ -:@ . )* )*  *. *0 )* (* '*  *(=)$*
" -(0( .!*-:* -  ,0:9*,0 +* 0./--*F
 ./-$ .0 +'$:9* * )*  *. * $' /*  F *( D 1*< +*
 . )1*'1 - 0( .$./ ( 0.)* 0( )*  *. *'*7G'* ( +-*0:9* 0.)*
$1 -.*. *0/-*. )*  *.D . ( +- $.- '/ -- >$"*.G!*)/ . +-  ,0-
.$)/3  ,0 -$ .,0 .>!0)$*)( (.  / -($)*.!*-)  *- .F
SFUF 1 -.$./ ) 
  $ -)/
 / +,",-' 
L M ; 0( !-( 2*-& +- + -.$./<)$ ( 1D ,0 *! - 
0( 
 (+ ( )/* *% /*G- '$*)' .*'0:@ . +- $)/ "-- + -.$./<)$ *(
.$./ (.*-+*-/$1*. .'71 $.F
*( D *. *% /*. .9*   L%"' % / #-,MD *0 . %D )9* ; )  ..7-$* )
 .+ $' +- /*-)- *. *% /*. + -.$./ )/ .F ./ $$*)- '"0(. )*/:@ . ).
'.. . ,0 - +- . )/( . )/$ . * .$./ ( *( :-  + -.$./$- *0 *).0'/-
*% /*.F
 ; 0( .+ $B:9*D )9* 0( +-*0/*F - /-'#- *( D +- $.(*. 
0($(+' ( )/:9*F
www.algaworks.com 38
 +-*% /* * $ -)/  +*..0$ '"0). (>0'*.D . )* ,0 * "+'-
'-"-2' + ;$(+' ( )/:9* ,0  )+.0'* $ -)/ *- F
 "+'- (+ ;  . +- * !0)$*)( )/*  + -.$./<)$D *( 
. )/$1.
( /*.  (+ ( )/*. ( -,0$1*.  F *..0$ 0( '$)"0" (  *).0'/.
#(  L+- $* *(  MD 0( *)%0)/*  $)/ -! . +- *).0'/. 0.)*
-$/;-$*.L-$/ -$
MD /F
 ./ '$1-*D ./0- (*. + ). * 7.$*   $ -)/ D +- $(+' ( )/-(*.
3 (+'*.($.$)/ - ..)/ .*( D*( ..**)* *.F
SFVF *)B"0-:9*    $ -)/ *(1 )
*(* ./(*. 0.)* 1 )D )9* +- $.(*.  ..- * .$/ * $ -)/ +- $3-
*. -,0$1*. )  ..7-$*. $)'0$- ()0'( )/ )* +-*% /*F * (*. $)'0$- /*.
.  + )<)$. )* -,0$1* )(&61&%D ,0 * 1 ) $3-7 *. -,0$1*. )  ..7-$*.
0/*(/$( )/ F
<dependencies>
<!-- Nœcleo do Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.8.Final</version>
<scope>compile</scope>
</dependency>
<!-- Implementa‹o de EntityManager da JPA -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.8.Final</version>
<scope>compile</scope>
</dependency>
<!-- Driver JDBC do MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
<scope>compile</scope>
</dependency>
...
</dependencies>
www.algaworks.com 39
$$*)(*. * )A' * * $ -)/ D  $(+' ( )/:9*   * -$1 -  *
4  ()*..*+-*% /*F
SFWF -$:9***($)* '
( )*..* +-*% /*  3 (+'*D $(+' ( )/- (*. 0( .$./ ( B)) $-* .$(+' .F
)/ .  ,0',0 - *$.D +- $.(*. -$- )*..* (* '*  *(=)$* +- * ) ">$*
(,0 ./9*F
*..* .$./ ( +*..0$-7 . '.. . Lancamento PessoaD ,0 - +- . )/-9* .
)/$ . ( .(*)*( D';( 0( )0( -:9* TipoLancamentoF
package com.algaworks.financeiro.model;
import java.io.Serializable;
public class Pessoa implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String nome;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}
package com.algaworks.financeiro.model;
public enum TipoLancamento {
RECEITA, DESPESA
}
www.algaworks.com 40
package com.algaworks.financeiro.model;
// imports...
public class Lancamento implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private Pessoa pessoa;
private String descricao;
private BigDecimal valor;
private TipoLancamento tipo;
private Date dataVencimento;
private Date dataPagamento;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Pessoa getPessoa() {
return pessoa;
}
public void setPessoa(Pessoa pessoa) {
this.pessoa = pessoa;
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public BigDecimal getValor() {
return valor;
}
public void setValor(BigDecimal valor) {
this.valor = valor;
}
public TipoLancamento getTipo() {
return tipo;
}
public void setTipo(TipoLancamento tipo) {
this.tipo = tipo;
www.algaworks.com 41
}
public Date getDataVencimento() {
return dataVencimento;
}
public void setDataVencimento(Date dataVencimento) {
this.dataVencimento = dataVencimento;
}
public Date getDataPagamento() {
return dataPagamento;
}
public void setDataPagamento(Date dataPagamento) {
this.dataPagamento = dataPagamento;
}
}
. /-$0/*. $ )/$B*- . L#(*.  idM .9* - ! - )/ . 6. #1 . +-$(7-$. )*
)*  *.F *- 3 (+'*D . 3$./$- ( 0. $)./8)$.  Pessoa *( * ( .(*
$ )/$B*-D ' .- +- . )/(( .('$)#)*)* *.F
. '.. .  )/$ .  1 ( . "0$- * ./$'*  1 ).D *( (;/**. --+,
,--+,F*-$"/>-$*,0  ...'.. .+*..0(0(*)./-0/*-. (-"0( )/*.F
SFXF
(+' ( )/:9** ,0'.LM #.#* LM
- ,0 *. *% /*.  )/$ . . %( $! - )$*. 0).  *0/-*.D +- $.(*.
$(+' ( )/-*.(;/**. equals() hashCode()F
* )*  *.D . #1 . +-$(7-$. $! - )$( - "$./-*. $./$)/*.F 0)*
(+ (*. 0( )/$  0( / 'D  1 (*. -$- *. (;/**. equals()
hashCode()D ' 1)* ( *).$ -:9*  !*-( ( ,0 *. - "$./-*. .9* $! - )$*.
)*)* *.F
 '$+. +*..0$ 0( " -*-  .. . (;/**. ,0 0. 0( +-*+-$  L*0 17-$.D
$)!*-(. +*- 1*<M +- -$- * >$"*G!*)/ F  % *(*  1 B-  $(+' ( )/:9*
*.(;/**.+- )/$ PessoaF
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
www.algaworks.com 42
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pessoa other = (Pessoa) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
- $.(*. " -- .. . (;/**. /(;( +-  )/$ LancamentoD * $ -)/
*). "0$-7*(+--*% /*.+- .*-$-. .9**.( .(*.F
.+'-'+&%!(+,(+"&)(+-5'"( equals  hashCode>
..$./ 6 1= * 0' "-/0$/ ( )*..* '*" .*- .. . (;/**.F  (0$/*
$(+*-/)/ ,0 1*< )/ ) $(+' ( )/ *-- /( )/ * equals hashCodeF
#//+EJJ'*"F'"2*-&.F*(J )/ ) )*G*G ,0'.G G#.#* J
SFYF + ( )/*7.$*
- ,0 * (+ ( )/* *% /*J- '$*)' !0)$*) D +- $.(*. $)!*-(- 6
$(+' ( )/:9* *  ($. $)!*-(:@ . .*- *(*  '.. . Lancamento Pessoa
 1 ( . /*-)- + -.$./ )/ .D *0 . %D *(* $)./8)$.  ... '.. . +* ( . -
"-1. *).0'/. )* )*  *.F - $..*D  1 (*. )*/- *. --+, *0 *.
/-$0/*.D';(.+->+-$.'.. .F
@Entity
@Table(name = "pessoa")
public class Pessoa implements Serializable {
private static final long serialVersionUID = 1L;
www.algaworks.com 43
private Long id;
private String nome;
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(length = 60, nullable = false)
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
// hashCode e equals
}
. )*/:@ . !*-( $(+*-/. * +*/ javax.persistenceF  )/-*  .. +*/
./9*/*..)*/:@ .+-*)$5.+ ' F
 )*/:9* @Entity $5 ,0  '.. ; 0( )/$ D ,0 - +- . )/ 0( / ' *
)*  *.D @Table  B)  /'# .  / ' )* )*  *.D *(* +*-
3 (+'**)*( / 'F
@Entity
@Table(name = "pessoa")
public class Pessoa implements Serializable {
. )*/:@ . )*. (;/**. --+, *)B"0-(  - ':9* *. /-$0/*.  '.. *(
. *'0). * )*  *.F . )*/:@ . @Id @GeneratedValue .9* 0.. +-
 '-- * $ )/$B*- * )*  *.D .. $ )/$B*-  1 / - 0( 1'*-
" -*)*(*( )/* $). -:9*L0/*G$)- ( )/*MF
@Id
@GeneratedValue
public Long getId() {
return id;
}
 B)$(*. ,0  +-*+-$  nome / ( /()#* ,0 *(+*-/ /; WQ -/ - . )9*
 $/ 1'*- . )0'*.D *0 . %D ,0 - (*. -$- 0( - ./-$:9* '(- '.%% )* )*  *.F
www.algaworks.com 44
*(* )9* $)!*-((*. * )*(  *'0) )* )*  *.D ' -   -7 * ( .(*
)*( +-*+-$  F
@Column(length = 60, nullable = false)
public String getNome() {
return nome;
}
(*. (+ -  '.. LancamentoD ,0 ; 0( +*0* ($. /-'#*. 0. )*1.
)*/:@ . F
@Entity
@Table(name = "lancamento")
public class Lancamento implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private Pessoa pessoa;
private String descricao;
private BigDecimal valor;
private TipoLancamento tipo;
private Date dataVencimento;
private Date dataPagamento;
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToOne(optional = false)
@JoinColumn(name = "pessoa_id")
public Pessoa getPessoa() {
return pessoa;
}
public void setPessoa(Pessoa pessoa) {
this.pessoa = pessoa;
}
@Column(length = 80, nullable = false)
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
www.algaworks.com 45
@Column(precision = 10, scale = 2, nullable = false)
public BigDecimal getValor() {
return valor;
}
public void setValor(BigDecimal valor) {
this.valor = valor;
}
@Enumerated(EnumType.STRING)
@Column(nullable = false)
public TipoLancamento getTipo() {
return tipo;
}
public void setTipo(TipoLancamento tipo) {
this.tipo = tipo;
}
@Temporal(TemporalType.DATE)
@Column(name = "data_vencimento", nullable = false)
public Date getDataVencimento() {
return dataVencimento;
}
public void setDataVencimento(Date dataVencimento) {
this.dataVencimento = dataVencimento;
}
@Temporal(TemporalType.DATE)
@Column(name = "data_pagamento", nullable = true)
public Date getDataPagamento() {
return dataPagamento;
}
public void setDataPagamento(Date dataPagamento) {
this.dataPagamento = dataPagamento;
}
// hashCode e equals
}
 +-$( $- )*1$ ; * (+ ( )/* (0$/*.G+-G0( ( getPessoasF  )*/:9*
@ManyToOne $)$  (0'/$+'$$ * - '$*)( )/* )/- '):( )/*. + ..*.D
 )*/:9* @JoinColumn $)$ ,0 .. - ':9* ; *). "0$ /-1;.  *'0)
.+ $B ) +-*+-$  nameF - !$'$/- * )/ )$( )/*D .. (+ ( )/* !*$
)  ..7-$* +- $5 -(*. * +-*1 *-  ,0 3$./ 0( #1 ./-)" $- ) *'0)
pessoa_id / ' lancamentoD,0 - ! - )$/ ' pessoaF
www.algaworks.com 46
@ManyToOne(optional = false)
@JoinColumn(name = "pessoa_id")
public Pessoa getPessoa() {
return pessoa;
}
/-$0=(*.+- $.9* 10 *( .' 2 ..)*'0) )A( -* $('F
@Column(precision = 10, scale = 2, nullable = false)
public BigDecimal getValor() {
return valor;
}
 /$+* * '):( )/* ; 0( )0( -:9*D +*- $..*D (+ (*. *(  )*/:9*
@EnumeratedD $)$)* ,0 ,0 - (*. -(5 )-  ./-$)"  *)./)/ ) *'0) 
/ 'D )9**=)$ *)./)/ F
@Enumerated(EnumType.STRING)
@Column(nullable = false)
public TipoLancamento getTipo() {
return tipo;
}
. +-*+-$  .  /. !*-( (+ . 0.)*  )*/:9* @TemporalD $)$)*
,0 ,0 - (*.-(5 )-+ )./L. ($)!*-(:@ .#*-MF
@Temporal(TemporalType.DATE)
@Column(name = "data_vencimento", nullable = false)
public Date getDataVencimento() {
return dataVencimento;
}
SFZF -,0$1*+ -.$./ ) F3('
 )+,",-'61&% ; 0( -,0$1*  *)B"0-:9* +-9*  F '  1 . - -$*
)* $- />-$* 7
  +'$:9* *0 * (>0'* ,0 *)/;( *.  ).  )/$ F
* '$+. D *+$*)'( )/ D 1*< +* $$*)- * +(#- -  )* . 0 +-*% /*D
,0  ./-0/0- 7.$  .. -,0$1* ; -$ 0/*(/$( )/ D ';(  / - *0/-.
!$'$ .F
 -,0$1* )+,",-'61&%  B) 0)$ .  + -.$./<)$D *)# $. *(*
)+,",-'.'"-,F
.. -,0$1* 1 B- ( ,+9&"'9+,(.+,9 7
F
www.algaworks.com 47
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="FinanceiroPU">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost/financeiro" />
<property name="javax.persistence.jdbc.user"
value="usuario" />
<property name="javax.persistence.jdbc.password"
value="senha" />
<property name="javax.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
 )*(  0)$  + -.$./<)$ !*$  B)$* *(* FinanceiroPUF - $.- (*.
 .. )*( ,0$+*0*D,0)*!*-(*.*'*-/0*+-!0)$*)-F
 provider $5 ,0' ;  $(+' ( )/:9* ,0 . -7 0. *(* +-*1 *- 
+ -.$./<)$F
3$./ ( 17-$. *+:@ .  *)B"0-:9* ,0 +* ( . - $)!*-(. ) ./ -,0$1*  F
 %(*..+-$)$+$.+-*+-$  .,0 0.(*. ()*..*-,0$1* *)B"0-:9*E
K javax.persistence.jdbc.urlE  .-$:9*    *) 39* *( * )*
 *.F
K javax.persistence.jdbc.driverE )*( *(+' /*  '.. * -$1 - F
K javax.persistence.jdbc.userE)*( *0.07-$**)* *.F
K javax.persistence.jdbc.passwordE. )#*0.07-$**)* *.F
K hibernate.dialectE$' /*. -0.*)*)./-0:9* *()*. F
www.algaworks.com 48
K hibernate.show_sqlE $)!*-( . *. *()*.   1 ( . - 3$$*. )
*).*' L$(+*-/)/ +- . D (.  1 . -  .$'$/* ( ($ )/ 
+-*0:9*MF
K hibernate.format_sqlE $)$ . *. *()*.  3$$*. ) *).*'
 1 ( . - !*-(/*. L!$'$/  *(+- ).9*D (. +* " -- / 3/*. '*)"*.
).=MF
K hibernate.hbm2ddl.autoE -$ *0 /0'$5 0/*(/$( )/  ./-0/0- .
/ '.)*)* *.F
SFRQF  -)*./ '.*)* *.
*(* $) )9* / (*. . / '. - +- . )/. + '. '.. . Pessoa Lancamento )*
)* *.D+- $.(*.-$7.G'F
 $ -)/ +* !5 - $..* +- " )/ D "-:. 6 +-*+-$  hibernate.hbm2ddl.auto
*(1'*- updateD,0 $)'0=(*.)*-,0$1* )+,",-'61&%F
- $.(*. + ). -$- 0( EntityManagerFactoryD ,0 /*. . / '. (+ .
+ '. )/$ .. -9*-$.*0/0'$5.F
import javax.persistence.Persistence;
public class CriaTabelas {
public static void main(String[] args) {
Persistence.createEntityManagerFactory("FinanceiroPU");
}
}
 +-8( /-* * (;/** createEntityManagerFactory  1 . - * ( .(* )*( ,0
$)!*-((*.)*/-$0/* name  - persistence-unitD)*-,0$1* )+,",-'61&%F
* 3 0/-*>$"*D./ '..9*-$.F
...
Set 30, 2013 1:50:52 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
Set 30, 2013 1:50:52 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
Set 30, 2013 1:50:52 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
Set 30, 2013 1:50:52 PM org.hibernate.tool.hbm2ddl.DatabaseMetadata...
INFO: HHH000262: Table not found: lancamento
Set 30, 2013 1:50:52 PM org.hibernate.tool.hbm2ddl.DatabaseMetadata...
www.algaworks.com 49
INFO: HHH000262: Table not found: pessoa
Set 30, 2013 1:50:52 PM org.hibernate.tool.hbm2ddl.DatabaseMetadata...
INFO: HHH000262: Table not found: lancamento
Set 30, 2013 1:50:52 PM org.hibernate.tool.hbm2ddl.DatabaseMetadata...
INFO: HHH000262: Table not found: pessoa
Set 30, 2013 1:50:52 PM org.hibernate.tool.hbm2ddl.DatabaseMetadata...
INFO: HHH000262: Table not found: lancamento
Set 30, 2013 1:50:52 PM org.hibernate.tool.hbm2ddl.DatabaseMetadata...
INFO: HHH000262: Table not found: pessoa
Set 30, 2013 1:50:53 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
SFRRF ->3$(*.+..*.
7 / (*. )*... )/$ . Pessoa Lancamento (+ . . / '. -$. )*
)*  *.F - $.- (*. + -.$./$- *% /*.D *).0'/-D 3'0$- /0'$5-D (.
 $3- (*. +- +- . )/- *(* !0)$*) .. .  /'# . + ). ,0)* !*-
)  ..7-$*D+*$. ./ )9*;0('$1-*  D*&I
www.algaworks.com 50
+=/0'*T

)/-*0:9** 
TFRF ,0 ; 1 -1 - .I
1 -1 -  .D /(;( *)# $* *(* D ; 0( / )*'*"$ +-
 . )1*'1$( )/* 2  ,0 0/$'$5 0( (* '*  $)/ -! . "-7B. . * (
1 )/*.F ./ / )*'*"$ !*$  B)$ + '*  L / (&&.'"-2 +(,,MD * ,0  /*-)
0( +-9*   . )1*'1$( )/* !$'$/ * /-'#* *. !*-)  *- .  ! --( )/.D
* -$- ( +-*0/*. ,0 1'*-$5 (  +-*0/$1$ )*  . )1*'1$( )/*  $)/ -! .
1$.0$.F
 ; . * )* +-9*  +-*% /*  L* ' $ 2 *)/-*'' -MD * ,0 /*-) *
 . )1*'1$( )/*  .$./ (. ( )*. *(+'$*F  +-9*  . +- * .$./ ( (
/-<. - .+*).$'$ . L(* '*D 1$.0'$5:9* *)/-*' MD *) * (* '* ; - .+*).71 '
+*- - +- . )/- *. *% /*.  ) ">$*D ()/ - * ./*  +'$:9* !*-)  - *
*)/-*'*- *  ..* *. *.F  1$.0'$5:9* ; - .+*).71 ' + ' $)/ -! * 0.07-$*F
' ,0  B)  !*-( *(* *. *. .9* +- . )/*. )($)# . :@ . *
0.07-$* +- * *)/-*'*-F  *)/-*'*- ; - .+*).71 ' +*- '$"- * (* '* 
1$.0'$5:9*D $)/ -+- /)* . .*'$$/:@ . * 0.07-$*D /-05$)* +- 0( *+ -:9*
)* (* '* L*) .9* - '$5. ! /$1( )/ . (0):. )* .$./ (M - /*-))*
1$.0'$5:9* ,06.*'$$/:9*F
( D * *)/-*' ; ! $/* /-1;.  0( . -1' / #( , +/%-D
*+$*)'( )/ D +*- -,0$1*.   *)B"0-:9* +*- 17-$*. ()$+0'*- . 
:@ . *. -1*- .  1 )/*.F  , +/%- -   . - ,0$.$:@ . *. 0.07-$*. )
2 D- $- $*)+-*(* '*  )1$0(- .+*./F
 (* '* ; - +- . )/* +*- *% /*.  ) ">$*D ,0 3 0/ 0( '>"$  ) ">$*
*-   -*.*-$0)*.( 1$.0'$5:9*F
www.algaworks.com 51
 1$.0'$5:9* ; *(+*./ +*- 0( #$ --,0$  *(+*) )/ . L(&)(''- -+MD *
,0 /*-) +*..=1 ' 0)$- *(+*) )/ . +- *)./-0$- $)/ -! . ($. -$. *(+' 3.F
TFSF -$)$+$.*(+*) )/ .
 1 - $-* +* -  1 -1 -  . ./7 ( . 0 (* '*  *(+*) )/ . 
$)/ -! * 0.07-$*D ,0 " - '/ +-*0/$1$ *.  . )1*'1 *- .D + -($/$)*
 *)./-0:9*  $)/ -! . +- 2  0.)* 0( *)%0)/*  *(+*) )/ . +-;G
*)./-0=*.D*$)1;. -$-$)/ -! .$)/ $-( )/ *5 -*F
3$./ ( 17-$*. *(+*) )/ . D  . *. ($. .$(+' .D *(* 0( .-).- %D ,0
+- . )/ .$(+' .( )/ 0( / 3/*D *0 0( - %D ,0 - +- . )/ *. /0'- .
 0(*' :9*,0 +* 1$-*)* *.F
 
  .0+*-/  3/ ).9* -$:9*  )*1*. *(+*) )/ .D ,0 +* ( !*-)  -
!0)$*)'$ . $$*)$.F . +-$)$+$. *(+*) )/ . ,0  $(+' ( )/:9* 
- ! -<)$ *  !*-)  .9*E !*-(0'7-$*D (+*.  )/-  / 3/* . )#.D
->/0'*.D'$)&.D*/@ .D( )." ).D+$);$.D/ ' *.D /F
TFTF $'$*/ . *(+*) )/ . / - $-*.
/0'( )/ D 3$./ ( $1 -.. *-")$5:@ . ,0 /-'#( ) -$:9*  *(+*) )/ .
+ -.*)'$5*.D *(* 3 (+'*D +* (*. $/-  -' L  . $# '$ )/MD

 *!/L
  .MD  /L$# .MD-$(  #)*'*"4L-$(  .M  /F
. $'$*/ .  *(+*) )/ . / - $-. $)'0 ( (0$/*. *(+*) )/ . $)/ - ..)/ .D
*(* / '.  *. 1):.D ( )0. .0.+ ).*.D */@ .D --.  +-*"- ..9*D
$7'*"*.D*(+*) )/ .+-+/0- /. *- .D /F
www.algaworks.com 52
.- (*. -$(  . )* +-*% /*  ./ '$1-*D (. )/ .D 0.- (*. + ). *.
*(+*) )/ .7.$*.* F
TFUF .*'# )*0($(+' ( )/:9*  
  !*$ -$ /-1;. * / (&&.'"-2 +(,, L MD ,0 ; 0( )/$ !*-(
+ '. ($. $(+*-/)/ . (+- ..  / )*'*"$ * (0)* .+ $'$./. ( $1 -.*.
..0)/*.F
  ; *(+*./* +*- 17-$*. "-0+*.  /-'#*D ,0 .9* #(*.   L /
)"3-"(' *.,-MF (  ; 0( +-*% /*  0( )*1 / )*'*"$F  -/ !/*
+-*05$* /-1;. . . .9* *0( )/:@ .D $)/ -! . '"0(. '.. . ,0
.+ $B(*(* 1 !0)$*)-0()*1*+-*0/*F
  !*$ -$ ; *)/-*' + '*  /-1;.  .F 0)* 0(  ; B)'$5D
(+- .. !*-)  *-.  / )*'*"$ /<(  #)  )/ ) -  .+ $B:9*
$(+' ( )/-0(+-*0/*B)'*(+/=1 '*(*+-*+*./*+ ' .+ $B:9*F
* .*  D  $(+' ( )/:9* ($. *)# $ /0'( )/ ;  (#++D ,0 +*
. -*/$ ( #//+.EJJ%1. -1 -! .F%1F) /JF
www.algaworks.com 53
TFVF $$*))* *+-*% /*1 )
*(* ./(*. 0.)* * 1 )D )9* +- $.- (*. $3-  $(+' ( (/:9*  
()0'( )/ F* (*.+ ).$$*)- + )<)$)**+-*% /*F
<!-- Mojarra (implementacao do JSF) -->
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.faces</artifactId>
<version>2.2.10</version>
<scope>compile</scope>
</dependency>
.- (*.* SFSD'):* )/-* 1XF
$$*)- (*. * +(#- - /+/+ , *(  1 -.9* SFS +- * '$+. )*.
03$'$-( '#*-0-)/ * . )1*'1$( )/*F
TFWF )"  )
)/$"( )/ D '"0). +-*"-(*- .  . )1*'1$( /** * *(+*-/( )/*  0(
+7"$) )* +->+-$* -,0$1*  '4*0/D ) 1 - D $)! '$5( )/ D $) 3$./ (
+-*"-(*- .,0 !5 ($..*F
www.algaworks.com 54
( D )9* *). "0$(*. !5 - $..*F  -,0$1* ,0 $)'0$ *. *(+*) )/ .  +7"$)
 1 B- . +-*  '.. ,0 " - )$ * *(+*-/( )/*  'D #( 
()"  )F
. ()"   ). ) ($. .9* ,0 1  ).D ,0 . -1 ( *(* )$. )/- 
$)/ -! "-7B L +7"$)M * $7'  +'$:9* L- "-.  ) ">$*D  ..* *
)* *.D /MF
.  ). " - )$*. *  +* ( -   - *. $"$/*. + '*. 0.07-$*. /-1;. 
'"0( +7"$)D +-* ..- (;/**. /-1;.  :@ . *. 0.07-$*. !*-)  - *. +-
+- . )/:9*)+7"$)F
- 0(  ) . - - *)# $* *(* 0( ()"   ) D +- $.(*. - "$./-7G'*F
 () $- ($. !7$'  !5 - $..* ; /-1;.  )*/:9* @ManagedBeanD * +*/
javax.faces.beanF *- +-9*D /*. . '.. . * +-*% /* . -9* .) . +-
)*)/-- ).)*/*.F
*..* +-$( $-* 3 (+'* . -7 * ()"   ) OlaBeanF . /-$0/*. nome
sobrenome . -9* $)!*-(*. + '* 0.07-$*.D +*- $..*D +*..0 ( *. --+, ,--+,
*-- .+*) )/ .F  /-$0/* nomeCompleto . -7 (*)/* + '* (;/** dizerOla
+- . )/* ) +7"$)D +*-/)/*D )9* +- $.(*. * ,--+ +- .. /-$0/*F 
(;/** dizerOla . -7#(*+-/$- 0(*/9*+7"$)F
@javax.faces.bean.ManagedBean
public class OlaBean {
private String nome;
private String sobrenome;
private String nomeCompleto;
public void dizerOla() {
this.nomeCompleto = this.nome.toUpperCase()
+ " " + this.sobrenome;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getSobrenome() {
return sobrenome;
}
www.algaworks.com 55
public void setSobrenome(String sobrenome) {
this.sobrenome = sobrenome;
}
public String getNomeCompleto() {
return nomeCompleto;
}
}
TFXF -$)*0(+7"$) 
(*. -$- 0( +7"$) .$(+' . ( D ,0 +*- ),0)/*D )9* !-7 '$":9* *( *
()"  ),0 +-*"-((*.F
-$- (*. 0( -,0$1* #(* %61!-&%D '$)* *( * */9* $- $/* )* +-*% /*
 ..)* 0D  "%F  / ' 0  "%D $"$/ * )*( * -,0$1*
'$,0  ( "'",!F-,0$1*. -7-$*)*$- />-$* ,+9&"'90)) *+-*% /*F
www.algaworks.com 56
 $3- (*.*>$"*G!*)/ *-,0$1* %61!-&% *(*. "0$-E
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Ol‡ JSF</title>
</h:head>
<h:body>
<h:form>
<h1>Ol‡</h1>
Nome: <h:inputText />
<br/>
Sobrenome: <h:inputText />
<br/>
<h:commandButton value="Dizer ol‡" />
</h:form>
</h:body>
</html>
  '-:9*  !*$ 0. +- $5 - *. +(0,+, *. 0.07-$*.  1 -.9* *
 ,0 ./(*. 0.)*D +- ,0 ' . +*..( 3$$- *. ' ( )/*.  !*-(
 ,0F()*..* 3 (+'*D '-(*.,0 ;*  VF
<!DOCTYPE html>

(+*-/(*.  $'$*/   *(+*) )/ .  /-1;. * )( .+


!--)5991&%',6#)6(+ 9#,9!-&%F  ' /- h ; * +- B3* 0.* +-  ..- *. *(+*) )/ .
 ..$'$*/ F
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
. /". <h:head> <h:body> .9* $(+*-/)/ . +- * !0)$*)( )/*  +7"$) D
+- B)$-* :'#* *-+*+7"$)F
<h:head>
<title>Ol‡ JSF</title>
</h:head>
<h:body>
...
</h:body>
www.algaworks.com 57
* *-+*  +7"$)D 0.(*. 0( *(+*) )/  !*-(0'7-$*D - +- . )/* + ' /"
<h:form>D *$. *(+*) )/ .  )/-  / 3/*D - +- . )/*. + ' /"
<h:inputText> 0(*/9*D*( <h:commandButton>F
<h:form>
<h1>Ol‡</h1>
Nome: <h:inputText />
<br/>
Sobrenome: <h:inputText />
<br/>
<h:commandButton value="Dizer ol‡" />
</h:form>
*).$ -)* ,0 .. 3 (+'* ./ % )* +-*% /* N$)) $-*ND +* (*.  ..- 
+7"$),0 -$(*.+ ' !--)599%(%!(,-5A<A<9"''"+(9,9%61!-&%F
 .. * >$"*G!*)/  +7"$) + '* )1 "*-F * .* * **"' #-*( D '$,0
*(**/9*$- $/*)+7"$)  +*$. ( "0 (.+F
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head id="j_idt2">
<title>Ol‡ JSF</title></head><body>
<form id="j_idt5" name="j_idt5" method="post"
action="/Financeiro/faces/Ola.xhtml"
enctype="application/x-www-form-urlencoded">
<input type="hidden" name="j_idt5" value="j_idt5" />
<h1>Ol‡</h1>
Nome: <input type="text" name="j_idt5:j_idt7" />
www.algaworks.com 58
<br />
Sobrenome: <input type="text" name="j_idt5:j_idt9" />
<br /><input type="submit" name="j_idt5:j_idt11" value="Dizer ol‡" />
<input type="hidden" name="javax.faces.ViewState"
id="j_id1:javax.faces.ViewState:0"
value="7050636473587579887:292279611572368337" autocomplete="off" />
</form></body>
</html>
 $(+' ( )/:9* *  " -*0 * >$"*G!*)/   +-/$- *. *(+*) )/ . ,0
$$*)*.6+7"$)  F
TFYF $")*1'*- . :@ .*(
 +*$. ,0 * ()"   ) ; - "$./-*D ' +* . -  ..* + '*. *(+*) )/ .
. +7"$). * +-*% /*F  ($*-$ *. *(+*) )/ .  +*..0$ +-*+-$  . ,0 )*.
+ -($/ ( .+ $B- 0( 1'*- *0 0( '$":9*  1'*- ,0 ./7 ..*$*  0(  )F
*- 3 (+'*D+* (*. .+ $B-0(1'*- ./7/$*)**(+*) )/
').-1-E
<h:inputText value="Ol‡ mundo!" />
1)+,,"(' ' .  L M /*-) +*..=1 ' *  ..* -7+$*  ()"   ).F  1'$*-
 3+- ..@ . ; - .+*).71 ' +*- /-/- 3+- ..@ .  ,0 ./9* )/- *. -/ - . #{
}F * 3 (+'* $3*D '$"(*. * 1'*- * *(+*) )/
').-1- 6 +-*+-$  nome
*()"  ) OlaBeanD/-1;.* --+ ,--+F
<h:inputText value="#{olaBean.nome}" />
0)* * *(+*) )/ !*- - ) -$5*D * (;/** getNome . -7 $)1**F 7 * (;/**
setNome . -7 #(* ,0)* * 0.07-$* $"$/- '"* )* *(+*) )/  )/- 
/ 3/* .0( / -*!*-(0'7-$*F
 )*( olaBean L*( $)$$' ( ($)A.0'*M ;  B)$* +*- +-9*D  *-* *( *
)*( '.. *()"  )D,0)*)9* .+ $B(*.0(*0/-*)*( F
@ManagedBean
public class OlaBean {
}
* -=(*. B)$-0()*( $! - )/ /-$0$)* name )*/:9* @ManagedBeanF
www.algaworks.com 59
@ManagedBean(name = "ola")
public class OlaBean {
}
"*-D ..*$- (*. *. 1'*- . J*0 :@ . *. *(+*) )/ . 6. +-*+-$  .
(;/**. OlaBeanF
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Ol‡ JSF</title>
</h:head>
<h:body>
<h:form>
<h1>Ol‡ #{ola.nomeCompleto}</h1>
Nome: <h:inputText value="#{ola.nome}" />
<br/>
Sobrenome: <h:inputText value="#{ola.sobrenome}" />
<br/>
<h:commandButton value="Dizer ol‡"
action="#{ola.dizerOla}" />
</h:form>
</h:body>
</html>
0)* * */9* N$5 - *'7N !*- $*)*D * !-( 2*-& #(-7 * (;/** dizerOla
* ()"   )D ,0 +..-7 * )*( +- ($A.0'*D *)/ )-7 *( * .*- )*(
/-$0$-7 6 1-$71 ' nomeCompletoD ,0 ;  .. + ' +7"$) /-1;. * --+D +-
$5 -N'7.*- )*( NF
www.algaworks.com 60
TFZF .*+*. ()"  ).
0)* - ! - )$(*. 0( ()"   ) 1$  D * !-( 2*-& *  $)./)$-7 0(
*% /*  '.. * ()"   )D *0 - 0+ --7 0( $)./8)$ 3$./ )/ F *. .
$)./8)$. +*..0 ( 0( / (+*  1$D ,0 ;  B)$*  + ) )* * .*+* 0.*
)*()"  )F
. .*+*.  ()"   ).  +* ( . -  B)$*. /-1;.  )*/:@ . * +*/
javax.faces.beanF.+-$)$+$..9*E
K @NoneScopedE* ). -7$)./)$*1 5,0 !*-- ! - )$*F
K @RequestScoped L+-9*ME / ( 1$ 0-/D *( :)* ,0)* ;
- ! - )$* ( 0( A)$ - ,0$.$:9*  / -($))* ,0)* 
- .+*./; )1$ 1*'/*'$ )/ F
K @ViewScopedE  $)./8)$ + -()  /$1 /; ,0 * 0.07-$* )1 "0 +-
0(+->3$(+7"$)F
K @SessionScopedE ()/;(  $)./8)$ 0-)/ $1 -.. - ,0$.$:@ . /;
( .(* )1 ":@ . )/- +7"$).D /; ,0  . ..9* * 0.07-$* . % $)1'$
www.algaworks.com 61
*0 * / (+* '$($/ ; /$)"$*F  0.07-$* +*..0$ .0 . ..9*  )1 ":9*D
+*-/)/*D*.*% /*.)9*.9**(+-/$'#*. )/- *.0.07-$*.F
K @ApplicationScopedE ()/;(  $)./8)$ 0-)/ /** * / (+* 
3 0:9*  +'$:9*F  0( .*+* ,0 *(+-/$'# *. *% /*. +- /**.
*.0.07-$*.*.$./ (F
- 3 (+'$B- * !0)$*)( )/*  '"0). .*+*.D -$- (*. 0( +7"$) *(
0('$./ )*( .D*(0((+* 0(*/9*+-$$*)-)*1*.)*( .F
 ()"   ) $3* ; - "$./-* )* .*+*  - ,0$.$:9*D +*$. )*/(*. *(
@RequestScopedF
@ManagedBean
@RequestScoped
public class NomesBean {
private String nome;
private List<String> nomes = new ArrayList<>();
public void adicionar() {
this.nomes.add(nome);
}
www.algaworks.com 62
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public List<String> getNomes() {
return nomes;
}
}
-$(*. 0( +7"$) #( (&,61!-&%F  A)$ )*1$ ) ./ -,0$1* ; 
$(+*-/:9* * )( .+ !--)5991&%',6#)6(+ 9#,9%-, * 0.*  /"
<ui:repeat>F  *(+*) )/ )- !0)$*) *(* 0( - + /$*-D *) 
' ( )/*  '$./ +.. +-  +-*+-$  values ; /-$0=*  0( 1-$71 ' *( *
)*(  B)$* ( varD- ) -$5)***)/ A*/"$/ -:9*F
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Lista de nomes</title>
</h:head>
<h:body>
<h:form>
Nome: <h:inputText value="#{nomesBean.nome}" />
<br/>
<h:commandButton value="Adicionar"
action="#{nomesBean.adicionar}" />
<br/>
<ol>
<ui:repeat var="nome" value="#{nomesBean.nomes}">
<li>#{nome}</li>
</ui:repeat>
</ol>
</h:form>
</h:body>
</html>
0)* 3 0/(*. .. 3 (+'*D *). "0$(*. $). -$- * +-$( $-* )*(
)*-('( )/ D (. * / )/- $). -$- * . "0)* )*( D * )*( )/ -$*- ,0 ./1
) '$./  .+-  F
..* **-- +*-,0 * .*+* ,0 0.(*. !*$  - ,0$.$:9*D *0
www.algaworks.com 63
. %D ,0)* $). -$(*. * . "0)* )*( D  +-$( $- - ,0$.$:9* %7 )9* 3$./ ($.D *
()"  );$)./)$*)*1( )/ + '*!-( 2*-&F
- (0- * .*+*  NomesBean +- /"0D ./ )*/-(*.  '.. *(
@ViewScopedF
@ManagedBean
@ViewScoped
public class NomesBean {
...
}
"*- *). "0$(*. $). -$- $1 -.*. )*( . ) '$./ ( . ,0<)$D (. .
- -- "-(*.  +7"$) *0 )1 "-(*. +- 0( *0/- +7"$) 1*'/-D  '$./ ./-7
15$D+*$.*.*.+ -()  ( ),0)/**0.07-$* ./$1 -)( .(+7"$)F
)*/:9* @SessionScoped  B) * .*+* . ..9*F
@ManagedBean
@SessionScoped
public class NomesBean {
...
}
 0.-(*. ./ .*+*D +* (*. $$*)- $1 -.*. )*( .D )1 "- ( *0/-.
+7"$). 1*'/- +-  '$./" (  )*( .D ,0 ' . $) ./-9* '7F  ()"   )
. -7 + -$* + ). ,0)* * / (+* '$($/  . ..9* !*- '):* *0 . * 0.07-$*
.*'$$/-$)1'$:9*. ..9*L" -'( )/ D/-1;. %( (.- *.$./ (MF
 .*+*  +'$:9* *(+-/$'#  $)./8)$ * ()"   ) *( /**. *.
0.07-$*.F
@ManagedBean
@ApplicationScoped
public class NomesBean {
...
}
 $$*)-(*. '"0). )*( .  ..-(*.  +7"$)  *0/-* )1 "*- *0 /;
( .(*  *0/-* *(+0/*-D *. ( .(*. )*( . +-  -9*D (*./-)* ,0
- '( )/  ./  .*+**(+-/$'#*.*.F
www.algaworks.com 64
TFRQF &$)" )
0)* 1*< $"$/ * ) - :*  +'$:9* )* +(0,+  .. 0( +7"$) *
.$./ (D * !-( 2*-&  '< +-* .. * -,0$1*   F .. -,0$1* *)/;( /". 
*(+*) )/ .D*(*!*-(0'7-$*.D(+*.  )/- / 3/*.D*/@ .  /F
 !*-)  0( *)%0)/*  '.. . ,0 - +- . )/( *. *(+*) )/ .F ... '.. .
.9* $)./)$.  *-* *( . /". $$*). ) +7"$)   *)./-* (
0( #$ --,0$  *(+*) )/ .D ,0 - +- . )/( *. ' ( )/*.  +7"$) . 0.
- '$*)( )/*.F
*- 3 (+'*D  '.. HtmlForm - +- . )/ * *(+*) )/  !*-(0'7-$*D
HtmlInputText - +- . )/ * *(+*) )/  )/-  / 3/* HtmlCommandButton *
*/9*F
0-)/ * +-* ..( )/*  +7"$)D 0( >$"*  ; " -* L- ) -$5*M
)1$* +- * )1 "*- * 0.07-$*F  *(+*) )/  +*..0$ 0( - ) -$5*-
,0 ;- .+*).71 '+*-" -->$"*  D- C /$)** ./* . 0*(+*) )/ F
( '"0(. *.$:@ .D . 0  ) +* +- $.- / -  ..* 6. $)./8)$. *.
*(+*) )/ .  +7"$)F ./  ..* 7 +*..$$'$  $).+ $*)- /; (*$B-
+-*+-$  . * *(+*) )/ ,0 ./7 . )* - ) -$5* +- * 0.07-$*F *- 3 (+'*D
0( *(+*) )/  )/-  / 3/* <h:inputText/>D - +- . )/* *(* *% /* 1
* /$+* HtmlInputTextD +* / -  +-*+-$  disabled (*$B ( / (+* 
3 0:9*+ '*>$"* 1D/-1;.* ..*$- /* ./ *% /*F
- !5 - ./ '$":9* )/- *. *(+*) )/ .  +7"$) +-*+-$  .   ).D
+- $.(*. -$- 0( $"' 'F (  )  ./ /$+* ; $"0' * ()"   )D
 A)$ $! - ): ; ,0 ' D ';(  !5 - '$":@ .  1'*- .D +* !5 - /(;(
'$":@ . *(+*) )/ .F
- 0(  ) . - -/ -$5* *(* 0( $"' 'D )* >$"*G!*)/  +7"$)
; ! $/ 0( (--:9* L"'"' M ( 0( /"  0( *(+*) )/  +- 0(
+-*+-$   0(()"  )F
* &$)"  ) NomesBeanD -$(*. *. /-$0/*. ,0 -   -9* $)./8)$. *.
*(+*) )/ .E
@ManagedBean
@ViewScoped
public class NomesBean {
www.algaworks.com 65
private String nome;
private List<String> nomes = new ArrayList<>();
private HtmlInputText inputNome;
private HtmlCommandButton botaoAdicionar;
public void adicionar() {
this.nomes.add(nome);
// desativa campo e bot‹o quando mais que 3 nomes
// forem adicionados
if (this.nomes.size() > 3) {
this.inputNome.setDisabled(true);
this.botaoAdicionar.setDisabled(true);
this.botaoAdicionar.setValue(
"Muitos nomes adicionados...");
}
}
// getters e setters
}
- *) /- *. *(+*) )/ . * !*-(0'7-$* *( . +-*+-$  . -$.D 0.(*. *
*/-$0/* binding ./".*.*(+*) )/ .F
Nome: <h:inputText value="#{nomesBean.nome}"
binding="#{nomesBean.inputNome}" />
<br/>
<h:commandButton value="Adicionar" action="#{nomesBean.adicionar}"
binding="#{nomesBean.botaoAdicionar}" />
* (*.  ..-  +7"$) )*-('( )/ $$*)- /; U )*( .F  +-/$- =D * (+*
*/9*.9* .$'$/*. */ 3/***/9*/(;(;(*$B*F
www.algaworks.com 66
+ .-  +* -*.*D ./ - 0-.*  1 . - 0.* *( ./)/ 0$*F  0.*
3 ..$1* +*  $3- * >$"*G!*)/ "-) $!=$'  )/ ) -F  ($*-$ .
1 5 .D *). "0$(*. !5 - * ,0 +- $.(*. 0.)* + ). 3+- ..@ .  '$":9* 
1'*-F
TFRRF $'* 1$
*< +*  . )1*'1 - 0( +'$:9* *(+' / (  . ( *)#  - /**. *.
 /'# .  ./ !-( 2*-&D +*-;( ,0)/* ($. 1*< .*0 - .*- ' D ( '#*- ($.
+-*0/$1* 1*< . /*-)-7F *- $..*D ./0- (*. "*- 0( ..0)/* ,0 ) ( /**. *.
 . )1*'1 *- . *)#  (E*$'* 1$F
* 3 0/- 0( +7"$) *)./-0= 0.)* *(+*) )/ . D ' +..-7 +*- 0( $'*
 1$ +-* ..( )/* ( B)$*D*)./$/0=*+*-W!. .E
RF  ./0--1$.9*
SF +'$-1'*- . - ,0$.$:9*
TF -* ..-1'$:@ .
UF /0'$5-*.1'*- .*(* '*
www.algaworks.com 67
VF
)1*-+'$:9*
WF  ) -$5-- .+*./
 ./0--1$.9*
 !.  - ./0-:9*  1$.9* - 0+ -  #$ --,0$  *(+*) )/ . +-  +7"$)
.*'$$/D . ' !*$ 3$$ )/ -$*-( )/ D *0 *)./->$ 0( )*1 #$ --,0$ 
*(+*) )/ .D. !*-+-$( $- 3$$:9*F
  +7"$) %7 /$1 - .$* 3$$D /**. *. *(+*) )/ . .9* - 0+ -*. ( . 0
./* )/ -$*-F
..* 7 *)$:@ . *. *.  0( !*-(0'7-$* .0( /$* * . -1$*-
. - ( - 0+ -*.D .* **-- '"0( +-*' (  1'$:9* *0 - ./-$:9*  - "-
 ) ">$*F *- 3 (+'*D . 0( !*-(0'7-$* .*'$$/ (+*. *-$"/>-$*. ,0 )9*
.9* /*/'( )/ +- )#$*.D +*-;( )1$*. + '* 0.07-$*D * ( .(* !*-(0'7-$*  1
+-  - )*1( )/ *( *. (+*. ,0 )9* ./1( 15$*. %7 +- )#$*.D +*-;(
*(( )." ).  --*$)$)**.(+*.- ,0 -$*.F
www.algaworks.com 68
+'$-1'*- . - ,0$.$:9*
 ./ !. D  *(+*) )/  #$ --,0$  *(+*) )/ . -$ ) !. )/ -$*-
/ (  #)  /0'$5- . 0 +->+-$* ./* *( $)!*-(:@ . ,0 1$ -( 
- ,0$.$:9*F
-* ..-1'$:@ .
. 1'*- . .0( /$*. .9* *)1 -/$*. ( /$+*. .+ =B*. ) 3*. *.
*(+*) )/ .F 0)* 1*< +-*"-( 0( +7"$) ( D 1*< +* $)'0$-
1'$*- . ,0 /0( )*. 1'*- . -  $*. + '*. 0.07-$*.F  ./ (*( )/*D *.
1'$*- . )/-( ( :9* D . .0-"$- ( --*.  *)1 -.9* *0  1'$:9*D  !.
 - ) -$5:9*  - .+*./ ; $)1* $( $/( )/ D +0')* /*. . *0/-. !. .
3$$)*  +7"$) /0' )*1( )/ D +- ,0 * 0.07-$* +*.. *--$"$- *. --*.
.0( / -*.*.($.0(1 5F
/0'$5-*.1'*- .*(* '*
0-)/ ./ !. D *. 1'*- . ) 3*. L*)# $*. *(* 1'*- . '*$.M *.
*(+*) )/ . .9* /0'$5*. )*. *% /*. * (* '*  *. *. 1'*- . '*$. .9*
'$(+*.F

)1*-+'$:9*
 ,0$)/ !. D *. 1 )/*. ,0 *-$"$)-( * )1$* * !*-(0'7-$* * . -1$*- .9*
3 0/*.F *- 3 (+'*D * '$- ( 0( */9* +- .0( / - 0( ./-*D 
+-*"-(:9*  :9*  ./ */9*  1 . - 3 0/F ( '"0). .*.D * (;/**
3 0/* +* - /*-)- 0( $ )/$B*- $5 )* ,0' ;  +->3$( +7"$)  . -
3$$D*0.$(+' .( )/ )9*- /*-)-)+- 3$$-( .(+7"$)F
 ) -$5-- .+*./
*- A'/$(*D  !.  - ) -$5:9*  - .+*./ " -  .= *( /**. *. *(+*) )/ .
)*. . 0. ./*. /0$. )1$ +- * '$ )/ F  $'* - *( : . (+- ,0 * 0.07-$*
$)/ -" *(+'$:9* 0(- ,0$.$:9*; )1$*. -1$*-F
www.algaworks.com 69
TFRSF -,0$1*! .G*)B"F3('
-*% /*. ,0 0.( 1 -1 -  . +* ( / - 0( -,0$1*  *)B"0-:9*D #(*
,7('3 61&%F ./ -,0$1* ; *+$*)'D (. . +- $.-  ' D -$ )* $- />-$* ,+9
&"'90))97
 *+-*% /*D*(**)/ A*(=)$(*$3*E
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
</faces-config>
 )/- $1 -.. *$.. ,0 +* (*. *)B"0-- ) .. -,0$1*D 0(  '. ; * - "$./-*
 ()"   ).D *(* '/ -)/$1 6. )*/:@ .F *- 3 (+'*D 1 % *(*
- "$./--=(*.*()"  ) NomesBean ) ./ -,0$1*F
<managed-bean>
<managed-bean-name>nomesBean</managed-bean-name>
<managed-bean-class>
com.algaworks.financeiro.controller.NomesBean
</managed-bean-class>
<managed-bean-scope>view</managed-bean-scope>
</managed-bean>
. 1 -.@ . )/ -$*- . *  SD )9* 3$./$( . )*/:@ . +- - "$./-- ()" 
 ).D +*- $..*D * -,0$1* ,7('3 61&% - *-$"/>-$*F  '$5( )/ D +- $.- (*.
 .. -,0$1* + ). +-  B)$- *0/-. *)B"0-:@ .  +'$:9*D ,0 1 - (*. ($.
$)/ F
TFRTF -,0$1*2 F3('
. +'$:@ . 2  ( 1 +* ( / - 0( -,0$1* .+ $'D #(* 061&%D ,0  1
B- ) +./ ,+9&"'90))97
 * +-*% /*F ./ -,0$1* /(;( ; #(*
 )%(2&'- ,+")-(+D +*$. '  .- 1 '"0(. *)B"0-:@ .  /'# . 
$(+')/:9**.- 0-.*.+'$:9*F
 -,0$1* 061&% )9* ; *-$"/>-$*D (. +* (*. -$7G'* '$)* *( * */9*
$- $/* ( )%(2&'- ,+")-(+ L )/-* * +-*% /*M  +*$. ( '+-
)%(2&'-,+")-(+-.F
www.algaworks.com 70
-,0$1*. -7-$**(0(*)/ A*. ( '#)/ *>$"*$3*E
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Financeiro</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
 -,0$1* " -*  .- 1 * )*(  +'$:9*D /-1;.  display-nameD '"0).
-,0$1*.  *. 1$).D /-1;.  welcome-file-listD ,0 .9* 0.*. .* * 0.07-$*
 .. *.$./ (+ ).+ '* ('-1-)-!D. ($)!*-(-*)*(  0(- 0-.*F
 )%(2&'- ,+")-(+  0( +'$:9* +*  .- 1 - /(;( . -1' /.D B'/-*.D
(+ ( )/*. *0/-.*)B"0-:@ .F
www.algaworks.com 71
- * !-( 2*-& * 1 -1 -  . !0)$*)-D ' - "$./- 0/*(/$( )/ 0(
. -1' / #( , +/%-F ./ . -1' / ; (+  +- * +-9*   9,9:D
+*-$..*D,0)* ..(*.0(+7"$)D .- 1 (*. 9,9(& "'61!-&%F
* (*..*- .- 1 -*- "$./-* ,+/%- . 0(+ ( )/*F
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
*( ./ )*1* (+ ( )/*  , +/%-D +* (*.  ..-  +7"$)  '$./" (
 )*( ./-1;. !--)599%(%!(,-5A<A<9"''"+(9(&,61!-&%F
(*. +-*1 $/- ,0 ./(*. $/)* * -,0$1* 061&% +- $)'0$-  *)B"0-:9*
$3*E
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
 +-8( /-*  *)/ 3/* $$*)* $( $5 ,0 ./(*. ( ($ )/ 
 . )1*'1$( )/*F 0)* *( / -(*. '"0( --*D  +7"$) +* 3$$- $)!*-(:@ .
/;)$.$$*)$.D+-)*.%0-- .*'1 -*+-*' (F
www.algaworks.com 72
+=/0'*U
1 ":9*
UFRF
)/-*0:9*6)1 ":9*
( D )1 ":9* ; 0( *)%0)/*  - "-. ,0  B)  +->3$( +7"$)  . - 3$$
,0)* 0( :9* ; 3 0/ + '* 0.07-$*F *- 3 (+'*D ,0)* 0( 0.07-$* '$ (
0( */9* +- . $).- 1 - ( 0( .$/ D ,0'  +->3$( +7"$) '  1 -7 1$.0'$5-I
 *. *. ./$1 - ( $)*(+' /*.D +-*11 '( )/  1 -7 1$.0'$5-  ( .( +7"$)D
*( . ( )." ).  --* +-*+-$.D +*-;( . /0* ./$1 - *-- /*  $).-$:9* !*-
! /0*(.0 ..*D ' +* -71 -0(+7"$) *.1$).*. -1$:*F
 )1 ":9* +* . - $(+'=$/ *0 3+'=$/F ./0- (*. *(* *)B"0-- *. *$.
/$+*.F
UFSF 1 ":9*$(+'=$/
0)* $)'0=(*. 0( <h:commandButton> *( 0( 1'*- ) +-*+-$  actionD
* ( )$.(*  /-/( )/*  )1 ":9* / )/-7 )*)/-- 0( +7"$)  ,0
0/*(/$( )/ F * 3 (+'* $3*D * ( )$.(*  )1 ":9* )*)/--7
)($)#-7- ,0$.$:9*+-+7"$) %61!-&%F
<h:commandButton value="Pr—xima p‡gina" action="Ola" />
 1'*- +..* +-  +-*+-$  action ; #(*  (.-(&D *0 - .0'/* 
:9*F
www.algaworks.com 73
1 ":9*$)8($
0)* 0( action / ( 0( 3+- ..9*  '$":9*  (;/**D * - /*-)* * (;/**
 1 . -* (.-(& )1 ":9*F
<h:commandButton value="Adicionar" action="#{nomesBean.adicionar}" />
0)* * (;/** adicionar - /*-)- nullD * 0.07-$* + -()  -7 ) ( .( +7"$)F
0)**- /*-)*!*-N'ND- ,0$.$:9*. -7 )($)#+-+7"$) %61!-&%F
public String adicionar() {
this.nomes.add(nome);
if (this.nomes.size() > 3) {
return "Ola";
}
return null;
}
 $- $*)( )/*
*- +-9*D 0( - ,0$.$:9* ; )($)# +-  +7"$) * (.-(&D *0 . %D 
- ,0$.$:9* ; A)$ * !-( 2*-& + ).  .+#  - ,0$.$:9* +- 0( )*1 +7"$)F
 !*- )  ..7-$* ,0 . % ! $/* * - $- $*)( )/*D +* (*. +..- * +-8( /-*
faces-redirect=true +-* (.-(&F
public String adicionar() {
this.nomes.add(nome);
if (this.nomes.size() > 3) {
return "Ola?faces-redirect=true";
}
return null;
}
UFTF 1 ":9* 3+'=$/
. - "-.  )1 ":9* 3+'=$/. .9*  '-. )* -,0$1* ,7('3 61&%F -
 '-- 0( - "- 3+'=$/D +- $.(*. $)!*-(- * ($)#*  +7"$)  *-$" (D 0(
(.-(& *(0()*( ,0',0 - *($)#*+7"$)  ./$)*F
www.algaworks.com 74
<navigation-rule>
<from-view-id>/Nomes.xhtml</from-view-id>
<navigation-case>
<from-outcome>oi</from-outcome>
<to-view-id>/Ola.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
 - "- ,0 (*.   '--  B) ,0  :9*  )*( N*$N  +-/$-  *-$" (
9(&,61!-&%  1 )($)#- +-  +7"$) 9%61!-&%F - 0.- ./ - "-D
+* (*. .$(+' .( )/ $$*)- 0( */9* ) +7"$) (&,61!-&% *( 
+-*+-$  action - ! - )$)** (.-(&F
<h:commandButton value="Pr—xima p‡gina" action="oi" />
$'-
* (*. 0.- 0"%+ )* ' ( )/* from-view-id +- . ' $*)- $1 -.*. -,0$1*.
 *-$" (D*(*+*- 3 (+'* * *0 /admin/*F
<navigation-rule>
<from-view-id>*</from-view-id>
<navigation-case>
<from-outcome>oi</from-outcome>
<to-view-id>/Ola.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
 $- $*)( )/*
*- +-9*D  - ,0$.$:9* . -7 )($)# +-  +7"$)   ./$)*F  +- $.-(*.
!5 -*- $- $*)( )/*D./$)'0$-* ' ( )/* <redirect/>F
<navigation-rule>
<from-view-id>*</from-view-id>
<navigation-case>
<from-outcome>oi</from-outcome>
<to-view-id>/Ola.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>
www.algaworks.com 75
+=/0'*V
*(+*) )/ . $)/ -!
VFRF $'$*/ .
-  . )1*'1 - 0( .$./ ( *(+' /* ( 1 -1 -  .D 1*<  1 *)#  - + '*
( )*. *. +-$)$+$. *(+*) )/ . 7.$*.F 0)* !'(*. ( *(+*) )/ . 7.$*.D
,0 - (*.$5 -*.*(+*) )/ .+-@ . .+ $B:9*/ )*'*"$F
.$'$*/ . /"..9*E
K (+E 3$./ +- - .0+*-/ 6. *0/-. $'$*/ .D )9* +*..0$
*(+*) )/ . 1$.0$.F +- ) - (*. ./ $'$*/  )*  *-- - * '$1-*D
. (+- ,0 !*-)  ..7-$*
K  E +*..0$ *(+*) )/ . ,0 " -( *)/ A* 1$.0'D *(* !*-(0'7-$*.D
(+*.  )/-D ->/0'*.  .=  / 3/*.D */@ .D '$)&.D . ' :@ .D
+$);$.D/ ' *.D( )." ).  /F
K %-,E;$'$*/ +--$:9* / (+'/ . +7"$).F
K (&)(,"-E0.+--$-*(+*) )/ .0./*($5*.F
 ./ +=/0'*D!*- (*.)*.*(+*) )/ .1$.0$.D$'$*/   F
- 0.- . $'$*/ .D / (*. ,0 $(+*-/7G'. /-1;. *. '&,),
*-- .+*) )/ .F
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
www.algaworks.com 76
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:composite="http://xmlns.jcp.org/jsf/composite">

(+*-/(*. . $'$*/ .  /". )*( (*. *( *. +- B3*. fD hD ui compositeF


.+- B3*.+* (. -(*$B*.D(.*.,0 0.(*..9**.*)1 )$*)$.F
VFSF  :'#* *-+*+7"$)
. *(+*) )/ . <h:head> <h:body> - ) -$5( . /".  <head> <body>D
- .+ /$1( )/ F
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
</h:head>
<h:body>
</h:body>
</html>
 $(+*-/8)$  ./ . *(+*) )/ . )9* ; + ).  - ) -$5:9* . /". (  D
+*$. . -$ (0$/* .$(+' . .- 1 - . /".  $- /( )/ )* >$"*  +7"$)F
 /+  - ) -$5:9*  - .+*./D *  +* $)'0$- - 0-.*. )  ..7-$*.
$)($( )/ D*(*+*- 3 (+'*- ! -<)$-,0$1*. 1-$+/ F
VFTF *-(0'7-$*.
1 -1 -  . +*..0$ 0( *(+*) )/ +- - ) -$5- !*-(0'7-$*.  D #(*
<h:form>F
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Form</title>
</h:head>
<h:body>
<h:form>
</h:form>
</h:body>
www.algaworks.com 77
</html>
 /" <h:form> " -*0 0( ' ( )/* <form>   F + .-  /" <form>  
+*..0$- . +-*+-$  . method actionD * *(+*) )/ *  )9* +*..0$D +*$.
. (+- ; *).$ -* * (;/** )(,-  :9* $"0' * ) - :*  ( .( +7"$)D
*(*1*<+* 1 -)*>$"*" -*F
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head id="j_idt2">
<title>Form</title></head><body>
<form id="j_idt5" name="j_idt5" method="post"
action="/Financeiro/Form.xhtml"
enctype="application/x-www-form-urlencoded">
<input type="hidden" name="j_idt5" value="j_idt5" />
<input type="hidden" name="javax.faces.ViewState"
id="j_id1:javax.faces.ViewState:0"
value="-1136606116435564932:-1470026908624937807"
autocomplete="off" />
</form></body>
</html>
VFUF -*+-$  .*(0).
. /". *. *(+*) )/ . +*..0 ( +-*+-$  . ,0 $5 ( *(* ' .  1 (
!0)$*)-F . +-*+-$  . +* ( . - 7.$.D ,0)* .9* *(+-/$'#. + '
($*-$ . /".D  D /(;( *)# $* *(* ),,7-!+(. !  D ,0)*
- +- . )/( *. ( .(*. /-$0/*. *. ' ( )/*.  D 1 )/*.   D ,0)*
9*.0+*-/ .-$+/.  1 )/*.D*(**'$-D*+..-*(*0. +*-$(D /F
+-*+-$  $
 +-*+-$  id ./7 +- . )/ ( ,0. /**. *. *(+*) )/ .F ' )*. + -($/
$ )/$B- *. *(+*) )/ .  +7"$) +- - ! -<)$ +*./ -$*-D /-1;.  '.. . 1
*0 *0/-*. *(+*) )/ . D ';(  +* -  ..- *. ' ( )/*.   /-1;. 
.-$+/.F
- 3 (+'$B-D -$- (*. 0( +7"$) *( 0( (+*  )/-  / 3/* 0(
*/9*F + ). +- .$(+'$B-D * */9* )9* . -7 -$* 0.)* 0( *(+*) )/ F
*'$-)**/9*D0(>$"* 1-$+/$-7'/ --**)/ A**(+*  )/-F
www.algaworks.com 78
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Propriedades comuns</title>
<script>
function alterarValorCampo() {
var campo = document.getElementById('meuForm:meuCampo');
campo.value = 'Valor alterado';
}
</script>
</h:head>
<h:body>
<h:form id="meuForm">
<h:inputText id="meuCampo" />
<input type="button" value="Alterar valor"
onclick="alterarValorCampo();"/>
</h:form>
</h:body>
</html>
 % ,0 -$(*. 0( !0):9* 1-$+/ #( alterarValorCampo()F ./ !0):9*
; #( )* 1 )/* onclick * */9* N'/ -- 1'*-ND  1 '/ -- * 1'*- * (+*
+- N'*- '/ -*NF  (+* ,0 $)'0=(*. ; - ) -$5* (   . "0$)/
!*-(E
<input id="meuForm:meuCampo" type="text" name="meuForm:meuCampo" />
 % ,0 * $ * ' ( )/* input !*$ *)B"0-* +- meuForm:meuCampoF  $ *
!*-(0'7-$* 0.* +-  B)$- * $ * ' ( )/*  * (+*  )/-F *( ./
$ )/$B*-D0.(*.. "0$)/ +-*"-(:9* 1-$+/+- ..7G'*E
var campo = document.getElementById('meuForm:meuCampo');
+-*+-$  $)$)"
 +-*+-$  binding +* . - .+ $B *( 0( 3+- ..9* '$":9* ,0
- ! - )$ 0( +-*+-$  *  ) * /$+* * *(+*) )/ F 7 0.(*. ./ /-$0/*
,0)*!'(*..*- $"' ',F
www.algaworks.com 79
+-*+-$  - ) - 
 +-*+-$  rendered /(;( ./7 +- . )/ ) ($*-$ . /".F ' *)/-*' 
- ) -$5:9* * *(+*) )/ F  * 1'*- *0 * - .0'/*  3+- ..9* !*- falseD *
*(+*) )/ )9*. -7- ) -$5*F
<h:inputText rendered="false" />
<h:inputText rendered="#{meuBean.usuarioAtivo}" />
.+-*+-$  ../4'  ./4' '..
 +*..=1 ' 0/$'$5- ./$'*.  L, -2% !-M ( *(+*) )/ .  (** "'%"'
*00.)*'.. .F
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Estilos</title>
<style>
.campo {
background-color: #ccc;
color: white
}
</style>
</h:head>
<h:body>
<h:form>
<h:inputText styleClass="campo"
style="border-color: blue; border-style: dotted" />
</h:form>
</h:body>
</html>
'- (*. .*- $(+*-/:9*  -,0$1*.  ($. $)/ F  .. 3 (+'*D  B)$(*.
0('.. )*+->+-$*-,0$1*+7"$)F
<style>
.campo {
background-color: #ccc;
color: white
}
</style>
www.algaworks.com 80
 '..  N(+*N *)B"0-  *-  !0)* +- $)5  *-  !*)/ +- -)*F
 (+* ,0 $$*)(*. - ! - )$  '..  /-1;.  +-*+-$  styleClassD
';( $..*D $$*) )*1*. ./$'*. "'%"' ) +-*+-$  styleD *)B"0-)* 0(
*-  ./$'* *-F
<h:inputText styleClass="campo"
style="border-color: blue; border-style: dotted" />
* 3 0/-  +7"$)D +* (*. 1 - ,0 . -/ -=./$. * (+* !*-(
(*$B.F
 ($*-$ *. *(+*) )/ .  $'$*/   +*..0 ( . +-*+-$  . style
styleClassF
.+-*+-$  . 
. +-*+-$  .   D /(;( *)# $*. *(* ),,7-!+(. !  D
- +- . )/( 3/( )/ *. /-$0/*.  ' ( )/*.  +->+-$  F 9* 1(*.
'$./-/**. ' .,0$D(.+ ).0.-'"0).*(* 3 (+'*F
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Propriedades HTML</title>
</h:head>
<h:body>
<h:form>
<h:inputText size="40" maxlength="50"
title="Informe seu nome" />
</h:form>
</h:body>
</html>
 %*(* ./ *(+*) )/ ;- ) -$5*E
www.algaworks.com 81
<input type="text" name="j_idt5:j_idt6" maxlength="50"
size="40" title="Informe seu nome" />
. +-*+-$  . maxlengthD size title .$(+' .( )/ !*-( - +... +- *
>$"* .=D+*-$..*.9*#(. ),,7-!+(. !  F
.+-*+-$  .  1 )/*. 
. +-*+-$  . ,0 .0+*-/( .-$+/.D ,0 .9* 3 0/*. ( '"0( 1 )/* *
0.07-$*D .9* #(.  +-*+-$  .  1 )/*.   F 0. /*. . /". 
$'$*/   +*..0 ( ...+-*+-$  .F
* (+*  )/- * 3 (+'* $3*D 0.(*. '"0(. +-*+-$  . +- $)'0$-
>$"*. 1-$+/,0 /-/( 1 )/*.*0.07-$*F
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Propriedades DHTML</title>
</h:head>
<h:body>
<h:form>
<h:inputText onclick="this.value = '';"
onchange="this.value = this.value.toUpperCase();"
onmouseover="this.style.backgroundColor = 'yellow';"
onmouseout="this.style.backgroundColor = 'white';"/>
</h:form>
</h:body>
</html>
. >$"*. 1-$+/ . -9* - ) -$5*. ) .= +- * )1 "*-F 0)* * 0.07-$*
'$- .*- * (+*D . 0 1'*- . -7 '/ -* +- 15$*D * '/ -- * 1'*- * (+*D
' . -7 (*$B* +- ' /-. ($A.0'.D * +..- * 0-.*- * (*0. +*- $( *
(+*D  *-  !0)* . -7 '/ - * - /$-- * 0-.*- * (*0.  $( * (+*D
*- !0)*B-7-)F
VFVF )/- / 3/*.
3$./ ( /-<. /$+*.  *(+*) )/ . ,0 - ) -$5( (+*.  )/-  / 3/*E
<h:inputText>D <h:inputSecret> <h:inputTextarea>F
www.algaworks.com 82
*(+*) )/ \#E$)+0/ 3/]
 *(+*) )/ <h:inputText> - ) -$5 0( (+*  )/-  / 3/* .$(+' .D
- +- . )/* + '* ' ( )/* input * /$+* N/ 3/N   F 7 0.(*. ./
*(+*) )/ )/ -$*-( )/ F * 3 (+'*  . "0$-D * (+*  )/- ./7 '$"* 6
+-*+-$  nome *()"  ) meuBeanF
<h:inputText value="#{meuBean.nome}"/>
*(+*) )/ \#E$)+0/ - /]
 *(+*) )/ <h:inputSecret> - ) -$5 0( (+*  )/-  . )#D
- +- . )/* + '* ' ( )/* input * /$+* N+..2*-N   F  / 3/* $"$/* ;
+- . )/* !*-(. - /F
<h:inputSecret value="#{loginBean.senha}" />
*(+*) )/ \#E$)+0/ 3/- ]
 *(+*) )/ <h:inputTextarea> - ) -$5 0( (+*  )/-  / 3/*.
($*- .D ,0 +* ( / - 17-$. *'0). '$)#.D ; - +- . )/* + '* ' ( )/*
textarea )  F . /-$0/*. cols rows  B) ( * /()#*  *'0). '$)#.
 7-  * / 3/*D - .+ /$1( )/ F  >$"* $3* -$ 0( 7-   / 3/* *( T '$)#.
UQ*'0).E
<h:inputTextarea cols="40" rows="3"
value="#{cadastroUsuarioBean.resumoCurriculo}"/>
VFWF = / 3/*.
3$./ ( /-<. /$+*.  *(+*) )/ . ,0 - ) -$5( .=.  / 3/*.E <h:outputText>D
<h:outputFormat> <h:outputLabel>F
*(+*) )/ \#E*0/+0/ 3/]
*(+*) )/ <h:outputText> - ) -$5/ 3/*..$(+' .)+7"$)F
www.algaworks.com 83
<h:outputText value="Bem-vindo "/>
<h:outputText value="#{segurancaBean.nomeUsuarioLogado}"
style="font-weight: bold" />
 3 (+'* $( - ) -$5 * /- #*   . "0$- L*).$ -)* ,0 *  )
segurancaBean 3$./MF
Bem-vindo <span style="font-weight: bold">Thiago</span>
 % ,0 * / 3/* N (G1$)*N )9* ./7 )1*'1$* +*- ) )#0( ' ( )/*   F
0)* 0.(*. '"0( /-$0/* ,0  1 . - - C /$* )* >$"*  D *(* * id *0
styleD*/ 3/*;" -* )/-*/" <span>D*(**)/  0*(N#$"*NF
*(+*) )/ \#E*0/+0/*-(/]
 *(+*) )/ <h:outputFormat> - ) -$5 / 3/*. +-( /-$5*. ) +7"$)F .
/ 3/*. +-( /-$5*. .9* *(+*./*. +*- .+:*. - . -1*. L)%!(%+,MD ,0 .9*
.0./$/0=*.+*-1'*- .)*(*( )/*- ) -$5:9*F
. 1'*- . +-( /-$5*. .9*  B)$*. *( )A( -*. )/- #1 .D $)$$)*  +-/$-
* )A( -* 5 -*F .  B)$:@ . *. 1'*- . .9* ! $/. /-1;.  /" <f:param> 
$'$*/  (+F
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>OutputFormat</title>
</h:head>
<h:body>
<h:form>
<h:outputFormat
value="Oi {0}! Existem {1} tarefas pendentes.">
<f:param value="#{tarefaBean.nomeUsuario}" />
<f:param value="#{tarefaBean.qtdeTarefasPendentes}" />
</h:outputFormat>
</h:form>
</h:body>
</html>
www.algaworks.com 84
*(+*) )/ \#E*0/+0/  ']
 *(+*) )/ <h:outputLabel> - ) -$5 * ' ( )/*   F . *(+*) )/ .
 ./ /$+* .9* 1$)0'*. *( *0/-*. /-1;. * /-$0/* forF  0.*  ./ *(+*) )/
;%0./$B*+--*/0'-(+*. . 0.!*-(0'7-$*.F
<h:form id="frm">
<h:outputLabel value="Nome:" for="nomeInput" />
<h:inputText id="nomeInput" />
</h:form>
>$"*$(- ) -$5. "0$)/ .= (  E
<label for="frm:nomeInput">Nome:</label>
<input id="frm:nomeInput" type="text" name="frm:nomeInput" />
VFXF
(" ).
 *(+*) )/ <h:graphicImage> - ) -$5 * ' ( )/* <img>   D ,0 3$
0( $(" ( ) +7"$)F  ) - :*  $(" (  1 . - $)!*-(* )* /-$0/* value
*0 urlD +*$. 0( ; /'#* +- * *0/-*F ./ *(+*) )/ + -($/ 1*< 0.- * ($)#*
- '/$1* * *)/ 3/*D *0 . %D 1*< )9* +- $. $)!*-(- * ($)#* * *)/ 3/* 
+'$:9*D+*$.;*'**0/*(/$( )/ F
<h:graphicImage value="/imagens/logo.png" width="200" height="36"/>
>$"*$(- ) -$5. "0$)/ .= (  E
<img src="/Financeiro/imagens/logo.png" height="36" width="200" />
-,0$1* %( (6)'  1 . -*'** ( ,+9&"'90))9"& ',F
$'$*/  - 0-.*.
 +-/$- *  SD /**. *. - 0-.*. 2 D *(* $(" ).D  1-$+/D +* ( . -
*'**.)*$- />-$* ,+9&"'90))9+,(.+,F
( .0$- />-$*  )/-*  +,(.+, ; *).$ -* *(* 0( $'$*/   - 0-.*.
* +-*% /*D 1*< +* - ! - )$- ,0',0 - 0( *. - 0-.*. + '*. /-$0/*. library
name  '"0).*(+*) )/ .F
www.algaworks.com 85
<h:graphicImage library="algaworks" name="logo.png"
width="200" height="36"/>
./(*. - ! - )$)* * - 0-.* %( (6)'  $'$*/  % 0(+$,F  ($)#* *
-,0$1*)*+-*% /*; ,+9&"'90))9+,(.+,9% 0(+$,9%( (6)' F
 %.= ( 
<img src="/Financeiro/javax.faces.resource/logo.png.xhtml?ln=algaworks"
height="36" width="200" />
VFYF  )0. $3. '$./" (
3$./ ( U *(+*) )/ . ,0 - ) -$5( ' ( )/*.  ( )0. $3.  '$./" (E
<h:selectOneMenu>D <h:selectManyMenu>D <h:selectOneListbox>
<h:selectManyListbox>F
*(+*) )/ \#E. ' /)  )0]
 *(+*) )/ <h:selectOneMenu> - ) -$5 0( ( )0  . ' :9* A)$D
- +- . )/* + '* ' ( )/*   D *( * /-$0/* size $"0'  R . ( * /-$0/*
multipleD,0 + -($/$-$. ' :@ .(A'/$+'.F
- $.(*. +..- 0( - ':9*  $/ ). ,0 +-  -9* ) '$./D 0.)*  /"
<f:selectItem>F
<h:outputLabel value="Time de futebol favorito: " for="timeFutebol" />
<h:selectOneMenu value="#{cadastroTorcedorBean.timeFavorito}"
id="timeFutebol">
<f:selectItem itemValue="Corinthians" />
<f:selectItem itemValue="Flamengo" />
<f:selectItem itemValue="Palmeiras" />
<f:selectItem itemValue="Santos" />
<f:selectItem itemValue="S‹o Paulo" />
<f:selectItem itemValue="Vasco" />
<f:selectItem itemValue="Outro" />
</h:selectOneMenu>
>$"*$(- ) -$5. "0$)/ .= (  E
<label for="frm:timeFutebol">Time de futebol favorito: </label>
<select id="frm:timeFutebol" name="frm:timeFutebol" size="1">
<option value="Corinthians">Corinthians</option>
<option value="Flamengo">Flamengo</option>
<option value="Palmeiras">Palmeiras</option>
www.algaworks.com 86
<option value="Santos">Santos</option>
<option value="S‹o Paulo">S‹o Paulo</option>
<option value="Vasco">Vasco</option>
<option value="Outro">Outro</option>
</select>
 1'*- .+ $B* )* /-$0/* itemValue  /" <f:selectItem> * $/ (
. ' $*)* ; +..* +-  +-*+-$  * ()"   ) 0. ) '$":9* * 1'*-
* *(+*) )/ D +*- $..*D +- $.(*. -$- 0( /-$0/* *( . 0 --+ ,--+ )*  )
CadastroTorcedorBeanF
private String timeFavorito;
public String getTimeFavorito() {
return timeFavorito;
}
public void setTimeFavorito(String timeFavorito) {
this.timeFavorito = timeFavorito;
}
- .0'/*)*-*2. -;*. "0$)/ E
. 1'*- . .+ $B*. ) /" <f:selectItem> .9* 0.*. /(;( *(* ->/0'*. *.
$/ ). * ( )0F '"0(. 1 5 . / (*.  )  ..$  / - *. 1'*- . $! - )/ . *.
->/0'*.D +*-$..*/" <f:selectItem> +*..0$+-*+-$  itemLabelF
<h:outputLabel value="Time de futebol favorito: " for="timeFutebol" />
<h:selectOneMenu value="#{cadastroTorcedorBean.timeFavorito}"
id="timeFutebol">
<f:selectItem itemValue="Corinthians" itemLabel="Tim‹o" />
<f:selectItem itemValue="Flamengo" itemLabel="Meng‹o" />
<f:selectItem itemValue="Palmeiras" />
<f:selectItem itemValue="Santos" />
<f:selectItem itemValue="S‹o Paulo" />
<f:selectItem itemValue="Vasco" itemLabel="Vasc‹o" />
<f:selectItem itemValue="Outro" />
</h:selectOneMenu>
 %*>$"*  " -*E
www.algaworks.com 87
<label for="frm:timeFutebol">Time de futebol favorito: </label>
<select id="frm:timeFutebol" name="frm:timeFutebol" size="1">
<option value="Corinthians">Tim‹o</option>
<option value="Flamengo">Meng‹o</option>
<option value="Palmeiras">Palmeiras</option>
<option value="Santos">Santos</option>
<option value="S‹o Paulo">S‹o Paulo</option>
<option value="Vasco">Vasc‹o</option>
<option value="Outro">Outro</option>
</select>
- .0'/*. -7E
*(+*) )/ \#E. ' /)4 )0]
 *(+*) )/ <h:selectManyMenu> - ) -$5 0( ( )0  . ' :9* (A'/$+'D
- +- . )/* + '* ' ( )/*   D *( * /-$0/* size $"0'  R multiple $"0'
 multipleF
<h:outputLabel value="Times de futebol favoritos: " for="timeFutebol" />
<h:selectManyMenu value="#{cadastroTorcedorBean.timesFavoritos}"
id="timeFutebol">
<f:selectItem itemValue="Corinthians" />
<f:selectItem itemValue="Flamengo" />
<f:selectItem itemValue="Palmeiras" />
<f:selectItem itemValue="Santos" />
<f:selectItem itemValue="S‹o Paulo" />
<f:selectItem itemValue="Vasco" />
<f:selectItem itemValue="Outro" />
</h:selectManyMenu>
>$"*$(- ) -$5. "0$)/ .= (  E
<label for="frm:timeFutebol">Times de futebol favoritos: </label>
<select id="frm:timeFutebol" name="frm:timeFutebol" multiple="multiple"
size="1">
<option value="Corinthians">Corinthians</option>
<option value="Flamengo">Flamengo</option>
<option value="Palmeiras">Palmeiras</option>
<option value="Santos">Santos</option>
www.algaworks.com 88
<option value="S‹o Paulo">S‹o Paulo</option>
<option value="Vasco">Vasco</option>
<option value="Outro">Outro</option>
</select>
*(* ./ *(+*) )/ +*..$$'$/  . ' :9*  ($.  0( $/ (  . ' :9*D *
()"   )  1 +*..0$- 0( +-*+-$  * /$+*  --4 +- *(+*-/- *.
' ( )/*. . ' $*)*.F * .*  ./ 3 (+'*D +- $.(*. -$- 0( +-*+-$  *
/$+* String[] *(*)*( timesFavoritosF
private String[] timesFavoritos;
public String[] getTimesFavoritos() {
return timesFavoritos;
}
public void setTimesFavoritos(String[] timesFavoritos) {
this.timesFavoritos = timesFavoritos;
}
*(+*) )/ \#E. ' /) $./*3]
./ *(+*) )/ - ) -$5 0( ' ( )/*   ,0',0 - /()#* L .+ $B*M
. (*/-$0/* multipleD*0. %D;+*..=1 '. ' $*)-+ ).0($/ (. ' :9*F
<h:outputLabel value="Time de futebol favorito: " for="timeFutebol" />
<h:selectOneListbox value="#{cadastroTorcedorBean.timeFavorito}"
id="timeFutebol" size="4">
<f:selectItem itemValue="Corinthians" />
<f:selectItem itemValue="Flamengo" />
<f:selectItem itemValue="Palmeiras" />
<f:selectItem itemValue="Santos" />
<f:selectItem itemValue="S‹o Paulo" />
<f:selectItem itemValue="Vasco" />
<f:selectItem itemValue="Outro" />
</h:selectOneListbox>
*(+*) )/ \#E. ' /)4 $./*3]
./ *(+*) )/ - ) -$5 0( ' ( )/*   ,0',0 - /()#* L .+ $B*M
*(*/-$0/* multipleD*0. %D;+*..=1 '. ' $*)-17-$*.$/ ).. ' :9*F
www.algaworks.com 89
<h:outputLabel value="Times de futebol favoritos: " for="timeFutebol" />
<h:selectManyListbox value="#{cadastroTorcedorBean.timesFavoritos}"
id="timeFutebol" size="4">
<f:selectItem itemValue="Corinthians" />
<f:selectItem itemValue="Flamengo" />
<f:selectItem itemValue="Palmeiras" />
<f:selectItem itemValue="Santos" />
<f:selectItem itemValue="S‹o Paulo" />
<f:selectItem itemValue="Vasco" />
<f:selectItem itemValue="Outro" />
</h:selectManyListbox>
0.07-$*+* . ' $*)-) )#0(D0(*017-$*.$/ ).. ' :9*F
VFZF (+*. # " ( */@ .-7$*
3$./ ( T *(+*) )/ . ,0 - ) -$5( ' ( )/*.  (+*.  # " ( */@ .
-7$*E <h:selectOneRadio>D <h:selectBooleanCheckbox>
<h:selectManyCheckbox>F
*(+*) )/ <h:selectOneRadio>
./ *(+*) )/ - ) -$5 0( ' ( )/*  <input> * /$+* radioD 0.*
,0)* 1*<  . % 3$$- 0( '$./  *+:@ . ,0 +* ( . - . ' $*).
0)$( )/ F
<h:selectOneRadio id="sexo">
<f:selectItem itemValue="M" itemLabel="Masculino" />
<f:selectItem itemValue="F" itemLabel="Feminino" />
</h:selectOneRadio>
 +*..=1 ' '/ -- * '4*0/ )* ,0' *. $/ ). .9* +- . )/*.D .+ $B)* 
+-*+-$  layoutF . 1'*- . +*..=1 $. +- ./ /-$0/* .9* pageDirection
lineDirection L+-9*MF
www.algaworks.com 90
<h:selectOneRadio id="sexo" layout="pageDirection">
<f:selectItem itemValue="M" itemLabel="Masculino" />
<f:selectItem itemValue="F" itemLabel="Feminino" />
</h:selectOneRadio>
'4*0/ pageDirection 3$ *.$/ ).)1 -/$'F
*(+*) )/ \#E. ' /**' )# &*3]
 *(+*) )/ <h:selectBooleanCheckbox> - ) -$5 * ' ( )/*   <input>
*/$+* checkboxF.  ./ *(+*) )/ +- B)$-/-$0/*.**' )*.F
<h:selectBooleanCheckbox id="aceite"
value="#{cadastroUsuarioBean.termoAceito}"/>
<h:outputLabel value="Li e aceito os termos e condi›es" for="aceite"/>
* ()"   )D +- $.(*. -$- 0( +-*+-$  **' ) +- -   -  *+:9*
* 0.07-$*F  * (+* !*- . ' $*)*D  +-*+-$  -   -7 * 1'*- trueD .*
*)/-7-$*D-   -7*1'*- falseF
private boolean termoAceito;
public boolean isTermoAceito() {
return termoAceito;
}
public void setTermoAceito(boolean termoAceito) {
this.termoAceito = termoAceito;
}
*(+*) )/ \#E. ' /)4# &*3]
./ *(+*) )/ - ) -$5 0( '$./  (+*.  # " (F  0.07-$* +*
. ' $*)-) )#0(D0(*017-$*.$/ ).F
<h:selectManyCheckbox id="assuntos"
value="#{cadastroUsuarioBean.assuntosInteresse}">
<f:selectItem itemValue="1" itemLabel="Java" />
<f:selectItem itemValue="3" itemLabel="Python" />
www.algaworks.com 91
<f:selectItem itemValue="4" itemLabel="Ruby" />
<f:selectItem itemValue="4" itemLabel="PHP" />
<f:selectItem itemValue="5" itemLabel="MŽtodos ‡geis" />
</h:selectManyCheckbox>
*(*  B)$(*. *. 1'*- . *. $/ ).  . ' :9* *(* )0(;-$*. * 0.07-$* +*
. ' $*)- (A'/$+'*. ' ( )/*.  '$./D -$(*. 0( /-$0/* )* ()"   ) *
/$+* Integer[]F
private Integer[] assuntosInteresse;
public Integer[] getAssuntosInteresse() {
return assuntosInteresse;
}
public void setAssuntosInteresse(Integer[] assuntosInteresse) {
this.assuntosInteresse = assuntosInteresse;
}
 +*..=1 ' '/ -- * '4*0/ +- +- . )/:9* *. $/ ).F . 1'*- . +*..=1 $. +-
./ /-$0/* .9* pageDirection lineDirection L+-9*MF  % 0( 3 (+'* *( *
'4*0/ pageDirectionF
<h:selectManyCheckbox id="assuntos" layout="pageDirection"
value="#{cadastroUsuarioBean.assuntosInteresse}">
<f:selectItem itemValue="1" itemLabel="Java" />
<f:selectItem itemValue="3" itemLabel="Python" />
<f:selectItem itemValue="4" itemLabel="Ruby" />
<f:selectItem itemValue="4" itemLabel="PHP" />
<f:selectItem itemValue="5" itemLabel="MŽtodos ‡geis" />
</h:selectManyCheckbox>
'4*0/ pageDirection 3$ *.$/ ).)1 -/$'F
www.algaworks.com 92
VFRQF
/ ). . ' :9*
3$./ ( .$/0:@ . ,0 +- $.(*. ,0 0( '$./  $/ ). . % */$ $)($( )/ D
+- $.+*)$$'$5- * 0.07-$*  . ' :9* ( ,0',0 - 0( *. *(+*) )/ . ,0 1$(*.
). A'/$( . :@ .F ./ '$./ +* 1$-  0( )*  *.D  0( -,0$1* *0
 ,0',0 - *0/- *-$" (F 0)* 3$./ .. )  ..$ D +- $.(*.  /"
<f:selectItems> L)*+'0-'MF
 ./ 3 (+'*D -$- (*. 0( '$./  *+:@ . *( )*( .  $ .F -$( $-( )/ D
1(*.-$-)*..*()"  )F
@ManagedBean
@ViewScoped
public class MinhaCidadeBean {
private String cidadeNatal;
private List<String> todasCidades;
public MinhaCidadeBean() {
this.todasCidades = new ArrayList<>();
this.todasCidades.add("Uberl‰ndia-MG");
this.todasCidades.add("Uberaba-MG");
this.todasCidades.add("Belo Horizonte-MG");
this.todasCidades.add("S‹o Paulo-SP");
this.todasCidades.add("Campinas-SP");
this.todasCidades.add("S‹o JosŽ dos Campos-SP");
this.todasCidades.add("Rio de Janeiro-RJ");
this.todasCidades.add("Goi‰nia-GO");
this.todasCidades.add("Fortaleza-CE");
this.todasCidades.add("Porto Velho-RO");
}
public String getCidadeNatal() {
return cidadeNatal;
}
public void setCidadeNatal(String cidadeNatal) {
this.cidadeNatal = cidadeNatal;
}
public List<String> getTodasCidades() {
return todasCidades;
}
}
 +7"$) D $)'0=(*. * *(+*) )/ <h:selectOneListbox> com uma lista de
sele‹o.
www.algaworks.com 93
<h:selectOneListbox size="5" value="#{minhaCidadeBean.cidadeNatal}">
<f:selectItems value="#{minhaCidadeBean.todasCidades}" />
</h:selectOneListbox>
VFRRF */@ . '$)&.
+- ) - (*.  $)'0$- */@ . '$)&. *( *. *(+*) )/ . <h:commandButton>D
<h:commandLink> <h:outputLink>F
*(+*) )/ \#E*(()0//*)]
7 0.(*. ./ *(+*) )/ ( 3 (+'*. )/ -$*- .F ./ *(+*) )/ " - 0(
' ( )/* <input>   * /$+* submitD image *0 resetF  /-$0/* type +* . -
.+ $B* *( submit L+-9*M *0 resetF - */@ . - +- . )/*. +*- $(" ).D
./ $)!*-(- * ($)#* )* /-$0/* imageD ,0 * /$+* * ' ( )/* ; '/ -*
0/*(/$( )/ +- imageF
*. */@ .  *()*D  +-*+-$  action +* . - .+ $B *( 3+- ..@ .
 '$":9* (;/**.*0 (.-(&  )1 ":9*F
<h:commandButton id="cadastrar" value="Cadastrar" type="submit"
action="#{cadastroBean.cadastrar}"/>
<h:commandButton id="limpar" value="Limpar" type="reset"/>
<h:commandButton id="voltar" value="Voltar"
image="/imagens/voltar.png" action="home"/>
*(+*) )/ \#E*(() $)&]
 *(+*) )/ <h:commandLink> " - 0( 8)*- (  L'$)&M ,0 !0)$*)
*(*0(*/9* .0($..9* !*-(0'7-$*F %'"0). 3 (+'*. 0.*F
<h:commandLink id="cadastrar" value="Cadastrar"
action="#{cadastroBean.cadastrar}"/>
www.algaworks.com 94
<h:commandLink id="voltar" action="home">
<h:graphicImage value="/imagens/voltar.png" />
<h:outputText value="Voltar"/>
</h:commandLink>
- *. '$)&. - "$- ( *(* 0( */9* .0( / - * !*-(0'7-$* 0.)* * (;/**
D*!-( 2*-&" -17-$*.>$"*. ( 1-$+/F %.= (  E
<a id="frm:cadastrar" href="#" onclick="mojarra.jsfcljs(
document.getElementById('frm'),{'frm:cadastrar':'frm:cadastrar'},'');
return false">Cadastrar</a>
<a id="frm:voltar" href="#" onclick="mojarra.jsfcljs(
document.getElementById('frm'),{'frm:voltar':'frm:voltar'},'');
return false"><img src="/Financeiro/imagens/voltar.png" />Voltar</a>
*(+*) )/ \#E*0/+0/ $)&]
. *(+*) )/ . <h:commandButton> <h:commandLink> .0( / ( - ,0$.$:@ . *
. -1$*-F (  / -($). .$/0:@ .D 1*< +* +- $.- .$(+' .( )/  0( '$)&
*(0(  F- ./ .*D 3$./ **(+*) )/ <h:outputLink>F
<h:outputLink value="http://www.algaworks.com" target="_blank">
<h:graphicImage library="algaworks" name="logo.png" />
</h:outputLink>
./ *(+*) )/ !$'$/  *)./-0:9*   . *( +-8( /-*. L*.+2,-+"' MF .
+-8( /-*..9* .+ $B*.0.)*/" <f:param>F
<h:outputLink value="http://www.algaworks.com" target="_blank">
<f:param name="codigo" value="931"/>
<f:param name="grupo" value="Java"/>
<h:graphicImage library="algaworks" name="logo.png" />
</h:outputLink>
* '$- )* '$)& " -* + '* >$"* $(D .*(*. - $- $*)*. +- * ) - :*
!--)5990006% 0(+$,6(&98(" (CB?=4 +.)(C /F
VFRSF $);$.
1 -1 -  . +*..0$ *(+*) )/ .  +$);$. ,0 %0(  *-")$5- *. *0/-*.
*(+*) )/ .+7"$)F
www.algaworks.com 95
*(+*) )/ \#E+) '-$]
 *(+*) )/ <h:panelGrid> - ) -$5 0( / '   F ' !0)$*) *(*
0( "- *-")$5*-  $/ ). )* '4*0/  +7"$)D +* *(*- ,0',0 -
*0/-**(+*) )/ F
<h:panelGrid columns="2">
<f:facet name="header">
<h:outputText value="Dados para cadastro" />
</f:facet>
<h:outputText value="Nome:" />
<h:inputText size="20" />
<h:outputText value="E-mail:" />
<h:inputText size="40" />
<h:outputText value="Senha:" />
<h:inputSecret size="20" />
<h:outputText />
<h:commandButton value="Cadastrar" />
</h:panelGrid>
 /-$0/* columns .+ $B * )A( -*  *'0). ,0 ,0 - (*. +*- '$)#F 
- ) -$5*- * <h:panelGrid> *-")$5 *. *(+*) )/ . ( *'0).D  .,0 -
+-$- $/D  $(+-$3*F
 *(+*) )/ ,0 ./$1 -  )/-* * +$) ' . -7 *(** ( 0( ;'0' 
/ 'F 0)* +- $.(*. +0'- 0( ;'0'D +* (*. $)'0$- 0( *(+*) )/ . (
1'*- '"0( *0 ,0 )9* " - $)!*-(:@ . 1$.0$.D *(* !*$ * .*  <h:outputText>F
 +*..=1 ' '/ --  .+ ..0-  *-D * .+:( )/* )/- ;'0'.D *- . 17-$.
*0/-.*$..D( .(!*-(,0 1*</(;(+* !5 -*(./ '.  F
*< +* /(;( .+ $B- *  :'#* -*+; * +$) ' 0.)*  /"
<f:facet>F ./ /"  B) 0( !-"( )/*  >$"* ,0 !5 '"0( . )/$* +-
* *(+*) )/ F *- 3 (+'*D +-  B)$- *  :'#*D * )*( * -  1 . -
N#  -ND +--*+;DN!**/ -NF
www.algaworks.com 96
*(+*) )/ \#E+) '-*0+]
./ *(+*) )/ ; 0( *)/$) - ,0 "-0+ . 0. B'#*.F '  1 . - 0.*D
.+ $'( )/ D ( .$/0:@ . *) ;  . %*  $)'0.9*  17-$*. *(+*) )/ . (
0( '0"- ,0 + ). 0( ; + -($/$*D +*- 3 (+'*D *(* 0( A)$ ;'0' *
<h:panelGrid>F
(*. 1 - *(* !-=(*. +- $)'0$- 0( */9* * '*  0( (+*  )/- 
/ 3/*0.)* <h:panelGrid> +-*-")$5-*.*(+*) )/ .F
<h:panelGrid columns="2">
<f:facet name="header">
<h:outputText value="Dados para cadastro" />
</f:facet>
<h:outputText value="Nome:" />
<h:inputText size="20" />
<h:outputText value="E-mail:" />
<h:panelGroup>
<h:inputText size="40" />
<h:commandButton value="Validar" />
</h:panelGroup>
<h:outputText value="Senha:" />
<h:inputSecret size="20" />
<h:outputText />
<h:commandButton value="Cadastrar" />
</h:panelGrid>
 %*- .0'/*E
www.algaworks.com 97
VFRTF  )." ).
. +'$:@ . +- $.( /-/- )/-. * 0.07-$*D +-* ..7G'. 3$$- ( )." ). 
.0 ..*D --*.D 1 -/<)$. /F ./0- (*. ( *0/-* +=/0'* .*- 1'$:9*
*)1 -.9*  *.D (. %7 +* (*. /-'#- *( ( )." ).  ! & +- *.
0.07-$*.D +*$. *. ()"   ). +*..0 ( * +* -  $$*)- ( )." ).  0( B'D
,0  ('"0((*( )/*+* . - 3$$)+7"$)F
-$- (*. 0( ()"   ) ,0 $)./)$ 0( ( )." ( $$*) * *)/ 3/* *
!-( 2*-&F
@ManagedBean
public class CadastroBean {
private String nome;
public void cadastrar() {
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage mensagem = new FacesMessage(
FacesMessage.SEVERITY_INFO, "Cadastro efetuado.",
"Cliente " + this.nome + " cadastrado com sucesso.");
context.addMessage(null, mensagem);
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
}

)./)$(*. 0( *% /*  '.. FacesMessageD +..)* *(* +-8( /-* 


. 1 -$  ( )." (D 0( ( )." ( - .0($ 0( ( )." (  /'#F ..
'.. !*-)  *0/-*.*)./-0/*- ./(;(F
www.algaworks.com 98
 (;/** FacesContext.addMessage -   * $ * *(+*) )/ ,0  ( )." (
. -7 ..*$ 0( $)./8)$  FacesMessageF  % ,0 )9* ..*$(*. 
( )." (*() )#0(*(+*) )/ D+*$.+..(*. null )*+-$( $-*+-8( /-*F
- 3$$- /*. . ( )." ). ) +7"$)D 0.(*. * *(+*) )/ <h:messages>D
+* )* .+ $B- . ,0 - (*. 3$$-  ( )." (   /'# J*0  - .0(*D ';(
  $/-/(;(+-*+-$  .  ./$'*.F
<h:form>
<h:messages showDetail="true" showSummary="true"
errorStyle="color: red" infoStyle="color: green"
warnStyle="color: orange" fatalStyle="color: gray" />
<h:outputLabel value="Nome:" />
<h:inputText value="#{cadastroBean.nome}" />
<h:commandButton value="Cadastrar"
action="#{cadastroBean.cadastrar}" />
</h:form>
 %*- .0'/*E
VFRUF  '. *.
 *(+*) )/ <h:dataTable> " - 0( / '  D ,0  1 . - ..*$  0(
'$./  ' ( )/*.  0( ()"   )D /-1;.  0( 3+- ..9*  '$":9*  1'*-F
 ()"   ) +* */ - *. $)($( )/ D /-1;.  0( )*  *.D
-,0$1*D /F
*..* 3 (+'* . -7  0( / '  *.  '$1-*. !1*-$/*.F - $.(*. -$- 0(
'.. ,0 - +- . )/-70('$1-*D#( LivroF
public class Livro {
private String titulo;
private String autor;
public Livro() {
}
public Livro(String titulo, String autor) {
www.algaworks.com 99
super();
this.titulo = titulo;
this.autor = autor;
}
public String getTitulo() {
return titulo;
}
public void setTitulo(String titulo) {
this.titulo = titulo;
}
public String getAutor() {
return autor;
}
public void setAutor(String autor) {
this.autor = autor;
}
}
*..* ()"   ) LivrosBean +*..0$-7 0( '$./  '$1-*.D ,0 . -7 +- )#$ )
$)./)$:9* *% /*.'.. F
@ManagedBean
@ViewScoped
public class LivrosBean {
private List<Livro> livros;
public LivrosBean() {
this.livros = new ArrayList<>();
this.livros.add(new Livro("Java e Orienta‹o a Objetos",
"Thiago Faria"));
this.livros.add(new Livro("JPA 2 e Hibernate", "Thiago Faria"));
this.livros.add(new Livro("JavaServer Faces", "Thiago Faria"));
this.livros.add(new Livro("Test Driven Development", "Kent Beck"));
this.livros.add(new Livro("Start Small, Stay Small",
"Rob Walling"));
this.livros.add(new Livro("Trabalhe 4 Horas Por Semana",
"Timothy Ferris"));
this.livros.add(new Livro("Getting Real", "Jason Fried"));
this.livros.add(new Livro("Rework", "Jason Fried"));
}
public List<Livro> getLivros() {
return livros;
}
public void setLivros(List<Livro> livros) {
this.livros = livros;
}
www.algaworks.com 100
}
-$- (*. "*- 0( +7"$) "/+(,61!-&%D ,0 '$./-7 *. '$1-*. !1*-$/*. /-1;. *
*(+*) )/ <h:dataTable>F
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Livros favoritos</title>
</h:head>
<h:body>
<h:form>
<h:dataTable value="#{livrosBean.livros}" var="livro"
border="1" cellspacing="0" cellpadding="2">
<f:facet name="header">
Rela‹o de livros favoritos
</f:facet>
<h:column>
<f:facet name="header">T’tulo</f:facet>
<h:outputText value="#{livro.titulo}" />
</h:column>
<h:column>
<f:facet name="header">Autor</f:facet>
<h:outputText value="#{livro.autor}" />
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
0)* ./ *(+*) )/ ; +-* ..*D  $/ (  '$./D - ! - )$ /-1;. *
/-$0/* valueD B $.+*)=1 '  )/-* * *-+*  /"F  )*(   $/ (  '$./ ;
 B)$*+ '*/-$0/* varD+*..$$'$/)** ..* )/-*.*'0).F
<h:dataTable value="#{livrosBean.livros}" var="livro"
border="1" cellspacing="0" cellpadding="2">
 B)$(*. * - !+  / '  *.D ,0 *-- .+*) *  :'#* 
+->+-$/ 'F
<f:facet name="header">Rela‹o de livros favoritos</f:facet>
www.algaworks.com 101

). -$(*. 0. *'0). *( <h:column>F  )/-*   *'0)D +* (*. $)'0$-
,0',0 - ,0)/$  *(+*) )/ .F * .*  ./ 3 (+'*D $). -$(*. + ). 0(
<h:outputText>D';(* - !+D,0 *-- .+*) * :'#**'0)F
<h:column>
<f:facet name="header">T’tulo</f:facet>
<h:outputText value="#{livro.titulo}" />
</h:column>
<h:column>
<f:facet name="header">Autor</f:facet>
<h:outputText value="#{livro.autor}" />
</h:column>
*-(0'7-$*+-$)'0.9* '$1-*.
 $3- (*.  +7"$)  '$1-*. !1*-$/*. 0( +*0* ($. $)8($D +*..$$'$/)* ,0
* +->+-$* 0.07-$* $$*) )*1*. '$1-*.F (*. '/ -- * ()"   ) LivrosBean
+-*>$"*$3*E
@ManagedBean
@ViewScoped
public class LivrosBean {
private List<Livro> livros;
private Livro novoLivro;
public LivrosBean() {
this.livros = new ArrayList<>();
this.novoLivro = new Livro();
}
www.algaworks.com 102
public void adicionar() {
this.livros.add(this.novoLivro);
this.novoLivro = new Livro();
}
public List<Livro> getLivros() {
return livros;
}
public void setLivros(List<Livro> livros) {
this.livros = livros;
}
public Livro getNovoLivro() {
return novoLivro;
}
}
* -,0$1* "/+(,61!-&%D $)'0$- (*. 0( +$) ' *( *. (+*. */9* +- $:9*
 )*1*.'$1-*.6- ':9* '$1-*.!1*-$/*.F
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Livros favoritos</title>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="2">
<h:outputLabel value="T’tulo: "/>
<h:inputText value="#{livrosBean.novoLivro.titulo}" />
<h:outputLabel value="Autor: "/>
<h:inputText value="#{livrosBean.novoLivro.autor}" />
<h:outputLabel />
<h:commandButton value="Adicionar"
action="#{livrosBean.adicionar}" />
</h:panelGrid>
<h:dataTable value="#{livrosBean.livros}" var="livro"
border="1" cellspacing="0" cellpadding="2">
<f:facet name="header">
Rela‹o de livros favoritos
</f:facet>
<h:column>
<f:facet name="header">T’tulo</f:facet>
<h:outputText value="#{livro.titulo}" />
www.algaworks.com 103
</h:column>
<h:column>
<f:facet name="header">Autor</f:facet>
<h:outputText value="#{livro.autor}" />
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
VFRVF -,0$1*. 1-$+/ 
. *(+*) )/ . <h:outputStylesheet> <h:outputScript> +* ( . - 0.*. +-
$$*)--,0$1*.  1-$+/$'$*/  - 0-.*.*+-*% /*F
- 3 (+'$B-D -$- (*. 0( -,0$1*  .$(+' . #(* ,-"%(6,, )* $- />-$*
,+9&"'90))9+,(.+,9% 0(+$, *+-*% /*F
body {
background-color: yellow
}
-$- (*./(;(D)*( .(*$- />-$*D0(-,0$1* 1-$+/#(* ,+")-6#,F
alert('Ol‡!');
* -,0$1*   D 0.(*. *. *(+*) )/ . +- $(+*-/- *. -,0$1*. 
1-$+/F
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
www.algaworks.com 104
<h:head>
<title>Biblioteca de recursos</title>
</h:head>
<h:body>
<h:outputStylesheet library="algaworks" name="estilo.css" />
<h:outputScript library="algaworks" name="script.js"
target="head" />
</h:body>
</html>
 /" <h:outputScript>  $/ 0( +-*+-$  targetD +- 1*< .+ $B- 0(
 ./$)* $! - )/  *) * *(+*) )/ ./7F *- 3 (+'*D ( .(* * *(+*) )/
./)* )/-* <h:body>D ' . -7- ) -$5* ( <head> )*  " -*F %E
<head id="j_idt2">
<title>Biblioteca de recursos</title>
<link type="text/css" rel="stylesheet"
href="/Financeiro/javax.faces.resource/estilo.css.xhtml?ln=algaworks"
/>
<script type="text/javascript"
src="/Financeiro/javax.faces.resource/script.js.xhtml?ln=algaworks">
</script>
</head>
www.algaworks.com 105
+=/0'*W
7"$) *).0'/
'):( )/*.
WFRF -$)*)/$/4)" -
. .$./ (. ,0 0.(  +- $.(  + ). 0( $)./8)$ 
EntityManagerFactoryF ./ A)$ $)./8)$ +* . - 0. +*- ,0',0 - >$"* ,0
,0 $-*/ -0( EntityManagerF
( EntityManager ; - .+*).71 ' +*- " - )$- )/$ . )* *)/ 3/*  + -.$./<)$F
/-1;. *. (;/**.  .. $)/ -! D ; +*..=1 ' + -.$./$-D + .,0$.- 3'0$- *% /*. *
)* *.F
 $)$$'$5:9*  EntityManagerFactory +*  (*-- '"0). . "0)*.D +*- $..* 
$)./8)$ ..$)/ -!  1 . -*(+-/$'#)+'$:9*F
- $.- (*.  0( '0"- +- *'*-  $)./8)$ *(+-/$'# 
EntityManagerFactoryD *) ,0',0 - >$"* / )#  ..* !7$' -7+$*F -$- (*.
'.. JpaUtil +--(5 )-$)./8)$ (0(1-$71 ' ./7/$F
package com.algaworks.financeiro.util;
// imports...
public class JpaUtil {
private static EntityManagerFactory factory;
static {
factory = Persistence.createEntityManagerFactory(
"FinanceiroPU");
www.algaworks.com 106
}
public static EntityManager getEntityManager() {
return factory.createEntityManager();
}
}
-$(*. 0( '** ./7/$* +- $)$$'$5-  !7-$  '-"-2 ' +F
..* **-- -7
+ ). 0( 1 5D )* -- "( )/*  '.. F "*-D . (+- ,0 +- $.-(*.  0(
EntityManagerD+* (*.#(-E
EntityManager manager = JpaUtil.getEntityManager();
WFSF  -.$./$)*+ ..*. '):( )/*.
 ./ +=/0'*D -$- (*. 0( +7"$)  ,0 *).0'/ '):( )/*. * )* 
*.D+*-$..*D+- $.(*. - "$./-*.)/ ' '):( )/*.F
>$"*$3*+ -.$./ L$). - M+ ..*. '):( )/*.)./ '.*-- .+*) )/ .F
// imports...
public class CriaLancamentos {
public static void main(String[] args) {
EntityManager manager = JpaUtil.getEntityManager();
EntityTransaction trx = manager.getTransaction();
trx.begin();
Calendar dataVencimento1 = Calendar.getInstance();
dataVencimento1.set(2013, 10, 1, 0, 0, 0);
Calendar dataVencimento2 = Calendar.getInstance();
dataVencimento2.set(2013, 12, 10, 0, 0, 0);
Pessoa cliente = new Pessoa();
cliente.setNome("WWW Indœstria de Alimentos");
Pessoa fornecedor = new Pessoa();
fornecedor.setNome("SoftBRAX Treinamentos");
Lancamento lancamento1 = new Lancamento();
lancamento1.setDescricao("Venda de licena de software");
lancamento1.setPessoa(cliente);
lancamento1.setDataVencimento(dataVencimento1.getTime());
lancamento1.setDataPagamento(dataVencimento1.getTime());
lancamento1.setValor(new BigDecimal(103_000));
lancamento1.setTipo(TipoLancamento.RECEITA);
www.algaworks.com 107
Lancamento lancamento2 = new Lancamento();
lancamento2.setDescricao("Venda de suporte anual");
lancamento2.setPessoa(cliente);
lancamento2.setDataVencimento(dataVencimento1.getTime());
lancamento2.setDataPagamento(dataVencimento1.getTime());
lancamento2.setValor(new BigDecimal(15_000));
lancamento2.setTipo(TipoLancamento.RECEITA);
Lancamento lancamento3 = new Lancamento();
lancamento3.setDescricao("Treinamento da equipe");
lancamento3.setPessoa(fornecedor);
lancamento3.setDataVencimento(dataVencimento2.getTime());
lancamento3.setValor(new BigDecimal(68_000));
lancamento3.setTipo(TipoLancamento.DESPESA);
manager.persist(cliente);
manager.persist(fornecedor);
manager.persist(lancamento1);
manager.persist(lancamento2);
manager.persist(lancamento3);
trx.commit();
manager.close();
}
}
 +*$. ,0 + "(*. 0( EntityManagerD $)$$(*. 0( /-).:9* +-  *+ -:9* 
+ -.$./<)$F
EntityManager manager = JpaUtil.getEntityManager();
EntityTransaction trx = manager.getTransaction();
trx.begin();

)./)$(*. *. *% /*. ,0 ,0 - (*. + -.$./$- #((*. * (;/**


EntityManager.persist() (  0(  ' .F  +*$.D !5 (*. (&&"- ) /-).:9*
! #(*.* '-"-2 ' +F
manager.persist(cliente);
manager.persist(fornecedor);
manager.persist(lancamento1);
manager.persist(lancamento2);
manager.persist(lancamento3);
trx.commit();
manager.close();
www.algaworks.com 108
WFTF )"  ),0 *).0'/'):( )/*.
-$- (*. 0( ()"   ) ,0 *).0'/ *. '):( )/*. )* )*  *.
/-$0$  0( 1-$71 '  $)./8)$D ,0 +* -7 . - *).0'/ + ' +7"$)  /-1;.
 0( 3+- ..9* '$":9* 1'*-F
* (*. *).0'/- *% /*.  )/$ .  *(  '$)"0" (  L / +,",-'
.+2 ' . MF   ; 0( 3/ ).9*   D +*-;( *( -/ -=./$. 
*-$ )/:9*  *% /*.F *( .. '$)"0" (D )9* - ! - )$(*. / '. * )* 
*.D(.+ ). )/$ . )*..*(* '*D,0 !*-((+ .+-/ '.F
0)* !5 (*. + .,0$.. ( *% /*.D )9* +- $.(*. . ' $*)- . *'0). *
)* *.D*(*;*.*  F>$"* ( . "0$-E
select * from lancamento
$. "0$)/ !*-( (  E
from Lancamento
 .$)/3 $( (  .$")$B ,0 ,0 - (*. 0.- *. *% /*. + -.$./ )/ . 
)/$ LancamentoF
- !5 - 0( *).0'/ 0.)*  D + "(*. 0( $)./8)$  EntityManager
#((*. * (;/** createQueryD +..)* *(* +-8( /-*  ./-$)"  *.+2 *
/$+* .+ -* *(* - /*-)*F  - /*-)* . -7 * /$+* TypedQueryD ,0 +* (*. 0.-
+-*/ -0('$./*(*- .0'/**).0'/D/-1;.*(;/** getResultListF
EntityManager manager = JpaUtil.getEntityManager();
TypedQuery<Lancamento> query = manager.createQuery(
"from Lancamento", Lancamento.class);
List<Lancamento> lancamentos = query.getResultList();
 )* $..*D +* (*. $(+' ( )/- * ()"   ) ,0 *).0'/ *. '):( )/*.
)*)* *.F
@ManagedBean
@ViewScoped
public class ConsultaLancamentosBean implements Serializable {
private static final long serialVersionUID = 1L;
private List<Lancamento> lancamentos;
public void consultar() {
EntityManager manager = JpaUtil.getEntityManager();
www.algaworks.com 109
TypedQuery<Lancamento> query = manager.createQuery(
"from Lancamento", Lancamento.class);
this.lancamentos = query.getResultList();
manager.close();
}
public List<Lancamento> getLancamentos() {
return lancamentos;
}
}
WFUF 7"$) - .0'/**).0'/
-$- (*. 0( -,0$1* (',.%- '&'-(,61!-&%D ,0 / -7 0( <h:dataTable> *(
$1 -..*'0).D,0  3$ (*.*.*.'):( )/*.F
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Consulta de lanamentos</title>
</h:head>
<h:body>
<f:metadata>
<f:viewAction action="#{consultaLancamentosBean.consultar}" />
</f:metadata>
<h1>Consulta de lanamentos</h1>
<h:form id="frm">
<h:dataTable value="#{consultaLancamentosBean.lancamentos}"
var="lancamento" border="1" cellspacing="0"
cellpadding="2">
<h:column>
<f:facet name="header">
<h:outputText value="Pessoa"/>
</f:facet>
<h:outputText value="#{lancamento.pessoa.nome}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Descri‹o"/>
</f:facet>
<h:outputText value="#{lancamento.descricao}"/>
</h:column>
www.algaworks.com 110
<h:column>
<f:facet name="header">
<h:outputText value="Tipo"/>
</f:facet>
<h:outputText value="#{lancamento.tipo}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Valor"/>
</f:facet>
<h:outputText value="#{lancamento.valor}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Data de vencimento"/>
</f:facet>
<h:outputText value="#{lancamento.dataVencimento}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Data de pagamento"/>
</f:facet>
<h:outputText value="#{lancamento.dataPagamento}"/>
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
.(*. <f:viewAction> +- #(- * (;/** consultar() * ()"   ) )
!.  $)1*:9*  +'$:9*D *0 . %D )/ .  - ) -$5-  +7"$)F ./ - 0-.* ./7
$.+*)=1 '+-/$-* SFSL 1XMF
www.algaworks.com 111
*- ),0)/*D)9*$- (*.)*.+- *0+-*(.!*-(/:@ . /.D)A( -*.D /F
WFVF +-9* +*.$/*-4
* ()"   ) ConsultaLancamentosBean +*..0$ >$"*   ..*  *.D $..*
+* )9* . - $)/ - ..)/ D +-$)$+'( )/ ( +'$:@ . (;$. "-) .D +*$. )9*
*). "0$- (*. - +-*1 $/-  '>"$   ..* *. *.F *- $..*D 1(*. +- ) -
$(+' ( )/-*+-9* )(,"-(+2F
 +-9* )(,"-(+2 / ( *(* *% /$1* $.*'- * >$"*   ..*  *.  ,0',0 -
*0/- '>"$  +'$:9*D *0 . %D /0 *(* 0( ( $*- )/-  (  *(=)$*
L) ">$*M  ..*  *.D . )* - +- . )/*D *) $/0'( )/ D *(* . !*.. 0(
*' :9*  *% /*.  0( /$+* .+ =B*F ( - +*.$/>-$*  1 !*-)  - (;/**. +-
$$*)-D/0'$5-D- (*1 - J*00.-*% /*.) ..N*' :9*NF
*..* +-$( $-* - +*.$/>-$* - +- . )/-7 0( *' :9*  *% /*. * /$+* LancamentoF
* (*. #(- )*..* - +*.$/>-$*  LancamentoRepositoryD
RepositorioLancamentoD /D(.+- ! -$(*.#(7G'* LancamentosF
package com.algaworks.financeiro.repository;
// imports...
public class Lancamentos implements Serializable {
www.algaworks.com 112
private static final long serialVersionUID = 1L;
private EntityManager manager;
public Lancamentos(EntityManager manager) {
this.manager = manager;
}
public List<Lancamento> todos() {
TypedQuery<Lancamento> query = manager.createQuery(
"from Lancamento", Lancamento.class);
return query.getResultList();
}
}
 *)./-0/*- * - +*.$/>-$* Lancamentos -   -7 0( '-"-2 ' +D *0 . %D )9* ;
- .+*).$'$ *- +*.$/>-$**/ -0( '-"-2 ' +F
 (;/** todos !5  *).0'/  /**. *. '):( )/*. - /*-) 0(
List<Lancamento>F
"*-D'/ -(*.)*..*()"  )+-0.-*- +*.$/>-$* LancamentosF
@ManagedBean
@ViewScoped
public class ConsultaLancamentosBean implements Serializable {
private static final long serialVersionUID = 1L;
private List<Lancamento> lancamentos;
public void consultar() {
EntityManager manager = JpaUtil.getEntityManager();
Lancamentos lancamentos = new Lancamentos(manager);
this.lancamentos = lancamentos.todos();
manager.close();
}
public List<Lancamento> getLancamentos() {
return lancamentos;
}
}
'1 5 1*< $) )9* *).$" 1 - 0( "-) 1)/" ( ( 0.- ./ +-9*D +*$.
)*..* 3 (+'* $) ; (0$/* .$(+' .D (.  ( $ ,0 * +-*% /* !*- - . )*D *
0.* - +*.$/>-$*.*( :-7!5 -($.. )/$*F
www.algaworks.com 113
+=/0'*X
 (+'/ .*( ' /.
XFRF 0';*+-*' ( - + /$-I
 -'( )/ D . +7"$).  0( ( .(* .$./ ( . "0 ( 0( '4*0/ (0$/* .$($'-F *-
3 (+'*D *  :'#*D ( )0 -*+; .9* *. ( .(*. 0-)/ /*  )1 ":9* )
+'$:9*F  + /$- .. >$"* ( /*. . +7"$). /*-) $!=$'  ()0/ ):9* *
.$./ (D +*- $..*D ; - *( )* * 0.*   ' /. +- -$- / (+'/ . ,0 +* ( . -
- 0/$'$5*.+*-/*..+7"$).*+-*% /*F
 ./ +=/0'*D-$- (*.*/ (+'/ *.$./ (B)) $-*F
XFSF
)'0$)*0( :'#* -*+;

)'0$- (*. 0(  :'#* -*+; ) +7"$) (',.%- '&'-(,61!-&%D ';( 


'"0). ./$'*.F
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Consulta de lanamentos</title>
<h:outputStylesheet library="algaworks" name="estilo.css" />
</h:head>
<h:body>
<f:metadata>
<f:viewAction action="#{consultaLancamentosBean.consultar}" />
</f:metadata>
www.algaworks.com 114
<header>
<h:graphicImage library="algaworks" name="logo.png" />
</header>
<div id="conteudo">
<h1>Consulta de lanamentos</h1>
<h:form id="frm">
<h:dataTable value="#{consultaLancamentosBean.lancamentos}"
var="lancamento" border="1" cellspacing="0"
cellpadding="2">
<h:column>
<f:facet name="header">
<h:outputText value="Pessoa"/>
</f:facet>
<h:outputText value="#{lancamento.pessoa.nome}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Descri‹o"/>
</f:facet>
<h:outputText value="#{lancamento.descricao}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Tipo"/>
</f:facet>
<h:outputText value="#{lancamento.tipo}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Valor"/>
</f:facet>
<h:outputText value="#{lancamento.valor}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Data de vencimento"/>
</f:facet>
<h:outputText value="#{lancamento.dataVencimento}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Data de pagamento"/>
</f:facet>
<h:outputText value="#{lancamento.dataPagamento}"/>
</h:column>
</h:dataTable>
</h:form>
</div>
<footer>
Sistema Financeiro - AlgaWorks
www.algaworks.com 115
</footer>
</h:body>
</html>
-,0$1* ,-"%(6,, !*$-$**(**)/ A*$3*E
@charset "utf-8";
body {
font-size: 12px;
font-family: Arial, Helvetica, sans-serif;
margin: 0px;
font-weight: normal
}
header {
padding: 5px;
margin-bottom: 20px;
height: 30px;
background-color: #545454;
color: #fff;
box-shadow: 0px 2px 2px #ccc
}
#conteudo {
padding: 0px 8px
}
footer {
border-top: 1px solid #ccc;
padding: 5px 8px;
margin-top: 20px;
margin-bottom: 10px
}
h1 {
font-size: 24px;
font-weight: 500;
padding: 0px;
margin: 0px;
margin-bottom: 10px
}
$) )9* ./7 + -! $/D (. * 1$.0'  +7"$)  *).0'/  '):( )/*. %7
( '#*-*0./)/ F
www.algaworks.com 116
XFTF -$)*0(/ (+'/
* (*. -$- / (+'/ .  +7"$). - 0/$'$57G'*. ( /*. . +7"$).  )*..*
.$./ (D 1$/)* - +'$:9*  >$"*  '4*0/ *(0(F
)'0=(*. 0(  :'#*
-*+; ) +7"$)  *).0'/  '):( )/*.D ). +->3$(. +7"$). * .$./ (
B)) $-*D+- $.-=(*.!5 -( .(*$.F
(*. -$- 0( / (+'/ +- * .$./ ( B)) $-*F -$- (*. 0( -,0$1*
2(.-61!-&% )*$- />-$* ,+9&"'90))97
9-&)%- *(*>$"*$3*E
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>
<ui:insert name="titulo">Sistema Financeiro</ui:insert>
</title>
<h:outputStylesheet library="algaworks" name="estilo.css" />
</h:head>
<h:body>
<header>
www.algaworks.com 117
<h:graphicImage library="algaworks" name="logo.png" />
</header>
<div id="conteudo">
<ui:insert name="corpo" />
</div>
<footer>
Sistema Financeiro - AlgaWorks
</footer>
</h:body>
</html>

(+*-/(*. * '&,)   ' /. L!--)5991&%',6#)6(+ 9#,9%-,M 0.(*. 


/" <ui:insert>F ./ /"  B) - "$@ . )* / (+'/ D ,0 ( / (+*  3 0:9*
-   -9* /- #*.  >$"* !*-) $*. + '. +7"$). ,0 0/$'$5- ( * / (+'/ F  ./
/ (+'/ -$(*.0.- "$@ .D*(*.)*( .N/$/0'*N N*-+*NF
XFUF .)**/ (+'/
"*-D /*. )*... +7"$). +* ( 0.- * -,0$1*  / (+'/ 2(.-61!-&%F
$/- (*. * -,0$1* (',.%- '&'-(,61!-&%  ,0- (*. +- ,0 ' B,0
. "0$)/ !*-(E
<!DOCTYPE html>
<ui:composition template="/WEB-INF/template/Layout.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<f:metadata>
<f:viewAction action="#{consultaLancamentosBean.consultar}" />
</f:metadata>
<ui:define name="titulo">Consulta de lanamentos</ui:define>
<ui:define name="corpo">
<h1>Consulta de lanamentos</h1>
<h:form id="frm">
<h:dataTable value="#{consultaLancamentosBean.lancamentos}"
var="lancamento" border="1" cellspacing="0"
cellpadding="2">
<h:column>
<f:facet name="header">
<h:outputText value="Pessoa"/>
</f:facet>
www.algaworks.com 118
<h:outputText value="#{lancamento.pessoa.nome}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Descri‹o"/>
</f:facet>
<h:outputText value="#{lancamento.descricao}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Tipo"/>
</f:facet>
<h:outputText value="#{lancamento.tipo}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Valor"/>
</f:facet>
<h:outputText value="#{lancamento.valor}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Data de vencimento"/>
</f:facet>
<h:outputText value="#{lancamento.dataVencimento}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Data de pagamento"/>
</f:facet>
<h:outputText value="#{lancamento.dataPagamento}"/>
</h:column>
</h:dataTable>
</h:form>
</ui:define>
</ui:composition>
.(*.  /" <ui:composition> +- - ! - )$- * -,0$1*  / (+'/ $(+*-/- .
$'$*/ .  *(+*) )/ .F  +*$.D .+ $B(*. *. *)/ A*. . - "$@ . N/ 'N
N*-+*N/-1;./" <ui:define>F
www.algaworks.com 119
+=/0'*Y
*)1 -.9* 1'$:9*
YFRF
)/-*0:9*
./0- (*. ) ./ +=/0'* *(* *. *. $)!*-(*. + '*. 0.07-$*. .9* *)1 -/$*.
+-*% /*. 1 *(*;! $/1'$:9* .. .*% /*.*( 1 -1 - .F
- )/ ) - *(* !0)$*) * +-* ..*  *)1 -.9* 1'$:9*D +- $.(*. )*.
' (--*$'* 1$* F
www.algaworks.com 120
0)* * 0.07-$* +- )# 0( (+* ( 0( !*-(0'7-$* * .0( / D  $)!*-(:9*
# "*. -1$*- ;#( /%(++*.","76( L+*.,-/%.MF
 !. )%"+ /%(+,  +*.","76( * $'*  1$D *. 1'*- .  - ,0$.$:9* .9*
) 3*. ( *% /*.  *(+*) )/ . #(*.  /%(+ ,.&-"( L,.&"--
/%.MF  *(+*) )/  +7"$) +*..0$ 0( *% /* *-- .+*) )/ ) ( (>-$ *
. -1$*-D,0 -(5 )*1'*-.0( /$*F
. 1'*- .  - ,0$.$:9* .9* * /$+* StringD +*$. /0* (  ; )1$* *(*
/ 3/*F * >$"* 1D +- $.(*. '$- *( /$+*. .+ =B*.D *(* 0( $)/ $-*D +*)/*G
C0/0)/ D /D /F 3$./ )/9* 0( +-* ..*  *)1 -.9* 1'$:9* ,0 ;
3 0/* + '* !-( 2*-&D ,0 *)1 -/ *. *. ( ./-$)" +- . 0. /$+*. .+ =B*.
1'$)!. +(,,+/%"78,F
. 1'*- . *)1 -/$*. 1'$*. )9* .9* /-$0=*. *.  ).D (. + ). ) 3*.
*. *% /*. ,0 - +- . )/( *. *(+*) )/ . #(*.  /%(+, %(", L%(%
/%.,MF  ./ (*( )/*D *. *% /*. *. *(+*) )/ . +*..0 ( *. 1'*- . .0( /$*.
(!*-( / 3/* *.1'*- .'*$.%7*)1 -/$*.+-*/$+* .+ =B* 1'$*.F

)$$G. )/9*  3 0:9*  !. -.%"3+ (, /%(+, ( &(%(D ,0 /-$0$ *.


1'*- .'*$.*)1 -/$*. 1'$*.*. ).F
0-)/ * +-* ..*  *)1 -.9* 1'$:9*D . **-- - --*. *0 $)*).$./<)$.D 
+7"$) ; - 3$$ +- ,0 * 0.07-$* / )#  #)  *--$"$- * +-*' (F .
1'*- . '*$. .9* /0'$5*. )* (* '* L)*.  ).M + ). . /*. . *)1 -.@ .
1'$:@ .!*- ( (.0 $.F
www.algaworks.com 121
YFSF *)1 -.*- .+-9*
*)1 -.9* ; * +-* ..* ,0 "-)/ ,0 *. *. $"$/*. + '*. 0.07-$*. .
/-).!*-( ( (0(/$+* .+ =B*'$)"0" ( 1F
1 -1 -  . !*-)  17-$*. *)1 -.*- . +-*)/*. +- . - ( 0.*.F **. *. /$+*.
+-$($/$1*. '.. . 0+))+,D ';(  BigInteger BigDecimalD 0.( *)1 -.*- .
+-9* *  0/*(/$( )/ F *- 3 (+'*D . 1*< *'*- 0( <h:inputText>
- ! - )$)* 0( /-$0/* * /$+* Double  0(  )D * 1'*- $"$/* + '* 0.07-$*
. -70/*(/$( )/ *)1 -/$*+- Double /-$0=** )F %0( 3 (+'*E
@ManagedBean
public class CalculadoraBean {
private Double valorA;
private Double valorB;
private Double resultado;
public void somar() {
this.resultado = this.valorA + this.valorB;
}
// getters e setters
www.algaworks.com 122
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Calculadora</title>
</h:head>
<h:body>
<h:form id="frm">
<h:messages />
<h:inputText size="12" value="#{calculadoraBean.valorA}" />
+
<h:inputText size="12" value="#{calculadoraBean.valorB}" />
=
<h:outputText value="#{calculadoraBean.resultado}" />
<br />
<h:commandButton value="Somar"
action="#{calculadoraBean.somar}" />
</h:form>
</h:body>
</html>
()"  )-   0*.1'*- .$"$/*.*(* Double  ! /0*0.*( ' .F
www.algaworks.com 123
 $)!*-(-(*. 0( 1'*- ,0 )9* +* . - *)1 -/$* +- DoubleD -   (*. 0(
( )." (  --* *)1 -.9*F
/"\!E*)1 -/0( -]
. 1 5 . +- $.(*. .+ $B-  /'# . +- ,0  *)1 -.9* **-- *(*  . %(*.F
 /" <f:convertNumber> + -($/ '"0(. *)B"0-:@ . +- *)1 -.9*  )/-.
)0(;-$.F
<h:inputText size="12" value="#{calculadoraBean.valorA}">
<f:convertNumber minFractionDigits="2" maxFractionDigits="2"
locale="pt_BR" />
</h:inputText>
+
<h:inputText size="12" value="#{calculadoraBean.valorB}">
<f:convertNumber minFractionDigits="2" maxFractionDigits="2"
locale="pt_BR" />
</h:inputText>
=
<h:outputText value="#{calculadoraBean.resultado}">
<f:convertNumber type="currency" locale="pt_BR" />
</h:outputText>

)'0=(*.  /" <f:convertNumber> )*. (+*.  )/- .+ $B(*. ,0 .


..  $($. +* ( / - )* (=)$(* )* (73$(* S ="$/*.D ';(  .+ $B- *
%(% -.$' $-*D,0 !*-:-7*0.* 1=-"0'.+-. +-:9* $('F
www.algaworks.com 124
<h:inputText size="12" value="#{calculadoraBean.valorA}">
<f:convertNumber minFractionDigits="2" maxFractionDigits="2"
locale="pt_BR" />
</h:inputText>
* (*. .+ $B- 0( *)1 -.*- +- *(+*) )/ .  .= /(;(F  ./ .*D
' . !0)$*)(  !*-( $)1 -.D *0 . %D *)1 -/ (  /$+*. .+ =B*. +- ./-$)".F
* 3 (+'*D 0.(*. 0( *)1 -.*- +- 3$$- * - .0'/*  .*( )* !*-(/* 
(* -.$' $-F
<h:outputText value="#{calculadoraBean.resultado}">
<f:convertNumber type="currency" locale="pt_BR" />
</h:outputText>
 %*- .0'/*E
/"\!E*)1 -// $( ]
./ /" !5 *)1 -.@ .  J+- * /$+* java.util.DateF -$- (*. 0( '0'*-
 /.D *) * 0.07-$* $)!*-(-7 0( / . 0( ,0)/$  $. +-
$$*)-6/F
@ManagedBean
public class CalculadoraDataBean {
private Date dataBase;
www.algaworks.com 125
private Integer dias;
private Date resultado;
public void adicionar() {
Calendar dataCalculo = Calendar.getInstance();
dataCalculo.setTime(this.dataBase);
dataCalculo.add(Calendar.DAY_OF_MONTH, dias);
this.resultado = dataCalculo.getTime();
}
// getter e setter
}

)'0=(*.*.(+*.)*!*-(0'7-$* 0.(*./" <f:convertDateTime>F


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Calculadora</title>
</h:head>
<h:body>
<h:form id="frm">
<h:messages showDetail="true" showSummary="false" />
<h:inputText size="12" value="#{calculadoraDataBean.dataBase}">
<f:convertDateTime pattern="dd/MM/yyyy" />
</h:inputText>
+
<h:inputText size="12" value="#{calculadoraDataBean.dias}" />
dias =
<h:outputText value="#{calculadoraDataBean.resultado}">
<f:convertDateTime dateStyle="full" locale="pt_BR"/>
</h:outputText>
<br />
<h:commandButton value="Adicionar"
action="#{calculadoraDataBean.adicionar}" />
</h:form>
</h:body>
</html>
 +-*+-$  pattern  B) * +-9*  !*-(/:9*  / ,0 * 0.07-$* +- $.-7
$"$/-F.. !*-(/*;*( .(*0.*)'.. SimpleDateFormat 
F
www.algaworks.com 126
<h:inputText size="12" value="#{calculadoraDataBean.dataBase}">
<f:convertDateTime pattern="dd/MM/yyyy" />
</h:inputText>
 /  - .0'/* . -7 3$$ ( 0( ./$'* *(+' /*D ( +*-/0"0<. -.$' $-*F
.(*..+-*+-$  . dateStyle locale +-*)B"0--$..*F
<h:outputText value="#{calculadoraDataBean.resultado}">
<f:convertDateTime dateStyle="full" locale="pt_BR"/>
</h:outputText>
 %*- .0'/*E
YFTF '/ -)/$1.+- B)$-*)1 -.*- .
*< +* .+ $B- 0( *)1 -.*- 3+'$$/( )/ ( 0( *(+*) )/ $$*))*
 +-*+-$  converter ) /" * *(+*) )/ $)!*-()* 0(
 * *)1 -.*-
*(*1'*-+- ./ /-$0/*F*- 3 (+'*E
<h:outputText value="#{pedidoBean.codigoProduto}"
converter="javax.faces.Integer"/>
 1 -1 - .%7+*..0$'"0).
. *)1 -.*- .+-;G B)$*.E
K %13F! .F0( -
K %13F! .F**' )
www.algaworks.com 127
K %13F! .F4/
K %13F! .F#-/ -
K %13F! .F*0'
K %13F! .F'*/
K %13F! .F
)/ " -
K %13F! .F *)"
K %13F! .F#*-/
K %13F! .F$" $('
K %13F! .F$"
)/ " -
K %13F! .F/ $(
0/- !*-(   $3- 3+'=$/* ,0' *)1 -.*- ,0 - (*. 0.- ; *( /"
<f:converter>D .+ $B)**
**)1 -.*-)*/-$0/* converterIdF
<h:outputText value="#{pedidoBean.codigoProduto}">
<f:converter converterId="javax.faces.Integer"/>
</h:outputText>
YFUF 0./*($5)*( )." ).  --* *)1 -.9*
*< +* +- $.- 0./*($5- . ( )." ).  --*.D ,0)* . *)1 -.@ . )9* .9*
 ( .0 $.F -$( $-( )/ D 0./*($5- (*. . ( )." ).  --*  *)1 -.9*
 (+*. /.F
- *( :-D -$- (*. 0( -,0$1*  &,, , .'% $)'0$- (*. * . "0$)/
*)/ A* )/-* ' E
javax.faces.converter.DateTimeConverter.DATE=Data inv‡lida.
javax.faces.converter.DateTimeConverter.DATE_detail=O campo ''{2}'' \
n‹o foi informado com uma data v‡lida.
./ -,0$1* !*$ *'**  )/-* * +*/ com.algaworks.resourcesD *( * )*(
,, ,6)+()+-",F *(* ./(*. 0.)* 1 )D  *)1 ):9* ; ,0 -$ (*. *
+*/ *'*,0 (*. -,0$1*.  - 0-.*. )* $- />-$* ,+9&"'9+,(.+, * +-*% /*F
0./*($5(*. . ( )." ).  --*  - .0(*  /'# +- *)1 -.9*  /J
#*-F
www.algaworks.com 128
"*-D $)'0$- (*. * . "0$)/ >$"* )* -,0$1* ,7('3 61&%D +- ,0 ./ +*/
 ( )." ).. %-- "*+ '+'$:9*F
<application>
<message-bundle>
com.algaworks.resources.Messages
</message-bundle>
</application>
(*. 3 0/- * 3 (+'*  0( . :9* )/ -$*-D ,0 -$(*. 0( '0'*- 
/D $)!*-(-0(/$)*-- /F
 ( )." ( ,0 0./*($5(*. !*$ 0.H * (*. ( '#*--  ( )." ( $)'0$)*
+-*+-$  label )*(+*F
<h:inputText size="12" value="#{calculadoraDataBean.dataBase}"
label="Data base">
<f:convertDateTime pattern="dd/MM/yyyy" />
</h:inputText>
"*- * 1'*-  +-*+-$  label !*$ 0.* )* )%!(%+ ,0 $)'0=(*. )
( )." (  /'# F
www.algaworks.com 129
- 0./*($5- *0/-. ( )." ).D +- $.(*. *)#  - . #1 .  '.F * (*.
*).0'/-  .+ $B:9*  1 -1 -  . *0D $) ($. !7$'D -$- * -,0$1*
,, ,;)-;6)+()+-", ,0 B )* +*/ javax.faces * -,0$1*
#/16,6>6>616#+F
www.algaworks.com 130
+-*+-$  *)1 -/ - .."
 0./*($5:9*  ( )." ). ,0 B5 (*. ; (0$/* $)/ - ..)/ D +*$. /*  +'$:9*
0/$'$5 . )*1. ( )." ).D (. '"0(. 1 5 . +* (*. ,0 - - 0( ( )." (
 --*  *)1 -.9* .+ =B +- 0( *(+*) )/  +7"$)F - ./  )7-$*D
 1 (*. 0.-  +-*+-$  converterMessage * (+*  )/-D *(* )*
3 (+'*$3*E
<h:inputText size="12" value="#{calculadoraDataBean.dataBase}"
converterMessage="Informe uma data base corretamente.">
<f:convertDateTime pattern="dd/MM/yyyy" />
</h:inputText>
YFVF '$*- .+-9*
1 -1 -  . +*..0$ 0( ( )$.(*  1'$:9* .$(+' . C 3=1 'D / ( *(*
*% /$1* +-*/ " - * (* '* *)/- 1'*- . $)17'$*. $)!*-(*. + '*. 0.07-$*.D
1$/)* ,0 B,0 ( 0( ./* $)*).$./ )/ F  +-* ..*  1'$:9* **-- +>.
*)1 -.9**.*.F
+-*+-$  - ,0$- 
 1'$*- ($. .$(+' .  . - 0.* ; * ,0 *-$" * +- )#$( )/*  0(
(+*F - $)'0$- ./ 1'$*-D ./ $$*)-(*.  +-*+-$  required ( 0(
*(+*) )/   )/- *.F
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Calculadora</title>
</h:head>
<h:body>
<h:form id="frm">
<h:messages />
<h:inputText size="12" value="#{calculadoraBean.valorA}"
label="Valor A" required="true" />
+
<h:inputText size="12" value="#{calculadoraBean.valorB}"
label="Valor B" required="true" />
www.algaworks.com 131
=
<h:outputText value="#{calculadoraBean.resultado}" />
<br />
<h:commandButton value="Somar"
action="#{calculadoraBean.somar}" />
</h:form>
</h:body>
</html>
"*- )9* *). "0$(*. ($. .0( / - * !*-(0'7-$*  '0'*-D . ( $)!*-(- *.
1'*- . F
/"\!E1'$/ )"/#]
3$./ ( *0/-*. 1'$*- . +-9* *  ,0 .9* !*-) $*. /-1;.  /".F *
3 (+'* $3*D $$*)(*. 0( 1'$*- ,0 - ./-$)" * /()#* * / 3/* $"$/*
+ '* 0.07-$* ( )* (=)$(* T )* (73$(* RV -/ - .F  % ,0 $$*)(*.  /"
<f:validateLength>F
<h:form id="frm">
<h:messages />
<h:outputLabel value="Seu nome: " for="seuNome"/>
<h:inputText size="12" value="#{calculadoraBean.nome}"
id="seuNome" label="Seu nome" required="true">
<f:validateLength minimum="3" maximum="15" />
www.algaworks.com 132
</h:inputText>
<br/>
<h:inputText size="12" value="#{calculadoraBean.valorA}"
label="Valor A" required="true" />
+
<h:inputText size="12" value="#{calculadoraBean.valorB}"
label="Valor B" required="true" />
<br/>
<h:panelGroup rendered="#{calculadoraBean.resultado != null}">
<h:outputText value="#{calculadoraBean.nome}, o resultado Ž "/>
<h:outputText value="#{calculadoraBean.resultado}" />
<br />
</h:panelGroup>
<h:commandButton value="Somar" action="#{calculadoraBean.somar}" />
</h:form>
./ 1'$*- )9* ; 3 0/* . * 1'*- * (+* ./$1 - 15$*D +*- $..* *-$"(*.
*+- )#$( )/**'*)**/-$0/* requiredF
0/-*.1'$*- .
';( * 1'$*- !*-) $* + ' /" <f:validateLength>D * 1 -1 -  .
/(;( +*..0$ *0/-*.D *(* <f:validateDoubleRange>D <f:validateLongRange>D
<f:validateRegex> <f:validateRequired>F
www.algaworks.com 133
YFWF 0./*($5)*( )." ).  --*. 1'$:9*
..$( *(* 0./*($5(*. . ( )." ).  --*.  *)1 -.9*D /(;( +* (*.
0./*($5- . ( )." ).  --*.  1'$:9*F (*. !*-)  - ( )." ). *( .
#1 . +-*+-$. +-  1'$:9*  *-$"/*-$  +- * 1'$*-  /()#*
 ./-$)".F $/- (*. * -,0$1* ,, ,6)+()+-", $)'0$- (*. * . "0$)/
*)/ A*E
javax.faces.component.UIInput.REQUIRED=O campo ''{0}'' Ž obrigat—rio.
javax.faces.validator.LengthValidator.MAXIMUM=O campo ''{1}'' deve \
ser informado com no m‡ximo "{0}" caracteres.
javax.faces.validator.LengthValidator.MINIMUM=O campo ''{1}'' deve \
ser informado com no m’nimo "{0}" caracteres.
. ( )." ).  --*  1'$:9* ,0 +- $.(*. ( )*.. +7"$) !*-(
0./*($5.F
.+-*+-$  .- ,0$-  .."  1'$/*- .."
. ( )." ).  --*  1'$:9* ,0 (*.  0./*($5- . -9* 0.. +*-
/*. . +7"$). * .$./ (F * (*. 0./*($5- . ( )." ).  --*  1'$:9*
+- (+*. .+ =B*.D 0/$'$5)* . +-*+-$  . requiredMessage
validatorMessageF
www.algaworks.com 134
<h:inputText size="12" value="#{calculadoraBean.nome}" id="seuNome"
label="Seu nome" required="true"
requiredMessage="Informe o seu nome."
validatorMessage="Informe o seu nome de 3 a 15 caracteres.">
<f:validateLength minimum="3" maximum="15" />
</h:inputText>
YFXF -$)**)1 -.*- .+ -.*)'$5*.
. *)1 -.*- . ,0 %7 1 ( (0/$*. )*  .9* A/ $. ) ($*-$ *. .*.D +*-;(
3$./ ( '"0(. .$/0:@ . ,0 1*< +* +- $.- -$- 0( *)1 -.*- 0./*($5*F
* 3 (+'*  . "0$-D -$(*. 0( *)1 -.*- + -.*)'$5* +- . - 0.* ( (+*.
 /F  (*. ,0 +- (+*.  / %7 3$./ 0( *)1 -.*- +-9*D (. )*..*
*)1 -.*- . -7 0( +*0* $! - )/ F 0 - (*. 0( *)1 -.*-  /. ,0 /-).!*-(
 )/- * 0.07-$* ( *% /*. * /$+* DateD 0.)* * !*-(/* ??2222
*( 0( "-) $! - )$'E * 0.07-$* +* -7 /(;( $)!*-(- +'1-. ,0 . -9*
*)1 -/$. (/.D*(*N()#9NDN#*% N*0N*)/ (NF
 +-$( $-* +..* +- !5 - 0( )*1* *)1 -.*- ; -$- 0( '.. ,0 $(+' ( )/
 $)/ -! javax.faces.convert.Converter ; )*/ *( @FacesConverterF (
*)1 -.*- ; 0( '.. ,0 /-).!*-( ./-$)". ( *% /*. *% /*. ( ./-$)".D +*-
$..*D ..'..  1 $(+' ( )/-*.(;/**. getAsObject() getAsString()F
 (;/** getAsObject()  1 *)1 -/ -  ./-$)" +- *% /* * (;/**
getAsString()  1 *)1 -/ -  *% /* +- ./-$)"F * *)1 -/ -  ./-$)" +-
*% /*D * (;/** +* '):-  3 :9* ConverterExceptionD .* **-- '"0(
+-*' (0-)/ *)1 -.9*F
@FacesConverter("smartDate")
public class SmartDateConverter implements Converter {
private static final DateFormat FORMATADOR =
new SimpleDateFormat("dd/MM/yyyy");
@Override
public Object getAsObject(FacesContext context,
UIComponent component, String value) {
Date dataConvertida = null;
if (value == null || value.equals("")) {
return null;
}
if ("hoje".equalsIgnoreCase(value)) {
dataConvertida = getDataAtual().getTime();
} else if ("amanha".equalsIgnoreCase(value)
www.algaworks.com 135
|| "amanh‹".equalsIgnoreCase(value)) {
Calendar amanha = getDataAtual();
amanha.add(Calendar.DAY_OF_MONTH, 1);
dataConvertida = amanha.getTime();
} else if ("ontem".equalsIgnoreCase(value)) {
Calendar ontem = getDataAtual();
ontem.add(Calendar.DAY_OF_MONTH, -1);
dataConvertida = ontem.getTime();
} else {
try {
dataConvertida = FORMATADOR.parse(value);
} catch (ParseException e) {
throw new ConverterException(new FacesMessage(
FacesMessage.SEVERITY_ERROR, "Data incorreta.",
"Informe uma data correta."));
}
}
return dataConvertida;
}
private Calendar getDataAtual() {
Calendar dataAtual = new GregorianCalendar();
// limpamos informa›es de hora, minuto, segundo
// e milissegundos
dataAtual.set(Calendar.HOUR_OF_DAY, 0);
dataAtual.set(Calendar.MINUTE, 0);
dataAtual.set(Calendar.SECOND, 0);
dataAtual.set(Calendar.MILLISECOND, 0);
return dataAtual;
}
@Override
public String getAsString(FacesContext context,
UIComponent component, Object value) {
return FORMATADOR.format((Date) value);
}
}

(+' ( )/(*. )*..* *)1 -.*- .+ -/*  /. - "$./-(*. ' *( *
$ )/$B*- ,&+--D /-1;.  )*/:9* @FacesConverterF "*-D +* (*. 0.-
* *)1 -.*- .$(+' .( )/ $)!*-()* *
 ) +-*+-$  converter  /"
<h:inputText>F
<h:inputText size="12" value="#{calculadoraDataBean.dataBase}"
converter="smartDate" />
www.algaworks.com 136
'/ -(*.  +7"$)  '0'*-  /D ,0 0.(*. )* 3 (+'*  0( . :9*
)/ -$*-D $)'0=(*.  +*..$$'$  $)!*-(- '"0(. +'1-. .+ $$. +-
- ! - )$-/.D*(*+*- 3 (+'*N*)/ (NF
YFYF -$)*1'$*- .+ -.*)'$5*.
 +-* ..* +- -$- 1'$*- . + -.*)'$5*. ; . ( '#)/ 6 -$:9* 
*)1 -.*- .F -$- (*. 0( 1'$*-  /. ,0 - ./-$)" *. 1'*- . + ). ( $.
A/ $.F
- -$- 0( 1'$*- 0./*($5*D +- $.(*. $(+' ( )/-  $)/ -!
javax.faces.validator.Validator )*/-'.. *( @FacesValidatorF
@FacesValidator("diaUtil")
public class DiaUtilValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent component,
Object value) throws ValidatorException {
Date data = (Date) value;
Calendar calendar = Calendar.getInstance();
www.algaworks.com 137
calendar.setTime(data);
int diaSemana = calendar.get(Calendar.DAY_OF_WEEK);
if (diaSemana < Calendar.MONDAY
|| diaSemana > Calendar.FRIDAY) {
throw new ValidatorException(new FacesMessage(
FacesMessage.SEVERITY_ERROR, "Data inv‡lida.",
"A data informada n‹o Ž um dia œtil."));
}
}
}
 "$./-(*. )*..* 1'$*- *( * $ "-"%F "*-D +* (*. 0.7G'* /-1;.  /"
<f:validator>F
<h:inputText size="12" value="#{calculadoraDataBean.dataBase}">
<f:convertDateTime pattern="dd/MM/yyyy"
timeZone="America/Sao_Paulo" />
<f:validator validatorId="diaUtil" />
</h:inputText>
www.algaworks.com 138
+=/0'*Z
7"$) ./-* '):( )/*
ZFRF
(+' ( )/)**- +*.$/>-$*
 ./ +=/0'*D -$- (*. 0( +7"$)  ./-* '):( )/*.F  +-$( $- *$. ,0
1(*.!5 -D;$)'0$-0((;/** adicionar() )*- +*.$/>-$* LancamentosF
public void adicionar(Lancamento lancamento) {
this.manager.persist(lancamento);
}
 +*$.D -$- (*. 0( )*1* - +*.$/>-$* #(* PessoasD *( (;/**. +- *).0'/
 /*..+ ..*.  0(A)$+ ..*+*-0(>$"* .+ =B*F
package com.algaworks.financeiro.repository;
// imports
public class Pessoas implements Serializable {
private static final long serialVersionUID = 1L;
private EntityManager manager;
public Pessoas(EntityManager manager) {
this.manager = manager;
}
public Pessoa porId(Long id) {
return manager.find(Pessoa.class, id);
}
public List<Pessoa> todas() {
TypedQuery<Pessoa> query = manager.createQuery(
"from Pessoa", Pessoa.class);
return query.getResultList();
www.algaworks.com 139
}
}
ZFSF
(+' ( )/)*.- "-. ) ">$*
. - "-.  ) ">$*  0( +'$:9* )9*  1 ( B- ( ()"   ).F
- $.(*. -$- '.. . ,0 /-/( + ).  +-* ..*.  ) ">$*D !$'$/)* 
()0/ ):9* - +-*1 $/( )/*'>"$*.$./ (F
(*. -$- 0( /$+*  3 :9* #(* NegocioExceptionD +- - +- . )/- --*.
 ) ">$*F
package com.algaworks.financeiro.service;
public class NegocioException extends Exception {
private static final long serialVersionUID = 1L;
public NegocioException(String msg) {
super(msg);
}
}
"*-D -$- (*. )*.. '..  ) ">$* ,0 /-/  ./-*  '):( )/*.F ..
'..  + ) -7 * - .+*.$/>-$*  '):( )/*.D +*- $..*D '  1 -   - * *% /*
*- +*.$/>-$*)**)./-0/*-F
package com.algaworks.financeiro.service;
// imports
public class CadastroLancamentos implements Serializable {
private static final long serialVersionUID = 1L;
private Lancamentos lancamentos;
public CadastroLancamentos(Lancamentos lancamentos) {
this.lancamentos = lancamentos;
}
public void salvar(Lancamento lancamento) throws NegocioException {
if (lancamento.getDataPagamento() != null &&
lancamento.getDataPagamento().after(new Date())) {
throw new NegocioException(
"Data de pagamento n‹o pode ser uma data futura.");
}
www.algaworks.com 140
this.lancamentos.adicionar(lancamento);
}
}
 !*-( ,0 $(+' ( )/(*.D +* (*. 0.-  '.. CadastroLancamentos ( 0(
.$./ ( .&/*+*02 D$)'0.$1 0.)*,0',0 -!-( 2*-&F
ZFTF -*"-()**()"  ) ./-*
(*. -$- * ()"   ) ,0 . -7 0.* + ' +7"$)  ./-*  '):( )/*.F
 (;/** salvar()  1 $)$$- 0( /-).:9*D $)./)$- 0( *% /* * /$+*
CadastroLancamentos .'1- * '):( )/*F .* **-- '"0( --*D  1 G. !5 -
+(%%$ +- . )/-0(( )." (  --*F
-$(*. /(;( 0( (;/** #(* prepararCadastro()D ,0 -- " 0( '$./
*( /*. . + ..*. ./-.F .- (*. .. '$./ +- +- )# - * ( )0 
+ ..*.)+7"$)F
package com.algaworks.financeiro.controller;
// imports
@ManagedBean
@ViewScoped
public class CadastroLancamentoBean implements Serializable {
private static final long serialVersionUID = 1L;
private Lancamento lancamento = new Lancamento();
private List<Pessoa> todasPessoas;
public void prepararCadastro() {
EntityManager manager = JpaUtil.getEntityManager();
try {
Pessoas pessoas = new Pessoas(manager);
this.todasPessoas = pessoas.todas();
} finally {
manager.close();
}
}
public void salvar() {
EntityManager manager = JpaUtil.getEntityManager();
EntityTransaction trx = manager.getTransaction();
FacesContext context = FacesContext.getCurrentInstance();
www.algaworks.com 141
try {
trx.begin();
CadastroLancamentos cadastro = new CadastroLancamentos(
new Lancamentos(manager));
cadastro.salvar(this.lancamento);
this.lancamento = new Lancamento();
context.addMessage(null, new FacesMessage(
"Lanamento salvo com sucesso!"));
trx.commit();
} catch (NegocioException e) {
trx.rollback();
FacesMessage mensagem = new FacesMessage(e.getMessage());
mensagem.setSeverity(FacesMessage.SEVERITY_ERROR);
context.addMessage(null, mensagem);
} finally {
manager.close();
}
}
public List<Pessoa> getTodasPessoas() {
return this.todasPessoas;
}
public TipoLancamento[] getTiposLancamentos() {
return TipoLancamento.values();
}
public Lancamento getLancamento() {
return lancamento;
}
public void setLancamento(Lancamento lancamento) {
this.lancamento = lancamento;
}
}
ZFUF -*"-()***)1 -.*-  ..*
-$(*. 0( *)1 -.*- ,0 ; +5  *)1 -/ - 0( ./-$)" *( * >$"*  + ..*
(*% /**/$+* PessoaD 1$ G1 -.F
 "$./-(*. * *)1 -.*- *)B"0-(*. ' *(* +-9* +- * /$+* PessoaD 0.)*
*/-$0/* forClass )*/:9* @FacesConverterF
package com.algaworks.financeiro.converter;
www.algaworks.com 142
// imports
@FacesConverter(forClass = Pessoa.class)
public class PessoaConverter implements Converter {
@Override
public Object getAsObject(FacesContext context,
UIComponent component, String value) {
Pessoa retorno = null;
EntityManager manager = JpaUtil.getEntityManager();
try {
if (value != null && !"".equals(value)) {
Pessoas pessoas = new Pessoas(manager);
retorno = pessoas.porId(new Long(value));
}
return retorno;
} finally {
manager.close();
}
}
@Override
public String getAsString(FacesContext context,
UIComponent component, Object value) {
if (value != null) {
return ((Pessoa) value).getId().toString();
}
return null;
}
}
ZFVF -$)**!*-(0'7-$* ./-*
 +7"$)  ./-* 0. * ,0 %7 !'(*. ( +=/0'*. )/ -$*- .F 9* $)'0=(*.
1'$:@ . )*. (+*.  )/-D +*- $..*D +*- ),0)/*D ; $(+*-/)/ +- )# -
*-- /( )/ + '*( )*.*.(+*.*-$"/>-$*.F
<!DOCTYPE html>
<ui:composition template="/WEB-INF/template/Layout.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<f:metadata>
<f:viewAction action="#{cadastroLancamentoBean.prepararCadastro}" />
</f:metadata>
www.algaworks.com 143
<ui:define name="titulo">Cadastro de lanamento</ui:define>
<ui:define name="corpo">
<h1>Cadastro de lanamento</h1>
<h:form id="frm">
<h:messages showDetail="false" showSummary="true" />
<h:panelGrid columns="2">
<h:outputLabel value="Tipo" />
<h:selectOneRadio
value="#{cadastroLancamentoBean.lancamento.tipo}"
label="Tipo do lanamento">
<f:selectItems
value="#{cadastroLancamentoBean.tiposLancamentos}"
var="tipoLancamento"
itemValue="#{tipoLancamento}" />
</h:selectOneRadio>
<h:outputLabel value="Pessoa" />
<h:selectOneMenu
value="#{cadastroLancamentoBean.lancamento.pessoa}"
label="Pessoa">
<f:selectItem
itemLabel="Selecione" noSelectionOption="true" />
<f:selectItems
value="#{cadastroLancamentoBean.todasPessoas}"
var="pessoa" itemValue="#{pessoa}"
itemLabel="#{pessoa.nome}" />
</h:selectOneMenu>
<h:outputLabel value="Descri‹o" />
<h:inputText size="60"
value="#{cadastroLancamentoBean.lancamento.descricao}"
label="Descri‹o" />
<h:outputLabel value="Valor" />
<h:inputText size="12"
value="#{cadastroLancamentoBean.lancamento.valor}"
label="Valor">
<f:convertNumber locale="pt_BR" maxFractionDigits="2"
minFractionDigits="2" />
</h:inputText>
<h:outputLabel value="Data de vencimento" />
<h:inputText size="12"
value="#{cadastroLancamentoBean.lancamento
.dataVencimento}"
label="Data de vencimento">
<f:convertDateTime pattern="dd/MM/yyyy"
timeZone="America/Sao_Paulo" />
</h:inputText>
<h:outputLabel value="Data de pagamento" />
www.algaworks.com 144
<h:inputText size="12"
value="#{cadastroLancamentoBean.lancamento
.dataPagamento}"
label="Data de pagamento">
<f:convertDateTime pattern="dd/MM/yyyy"
timeZone="America/Sao_Paulo" />
</h:inputText>
<h:outputLabel />
<h:commandButton value="Salvar"
action="#{cadastroLancamentoBean.salvar}" />
</h:panelGrid>
</h:form>
</ui:define>
</ui:composition>
.(*.  /" <f:viewAction> ( <f:metadata> +- #(- * (;/**
prepararCadastro() *()"  ))!.  $)1*:9*+'$:9*F
 %(*.*- .0'/* )*..+7"$)- ;(-$E
www.algaworks.com 145
+=/0'*RQ
 )'$/$*)
RQFRF ,0 ; )'$/$*)I
 
  ) '$/$*) !*-)  0( !$'$ +- 1'$- *% /*. ( $! - )/ .
(.  +'$:9*F 1 -1 -  . $)/ "- *( ./ / )*'*"$ +- 1'$-
*% /*.+- )#$*.+ '.+7"$).,0 -$(*.F
 1)/" (  0.-  ) '$/$*) ; ,0 . - ./-$:@ . B( $). -$. ). '.. . 
(* '*D )9* ( +7"$).   D +*- $..*D +* ( . - 0.. +*- *0/-. (. 
+'$:9*F
. - ./-$:@ .   ) '$/$*) .9* ( !*-(  )*/:@ .D ,0 +* ( . - 0..D
+*- 3 (+'*D ( )/$ .*0'.. . ()"  ).F
$1 -.. )*/:@ .  - ./-$:@ . ./9* $.+*)=1 $. )* +*/
javax.validation.constraintsF  %(*. 0( 3 (+'*  0( '.. *( /-$0/*.
)*/*.*(- ./-$:@ . )'$/$*)F
public class Usuario {
@NotNull
@Size(min = 5, max = 20)
private String nome;
@NotNull
@Size(min = 5, max = 40)
private String sobrenome;
}
www.algaworks.com 146
 ) '$/$*) ; 0( .+ $B:9*D )9* 0( +-*0/*D +*- $..*D +- $.(*.  0(
$(+' ( )/:9* +- /0* !0)$*)-F . $(+' ( )/:@ . +* ( $$*)- - ./-$:@ .
0./*($5.D ';( . !*-) $. + ' .+ $B:9*F .- (*. * $ -)/
'$/*-D,0 $(+' ( )/ )'$/$*)F
RQFSF $$*))**-/ !/*)*+*(F3('
 $ -)/ '$/*- +* . - $3* ( #//+EJJ#$ -)/ F*-"J1'$/*-JD (.
*(* ./(*. 0.)* 1 )D +* (*. !$'( )/ $$*)- * -/ !/* )* -,0$1*
)(&61&%  )*..*+-*% /*F
<!-- Implementacao do Bean Validation -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
<scope>compile</scope>
</dependency>
 ./ +=/0'*D$(+' ( )/- (*.*. 3 (+'*.)*+-*% /**.$./ (B)) $-*F
RQFTF $$*))*- ./-$:@ .)*(* '*

)'0$- (*.'"0(.- ./-$:@ . ()*..*(* '*D).'.. . Lancamento PessoaF


@Entity
@Table(name = "lancamento")
public class Lancamento implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private Pessoa pessoa;
private String descricao;
private BigDecimal valor;
private TipoLancamento tipo;
private Date dataVencimento;
private Date dataPagamento;
@Id
@GeneratedValue
public Long getId() {
return id;
}
@NotNull
www.algaworks.com 147
@ManyToOne(optional = false)
@JoinColumn(name = "pessoa_id")
public Pessoa getPessoa() {
return pessoa;
}
@NotEmpty
@Size(max = 80)
@Column(length = 80, nullable = false)
public String getDescricao() {
return descricao;
}
@NotNull
@DecimalMin("0")
@Column(precision = 10, scale = 2, nullable = false)
public BigDecimal getValor() {
return valor;
}
@NotNull
@Enumerated(EnumType.STRING)
@Column(nullable = false)
public TipoLancamento getTipo() {
return tipo;
}
@NotNull
@Temporal(TemporalType.DATE)
@Column(name = "data_vencimento", nullable = false)
public Date getDataVencimento() {
return dataVencimento;
}
@Temporal(TemporalType.DATE)
@Column(name = "data_pagamento", nullable = true)
public Date getDataPagamento() {
return dataPagamento;
}
// setters, hashCode e equals
}
@Entity
@Table(name = "pessoa")
public class Pessoa implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String nome;
@Id
@GeneratedValue
www.algaworks.com 148
public Long getId() {
return id;
}
@NotEmpty
@Size(max = 60)
@Column(length = 60, nullable = false)
public String getNome() {
return nome;
}
// setters, hashCode e equals
}
 %(*.*.$")$B*.)*/:@ .,0 0.(*.+-$$*)-- ./-$:@ .F
,-+"76( ,+"76(
O*/0'' 1'*-+-*+-$  )9*+* . - nullF
'$,0 0(./-$)"D*' :9*D(+*0--4)9*;)0'* ) (
O*/(+/4
15$*F./- ./-$:9*; .+ =B* $ -)/ '$/*-F
/()#**1'*-+-*+-$   1  ./- )/- *.'$($/ .
O$5
*)B"0-*.F0)$*)*(./-$)"D*' :9*D(+ --4F
1'*-+-*+-$   1 . -0()A( -* $('($*-*0
O $('$)
$"0'*)A( -* .+ $B*F
3$./ ( $1 -.. *0/-. )*/:@ .  ) '$/$*) /(;( * $ -)/ '$/*-F
*< +* )*)/-7G'. )*. +*/ . javax.validation.constraints
org.hibernate.validator.constraintsF
*(* 0( +..  (7"$D +* (*.  ..-  +7"$)  ./-*  '):( )/*.
/ )/- .0( / - * !*-(0'7-$* *( *. $)17'$*. *0 $).0B$ )/ .D *  $-7 0.- 
$)/ "-:9**( )'$/$*)+-1'$-*.1'*- .F
www.algaworks.com 149
RQFUF 0./*($5)*( )." ). 1'$:9*
*- +-9*D * 1 -1 -  . )9* $$*) * ->/0'* * (+* ). ( )." ). 
--* " -. /-1;. . - ./-$:@ .  ) '$/$*)D (. +* (*. '/ -- .. !*-(/*D
-$)* 0( -,0$1* ,, ,6)+()+-", )* +*/
com.algaworks.financeiro.resources  )*..*+-*% /*D*(*. "0$)/ *)/ A*E
javax.faces.validator.BeanValidator.MESSAGE={1} {0}

)'0=(*. *$. )%!(%+, )* !*-(/*  ( )." ( 0. +- - ./-$:@ .  )


'$/$*)F )%!(%+ {1} - +- . )/*->/0'**(+* {0} ( )." (F
 +*$. $..*D +- $.(*. $)'0$- * . "0$)/ >$"* )* -,0$1* ,7('3 61&%D +-
,0  ./ +*/  ( )." ).. %-- "*+ '+'$:9*F
<application>
<message-bundle>
com.algaworks.financeiro.resources.Messages
</message-bundle>
</application>
www.algaworks.com 150
*). "0$(*. ( '#*--  $ )/$B:9* *. (+*. ,0 ./9* *( --*D (. +* (*.
 $3- $) ( '#*-D 0./*($5)* . ( )." ). . - ./-$:@ .F - $..*D ./ -$-
0( -,0$1* %"-"(' ,, ,6)+()+-", ) -=5 * $- />-$* ,+9&"'9+,(.+, *
+-*% /*D*(**)/ A*. "0$-E
javax.validation.constraints.NotNull.message = deve ser informado.
javax.validation.constraints.Size.message = deve ter tamanho entre \
{min} e {max}.
javax.validation.constraints.DecimalMin.message = deve ser maior ou \
igual a {value}.
org.hibernate.validator.constraints.NotEmpty.message = deve ser \
informado.
0./*($5(*.+ )..( )." )..- ./-$:@ .,0  ./(*.0.)*F
www.algaworks.com 151
- *)#  - . *0/-. #1 .  ( )." ).D *).0'/ * -,0$1*
%"-"(' ,, ,6)+()+-", * -,0$1* !"+'-7/%"-(+7@61616"'%6#+D
- ! - )$*+ '*+-*% /*F
www.algaworks.com 152
RQFVF *(+*)*0()*1- ./-$:9*
* (*. -$- - ./-$:@ .  ) '$/$*) 0./*($5.  +-/$- . )*/:@ .
3$./ )/ .F *- 3 (+'*D $("$) ,0 +- $.- (*. $$*)- . - ./-$:@ . @NotNull
@DecimalMin ( $1 -.. +-*+-$  . ( )*..* .$./ (F  -=(*. ,0 - + /$- *
( .(*>$"* ($1 -.*.'0"- .)*>$"*G!*)/ F
@NotNull
@DecimalMin("0")
@Column(precision = 10, scale = 2, nullable = false)
public BigDecimal getValor() {
return valor;
}
* (*. !$'$/- 0( +*0*D -$)* 0( A)$ - ./-$:9* #(
@DecimalPositivoD ,0 ; .$(+' .( )/ 0( *(+*.$:9* . - ./-$:@ . @NotNull
@DecimalMin %0)/.F
package com.algaworks.financeiro.validation;
// imports
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Constraint(validatedBy = {})
@NotNull
@DecimalMin("0")
public @interface DecimalPositivo {
@OverridesAttribute(constraint = DecimalMin.class, name = "message")
String message() default "{com.algaworks.NumeroDecimal.message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
 B)$(*.D +*- +-9*D ,0  ( )." (  - ./-$:9* @DecimalMin . -7 */$ 
#1 (&6% 0(+$,6.&+("&%6&,,  * -,0$1*
%"-"(' ,, ,6)+()+-",D+*-$..*D+- $.(*.$$*)-0()*1'$)#) ' F
com.algaworks.NumeroDecimal.message = deve ser um nœmero decimal \
positivo.
"*-D . (+- ,0 +- $.-(*. 1'$- 0( )A( -*  $(' +*.$/$1*D 0.- (*. 
- ./-$:9* @DecimalPositivoF
www.algaworks.com 153
@DecimalPositivo
@Column(precision = 10, scale = 2, nullable = false)
public BigDecimal getValor() {
return valor;
}
www.algaworks.com 154
+=/0'*RR
)$+0')* 1 )/*.
RRFRF
)/-*0:9*
0',0 - +'$:9* ,0 1*<  . )1*'1 -D +*- ($. .$(+' . ,0 . %D  -/( )/
+- $.-7 - .+*) - *. 1 )/*. * 0.07-$*D *(* '$,0 . ( */@ .D '$,0 . ( '$)&.D
. ' :9* 0($/ ( 0(( )0D'/ -:9* 0(1'*- (0((+*D /F
)'$.)* -+$( )/ * (* '*  1 )/*.  1 -1 -  .D )*/(*. ,0 .
+-  (0$/* *( * (* '* 0.* ( +'$:@ .  .&/*+F  $! - ): ; ,0 (  .
:@ . * 0.07-$* *)/  ( )* '$ )/ L+(0,+MF  .. !*-(D *. 1 )/*.  1 ( . -
)/- "0 . * . -1$*- /-1;.  - ,0$.$:@ .  +- ,0 ' +-* ..  '>"$ 
) ">$*- ! - )/ * 1 )/*F
1 -1 -  . +*..0$ ( )$.(*. +- /-/- $! - )/ . /$+*.  1 )/*.F  ./
+=/0'*D 1(*. ./0- .*- 1 )/*.  :9* 1 )/*.  (0):  1'*- *
0.07-$*F
RRFSF 1 )/*. :9*
. 1 )/*.  :9* .9* $)$$*. +*- *(+*) )/ .  *()*D *(*
<h:commandButton> <h:commandLink>D ,0)* *. ( .(*. .9* $.+-*. + '*
0.07-$*F ./ . 1 )/*. .9* 3 0/*. ) !.
'/(+  )%"76( * $'*  1$D
)/ . - ) -$5-- .+*./*'$ )/ F
. 1 )/*.  :9* +* ( . - %",-'+,  :9* *0 .$(+' .( )/ :@ .F  +-$( $-*
/$+* )9* *)/-$0$ +-  )1 ":9* . +7"$).D +*-;( +*..0$ $)!*-(:@ . .*-
* 1 )/*D %7 * . "0)* /$+* *)/-$0$ +-  )1 ":9* . +7"$). )9* +*..0$
www.algaworks.com 155
$)!*-(:@ . .*- * 1 )/* L*(* * *(+*) )/ ,0 " -*0  :9*MF . *$. /$+*.
+* (/-'#- (*)%0)/* (0(( .(**(+*) )/ F
- -$- 0( %",-'+  :9*D +- $.(*. -$- 0( (;/** ( 0( ()"   ) ,0
-   0(*% /**/$+* javax.faces.event.ActionEventF
( (;/**  :9*D *(* %7 1$(*. ( *0/-*. +=/0'*.D *)/-$0$ +-  )1 ":9*
.+7"$).F./ (;/** 1 - /*-)-0(./-$)"*0 voidF
public void registrarLogCadastro(ActionEvent event) {
System.out.println("Cadastrando...");
}
public String cadastrar() {
return "Consulta";
}
$)0'(*. * %",-'+  :9*  :9* /-1;. . +-*+-$  . actionListener
actionF
<h:commandButton value="Cadastrar" action="#{bean.cadastrar}"
actionListener="#{bean.registrarLogCadastro}"/>
* '$- )* */9*D * (;/** registrarLogCadastro() ; 3 0/* )/ . * (;/**
cadastrar()F
. %",-'+  :9* . 1*< ,0 - 3 0/- '"0( >$"* )/ .  '>"$ - ' * */9*D
*(* +*- 3 (+'*D " -- 0( '*"D +- +-- '"0( *% /* ,0 * (;/**  :9* $-7 0.-
*0. +- $.-/ - ..***(+*) )/  *-$" (:9*F
. action +- 3 0/-  '>"$ - '  :9* D . )  ..7-$*D /-/-  )1 ":9*
)/- +7"$).F
RRFTF 1 )/*. (0): 1'*- +-*+-$ 
$(( $/
. 1 )/*.  (0):  1'*- .9* $)1**. ,0)* 1'*- .  *(+*) )/ .
 )/- .9* (*$B*. * !*-(0'7-$* ; .0( /$* * . -1$*-F . (;/**.
#(*. ,0)* 1 )/*.  (0):  1'*- **-- (  1 ( -   - 0(
+-8( /-**/$+* ValueChangeEventF
-$- (*. 0( (;/** ,0 -   -7 1 )/*.  (0):  1'*- )* ()"   )
CadastroLancamentoBeanD*+-*% /*B)) $-*F
www.algaworks.com 156
public void descricaoModificada(ValueChangeEvent event) {
System.out.println("Valor antigo: " + event.getOldValue());
System.out.println("Novo valor: " + event.getNewValue());
}
* (*. */ - * 1'*- )/ -$*- * )*1* 1'*-  +-/$- * *% /* * /$+*
ValueChangeEventF
. (;/**.  (0):  1'*- .9* #(*. )* B)'  !. +(,,+
/%"78,D)/ . /-$0$-*.1'*- .*)1 -/$*. 1'$*.*(* '*F
 +7"$) ,-+( '&'-(61!-&%D '/ -- (*. * (+*   .-$:9* +-
#(-*(;/**,0 -$(*.D0.)*+-*+-$  valueChangeListenerF
<h:inputText size="60" label="Descri‹o"
value="#{cadastroLancamentoBean.lancamento.descricao}"
valueChangeListener="#{cadastroLancamentoBean
.descricaoModificada}" />
  ..-(*.  +7"$)D $)!*-(-(*. + ). 0(  .-$:9* *( 1'*- N ./ N
.0( / -(*. * !*-(0'7-$*D )9* *). "0$- (*. ./-- 0( '):( )/*D (. *
(;/** descricaoModificada() . -7#(*D $(+-$($-7)*).*' E
Valor antigo: null
Novo valor: Teste
 (;/** descricaoModificada() )9* ./7 !5 )* )  A/$'D (.  $ $ ;
(*./--*(**. 1 )/*. (0): 1'*-!0)$*)(F
0-)/  3 0:9*  0( (;/**  1 )/*  (0):  1'*-D +* (*. $5 - *
*)/ 3/* *  ,0 ,0 - (*. ,0  +7"$) . % - ) -$5D +0')* /*. . *0/-.
!. .*$'* 1$F
public void descricaoModificada(ValueChangeEvent event) {
System.out.println("Valor antigo: " + event.getOldValue());
System.out.println("Novo valor: " + event.getNewValue());
FacesContext.getCurrentInstance().renderResponse();
}
  ..-(*.  +7"$)  ./-*  '):( )/*D +- )# -(*. *. (+*.
.0( / -(*. * !*-(0'7-$*D ) $-7 *)/  -H  )9* '/ --(*. )
.0( / -(*. * !*-(0'7-$* )*1( )/ D *). "0$(*. ./-- * '):( )/*F
..*
*)/  +*-,0 D ) +-$( $- 1 5D * (;/** descricaoModificada() ; #(*D (.
)>.  .1$(*.  3 0:9* +-  !.  - ) -$5:9*  - .+*./F  ) !*- '/ -*
)* !*-(0'7-$*D * (;/** )9* ; $)1** )*1( )/ D +*$. *(* * +->+-$* )*( $5D ;
0( 1 )/* (0): 1'*-F
www.algaworks.com 157
-*+-$  $(( $/
*(+*) )/ .  )/-  *()* +*..0 ( 0( +-*+-$  #(
immediateD ,0 ,0)* /-$0= +- trueD !5 *( ,0 . 1'$:@ .D *)1 -.@ .
1 )/*.  .. *(+*) )/ . %( 3 0/*. 0-)/  !. )%"+ /%(+, 
+*.","76(F
(*.$)'0$- ..+-*+-$  )*(+*  .-$:9*D 1 - (*.*- .0'/*F
<h:inputText size="60" label="Descri‹o"
value="#{cadastroLancamentoBean.lancamento.descricao}"
valueChangeListener="#{cadastroLancamentoBean
.descricaoModificada}"
immediate="true" />
"*-D .  ..-(*.  +7"$)  ./-*  '):( )/*D +- )# -(*. + ). 
 .-$:9* .0( / -(*. * !*-(0'7-$*D ) *)/  -7D ) ( ( .(* . ( )." ).
 1'$:9* *. *0/-*. (+*. . -9* 3$$. L ) ( 3 0/.MD +*$. *)/$)0(*.
#()* * (;/** renderResponse() )* (;/** * 1 )/*D +*-;( ( 0( !.
,0 **-- )/ ..1'$:@ .H
1 )/*.  (0):  1'*- +* ( . - 0.*.D +*- 3 (+'*D +- $(+' ( )/-
$3.  . ' :9*  + ) )/ . *0 *(+*) )/ 0% 1$.0'$5:9*  + ) * ./*
 0( *0/-* *(+*) )/ D ,0 +* . - '/ -*  ,0',0 - (*( )/* + '* 0.07-$*F
*( *. - 0-.*.  %3D ,0 !'- (*. ( *0/-* +=/0'*D +* (*. !5 - $..*  0(
!*-(($.!7$'F
www.algaworks.com 158
+=/0'*RS

G*)/ 3/.) + ) )4

)% /$*)
RSFRF
)% :9*  + )<)$.

)% :9*   + )<)$. L)''2 "'#-"(' *0 


M ; 0( +-9* 
 . )1*'1$( )/*  .*!/2- 0.* +- ()/ - * $3* *+'( )/* )/- '.. . *
.$./ (F .  + )<)$.  0( *% /* )9* .9* $)./)$. +-*"-(/$( )/ D (.
.$($)% /. '"0(!*-(F
(*. .0+*- ,0 / (*. 0( '.. EmissorNotaFiscalD ,0  1 - "$./-- 0( %(
. (+- ,0 0()*1)*/B.'!*- ($/$F
public class EmissorNotaFiscal {
private Logging logging;
public EmissorNotaFiscal() {
this.logging = new ConsoleLogging();
}
public void emitir(NotaFiscal nf) {
// emite nota fiscal...
this.logging.registrar("Nota fiscal emitida: "
+ nf.getNumero());
}
}
Logging ;0($)/ -! F
www.algaworks.com 159
public interface Logging {
public void registrar(String mensagem);
}
ConsoleLogging ;0('.. ,0 $(+' ( )/$)/ -! LoggingF
public class ConsoleLogging implements Logging {
@Override
public void registrar(String mensagem) {
System.out.println(mensagem);
}
}
 ./-0/0- ./7 ,0. + -! $/D . )9* !*.. + ' $)./)$:9*  ConsoleLogging )*
*)./-0/*- EmissorNotaFiscalF
public class EmissorNotaFiscal {
private Logging logging;
public EmissorNotaFiscal() {
this.logging = new ConsoleLogging();
}
...
}
.. $)./)$:9* " - 0( *+'( )/*  .)  ..7-$* )/-  '..
EmissorNotaFiscal ConsoleLoggingF
 '.. EmissorNotaFiscal )9* +- $. . -  3$./<)$  ConsoleLoggingF 0*
,0 ' +- $. *)#  - ;  $)/ -! F *- $..*D -   - (*. 0( $)./8)$ * /$+* 
$)/ -! + '**)./-0/*-F
public class EmissorNotaFiscal {
private Logging logging;
public EmissorNotaFiscal(Logging logging) {
this.logging = this.logging;
}
...
}
www.algaworks.com 160
 $3(*.  '.. EmissorNotaFiscal (0$/* ($. $)/ - ..)/ F * (*. 0.7G' *(
,0',0 - /$+*  ( )$.(*  %( "' F *- 3 (+'*D +* -=(*. -$- 0( )*1
'.. ArquivoLoggingD ,0 $(+' ( )/  $)/ -! LoggingD +..- 0( $)./8)$
)* *)./-0/*-  EmissorNotaFiscalD )9* +- $.-=(*. (0- ) )#0( '$)#
 ..'.. F
..*$)$,0 *). "0$(*.0($3**(+'( )/*H
. ,0 ( $-7 $)./)$- 0( /$+*  Logging 0( *% /* EmissorNotaFiscalI  =
,0  )/-*.( )$.(*. $)% :9*  + )<)$.H

L('-1-, ' )''2
'#-"('M ;  .+ $B:9*  1  ,0 /-'#
*( $)% :9*   + )<)$.F * (*. 0.- 
+- $)./)$- $)% /- *% /*. 
)*..+'$:9*F
RSFSF *)B"0-)*
)*+-*% /*
- *)B"0-- 
( 0( +-*% /*D +-$( $-* +- $.(*.  0( $(+' ( )/:9*D
+*$. 
; + ). 0( .+ $B:9*F .- (*.  'D ,0 +* . - $3* (
#//+EJJ2 'F$G.+ F*-"JF *(* ./(*. 0.)* 1 )D + ). $$*)- (*. .
 + )<)$.)*-,0$1* )(&61&% *+-*% /*F
<!-- Weld (implementa‹o do CDI) -->
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>2.2.9.Final</version>
<scope>compile</scope>
</dependency>
<!-- Weld depende do Jandex -->
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jandex</artifactId>
<version>1.2.3.Final</version>
<scope>compile</scope>
</dependency>
- $.(*. $$*)- 0( -,0$1* #(* ('-1-61&% )* $- />-$* ,+9&"'9
0))9 7
 *+-*% /*F
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Manager pathname=""/>
<Resource name="BeanManager" auth="Container"
type="javax.enterprise.inject.spi.BeanManager"
factory="org.jboss.weld.resources.ManagerObjectFactory"/>
</Context>
www.algaworks.com 161
*-,0$1* 061&%D$$*)(*.*>$"*$3*F
<listener>
<listener-class>
org.jboss.weld.environment.servlet.Listener
</listener-class>
</listener>
<resource-env-ref>
<resource-env-ref-name>BeanManager</resource-env-ref-name>
<resource-env-ref-type>
javax.enterprise.inject.spi.BeanManager
</resource-env-ref-type>
</resource-env-ref>
* $- />-$* ,+9&"'9+,(.+,9 7
D -$(*. 0( -,0$1* 15$* #(*
',61&%F 3$./<)$ .. -,0$1*#$'$/
)*+-*% /*F
RSFTF  ).
D ( . O
)% /
( 0( +-*% /* ,0 0. 
D *., /*. . '.. . .9* *).$ -. *(*  ). 
D
/(;( *)# $. *(* 
&'  ',F  ). 
+* ( . - $)% /*. (
*0/-*. ).F
 +-*% /* * .$./ ( B)) $-* %7 !*$ *)B"0-* *( 
D "*-D 1(*. + ).
 ,0-'"0(.'.. .F
 +*.$/>-$*.
Lancamentos ; 0(  ) 
H  % ,0 )9* !*$ )  ..7-$* $5 - $..* /-1;. 
)*/:@ . *0 -,0$1*.  *)B"0-:9*F + ). + '* !/*  0( '.. +A'$ / - 0(
*)./-0/*- . ( -"0( )/*.D *0 / - 0( *)./-0/*- *( -"0( )/*. $)% /*.D !5 *(
,0  '. %0( )
F

)% /- (*. EntityManager /-1;. * *)./-0/*-  '.. LancamentosD )*/)*


*( @InjectF  +-*' ( ; ,0 EntityManager )9* ; 0(  ) 
D (. 1(*.
$")*--$..*+*- ),0)/*F
public class Lancamentos implements Serializable {
private static final long serialVersionUID = 1L;
private EntityManager manager;
www.algaworks.com 162
@Inject
public Lancamentos(EntityManager manager) {
this.manager = manager;
}
...
public void adicionar(Lancamento lancamento) {
EntityTransaction trx = this.manager.getTransaction();
trx.begin();
this.manager.persist(lancamento);
trx.commit();
}
}
-*1$.*-$( )/ D $)$$(*. 0( /-).:9* )* (;/** adicionar()F ./ )9* ; *
( '#*-'0"-+-*)/-*'-/-).:@ .D(.- .*'1 - (*.$..* (- 1 F
- $.(*.!5 -( .(*$.)**)./-0/*- PessoasF
'..  ) ">$*
 '.. CadastroLancamentosD - (*1 - (*. * *)./-0/*- $)% /- (*. *
- +*.$/>-$* $- /( )/ )* /-$0/*F  % ,0 ./ ; 0( *0/- '/ -)/$1  $)% :9*D
( .(**/-$0/*. )*+-$1*F
public class CadastroLancamentos implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private Lancamentos lancamentos;
...
}
 )../-* )( )/* ) *).0'/ )( )/*. )
)"   ). 
)9* +* ( . - $)% /*. ( ()"   ). D +*- $..*D
+- $.(*.0.-* ) CadastroLancamentoBean *(*0( )
F
 )*/:9* @Named /*-) +*..=1 ' *  ..* *  ) 
+*- 1)+,,"(' ' . D
/-1;. . 0)*( F
www.algaworks.com 163
 ). 
/(;( +*..0 ( .*+*. ,0)* $)/ "-*. *( D $)'0.$1 * .*+*
/"0F  
 D 3$./ ( 0. )*/:@ . @ViewScopedF - !0)$*)- *( 
D 
)*/:9**-- /;*+*/ javax.faces.viewF

)% /- (*. *% /*. * /$+* CadastroLancamentos Pessoas )*  )


CadastroLancamentoBeanF %*(**>$"*B($..$(+' .F
@Named
@javax.faces.view.ViewScoped
public class CadastroLancamentoBean implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private CadastroLancamentos cadastro;
@Inject
private Pessoas pessoas;
private Lancamento lancamento = new Lancamento();
private List<Pessoa> todasPessoas;
public void prepararCadastro() {
this.todasPessoas = this.pessoas.todas();
}
public void salvar() {
FacesContext context = FacesContext.getCurrentInstance();
try {
this.cadastro.salvar(this.lancamento);
this.lancamento = new Lancamento();
context.addMessage(null, new FacesMessage(
"Lanamento salvo com sucesso!"));
} catch (NegocioException e) {
FacesMessage mensagem = new FacesMessage(e.getMessage());
mensagem.setSeverity(FacesMessage.SEVERITY_ERROR);
context.addMessage(null, mensagem);
}
}
public List<Pessoa> getTodasPessoas() {
return this.todasPessoas;
}
...
}
(*.!5 -( .(*$.*(* ) ConsultaLancamentosBeanF
www.algaworks.com 164
@Named
@ViewScoped
public class ConsultaLancamentosBean implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private Lancamentos lancamentosRepository;
private List<Lancamento> lancamentos;
public void consultar() {
this.lancamentos = lancamentosRepository.todos();
}
...
}
RSFUF .*+*.  ).

. ).
+*..0 ( .*+*.D,0 +* (. -E
,()( .+76(
O ,0 ./*+ 
)/ -:9**(0.07-$* (0(A)$- ,0$.$:9* F

)/ -:9**(0.07-$* )/- (0$/.- ,0$.$:@ . D*0
O ..$*)*+ 
. %D. ..9**0.07-$*F
./**(+-/$'#**(/**.*.0.07-$*.0-)/
O++'$/$*)*+ 
/* 3 0:9*+'$:9*F
* .*+*+-9*D. ) )#0(!*- .+ $B*F)/;(
O + ) )/
*( .(*$'* 1$* ),0 *$)% /*0F

)/ -:9**(0.07-$* )/- (0$/.- ,0$.$:@ . D
O*)1 -./$*)*+ 
*(*$)=$* /;-($)**)/-*'*+ '*+-*"-(*-F
*. ... )*/:@ .  1 ( . - $(+*-/. * +*/ javax.enterprise.contextF
. T +-$( $-.D +*..0 ( *. ( .(*. )*( .  )*/:@ . * +*/
javax.faces.beanF 0)* 0.(*. 
D / (*. ,0 /*(- 0$* +- )9*
$(+*-/-*+*/  --*H
 )*/:9* @javax.faces.view.ViewScopedD + .-  !0)$*)- *( 
D ;  

  1 -1 - .F
www.algaworks.com 165
RSFVF -*0/*- )/$/4)" -
EntityManager )9* ; 0(  ) 
D +*- $..*D )9* *). "0$(*. $)% /7G'*
0/*(/$( )/ F  $)$$-(*. )*.. +'$:9*  !*-( ,0 ./7D 0( 3 :9* . -7
'):F
org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied
dependencies for type EntityManager with qualifiers @Default
at injection point [BackedAnnotatedParameter] Parameter 1 of
[BackedAnnotatedConstructor] @Inject public com.algaworks
.financeiro.repository.Pessoas(EntityManager)
at com.algaworks.financeiro.repository.Pessoas.L ..*.F%1EQM
- - .*'1 - .. +-*' (D -$- (*. 0( (;/** +-*0/*- L)+(.+ &-!(M 
EntityManagerF((;/**+-*0/*-" -0(*% /*,0 +* . -$)% /*F
*-('( )/ D 0.(*. (;/**. +-*0/*- . ,0)* ,0 - (*. $)% /- 0( *% /* ,0
)9* ; 0(  ) 
D ,0)* * /$+* *)- /* * *% /*  . - $)% /* +* 1-$- (
/ (+*  3 0:9* *0 ,0)*  $)./)$:9* * *% /* - ,0 - '"0( +-* $( )/*
$$*)'F
package com.algaworks.financeiro.util;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
// outros imports...
@ApplicationScoped
public class EntityManagerProducer {
private EntityManagerFactory factory;
public EntityManagerProducer() {
this.factory = Persistence.createEntityManagerFactory(
"FinanceiroPU");
}
@Produces
@RequestScoped
public EntityManager createEntityManager() {
return factory.createEntityManager();
}
public void closeEntityManager(@Disposes EntityManager manager) {
manager.close();
}
}
www.algaworks.com 166
 '.. EntityManagerProducer +*..0$ 0( (;/** +-*0/*- #(*
createEntityManagerF  *% /* +-*05$* +*- .. (;/** / -7 * .*+* 
- ,0$.$:9*D+*$.*(;/**!*$)*/**( @RequestScopedF
-$(*. /(;( 0( (;/**  10:9* L",)(,+ &-!(M  EntityManagerD
#(*  closeEntityManagerF ./ (;/** . -7 #(* 0/*(/$( )/
,0)* * *)/ 3/* *) * *% /* +-*05$* ./$1 - !*- ) --*F +-*1 $/(*. ./
1 )/*+-! #-* EntityManagerF
* (*.  ..- . +7"$). * .$./ ( B)) $-* )*-('( )/ D /0*  1
!0)$*)-H
RSFWF *)/-*')*./-).:@ .*($)/ - +/*- .
( $)/ - +/*- ; 0( '.. 0. +- $)/ -1$- ( #(.  (;/**. 
'.. .F * (*. 0.- $)/ - +/*- . +- 17-$. *$..D *(* - "$./-- '*". *0
3 0/-/- !.- + /$/$1. ,0 )9*!5 (+-/ - "- ) ">$**.$./ (F
-$- (*. 0( $)/ - +/*- +- *)/-*'- . /-).:@ .  )*.. +'$:9*F )/ .
 *( :-D 1(*. 1*'/- * >$"* * (;/** adicionar() * - +*.$/>-$* 
'):( )/*.+-1 -.9**-$"$)'D,0 )9*" - )$1/-).:@ .()0'( )/ F
public class Lancamentos implements Serializable {
private static final long serialVersionUID = 1L;
...
public void adicionar(Lancamento lancamento) {
this.manager.persist(lancamento);
}
}
)/ .  -$- 0( $)/ - +/*-D +- $.- (*.  0( )*/:9* ,0 ..*$-7 *
$)/ - +/*- * (;/** *0 '.. ,0 . -7 $)/ - +/F ./ )*/:9*  1 . -
)*/*( @InterceptorBindingF
package com.algaworks.financeiro.util;
// imports
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
www.algaworks.com 167
public @interface Transactional {
}
 '.. - .+*).71 ' + '. $)/ - +/:@ .  1 . - )*/ *( @Interceptor
/(;()*/:9*,0 (*. -$-D)*.*D @TransactionalF
 (;/** ,0 - '$5-7 . $)/ - +/:@ .  1 -   - InvocationContext *(*
+-8( /-*D - /*-)- 0( Object +* '):- ExceptionD ';(  . - )*/* *(
@AroundInvokeF ./ (;/**  1 #(- context.proceed()D ,0 !5 *( ,0 *
(;/**$)/ - +/*. %- '( )/ #(*F
package com.algaworks.financeiro.util;
// imports
@Interceptor
@Transactional
public class TransactionInterceptor implements Serializable {
private static final long serialVersionUID = 1L;
private @Inject EntityManager manager;
@AroundInvoke
public Object invoke(InvocationContext context) throws Exception {
EntityTransaction trx = manager.getTransaction();
boolean criador = false;
try {
if (!trx.isActive()) {
// truque para fazer rollback no que j‡ passou
// (sen‹o, um futuro commit, confirmaria atŽ mesmo
// opera›es sem transa‹o)
trx.begin();
trx.rollback();
// agora sim inicia a transa‹o
trx.begin();
criador = true;
}
return context.proceed();
} catch (Exception e) {
if (trx != null && criador) {
trx.rollback();
}
throw e;
} finally {
if (trx != null && trx.isActive() && criador) {
trx.commit();
www.algaworks.com 168
}
}
}
}
- $.(*. - "$./-- * $)/ - +/*-F ( . !*-(.  !5 - $..* ; )* -,0$1*
',61&%F
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1" bean-discovery-mode="all">
<interceptors>
<class>
com.algaworks.financeiro.util.TransactionInterceptor
</class>
</interceptors>
</beans>
 (+- ,0 +- $.-(*.  0( /-).:9* ( 0(  ) 
D ./ )*/-(*. *
(;/***( @TransactionalF
public class CadastroLancamentos implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private Lancamentos lancamentos;
@Transactional
public void salvar(Lancamento lancamento) throws NegocioException {
...
}
}
RSFXF
)% :9* (*)1 -.*- . 
9* ; +*..=1 ' $)% /-  ). 
( *)1 -.*- . D (. +-*11 '( )/ .. - 0-.*
./-7$.+*)=1 ' (0(!0/0-1 -.9** F
www.algaworks.com 169
 '$5( )/ D  $'$*/  ()$ . - .*'1 .. +-*' (D !5 )* *( ,0  $)% :9*
  + <)$. !0)$*) ( /*. . $)./8)$. )*/. *( @FacesConverterD . (
,0',0 -(*$B:9*F
- $.(*.+ ).$$*)-*-/ !/**()$ .)*-,0$1* )(&61&%F
<!-- OmniFaces (utilitarios para JSF) -->
<dependency>
<groupId>org.omnifaces</groupId>
<artifactId>omnifaces</artifactId>
<version>2.0</version>
<scope>compile</scope>
</dependency>
"*- )*..* *)1 -.*- PessoaConverter +* .$(+' .( )/ $)% /- *  ) 
*
- +*.$/>-$* + ..*.F
@FacesConverter(forClass = Pessoa.class)
public class PessoaConverter implements Converter {
@Inject // funciona graas ao OmniFaces
private Pessoas pessoas;
@Override
public Object getAsObject(FacesContext context,
UIComponent component, String value) {
Pessoa retorno = null;
if (value != null) {
retorno = this.pessoas.porId(new Long(value));
}
return retorno;
}
...
}
www.algaworks.com 170
+=/0'*RT
%3
RTFRF
)/-*0:9*
%3 L,2'!+('(., /+")- '  M ; 0( "-0+*  / )*'*"$. 2  ,0 + -($/
 -$:9*  +'$:@ . $)/ -/$1.F 0)* 0.(*. %3D . +'$:@ . 2  +* (
!5 - - ,0$.$:@ .  *)/ A* * . -1$*- . ( - -- "-  / 'D 0.)* + ).
!-"( )/*.+7"$),0 +- $.(. -/0'$5*.F
( 1 -.@ . )/ -$*- . * !-( 2*-& D +- ,0 . +'$:@ . +0 .. ( !0)$*)-
*( %3D - )  ..7-$* $$*)- $'$*/ . / - $-.F  +-/$- *  SD * .0+*-/
 %3 ; !*-) $* +*- 0( $'$*/  1-$+/ $)/ "-F *( ./ $'$*/ D
+* (*.0.-%3 (*(+*) )/ .1$.0$.D*(**/@ .D(+*.D /F
RTFSF  ) -$5:9*+-$'
 "-) 1)/" ( ( 0/$'$5- %3 ; +* - /0'$5- + ). *. *(+*) )/ .
)  ..7-$*. ( 0( +7"$)F (*. 3 (+'$B- .. !0)$*)'$ *( 0( 3 (+'*
(0$/*.$(+' .D,0 +*..0$0((+* 0(*(+*) )/  .= / 3/*F
*..*()"  )/ (0(/-$0/* nomeD*(. 0 --+ ,--+F
@Named
@RequestScoped
public class MeuBean {
private String nome;
public String getNome() {
return nome;
www.algaworks.com 171
}
public void setNome(String nome) {
this.nome = nome;
}
}
 +7"$)   0.  /" <f:ajax> +- $$*)- !0)$*)'$  %3 )*
*(+*) )/ <h:inputText>F
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Teste Ajax</title>
</h:head>
<h:body>
<h:form id="frm">
<h:inputText value="#{meuBean.nome}">
<f:ajax render="ola" />
</h:inputText>
<br/>
<h:outputText value="Ol‡ #{meuBean.nome}" id="ola" />
</h:form>
</h:body>
</html>
 B)$(*. * $ N*'N +- * *(+*) )/ <h:outputText> ,0 $$*)(*.D
- ! - )$(*. .. ( .(*$)+-*+-$  render  <f:ajax>F
<h:inputText value="#{meuBean.nome}">
<f:ajax render="ola" />
</h:inputText>
0)*  !0)$*)'$  %3 ; $$*) ( 0( <h:inputText>D +*- +-9*D
0( - ,0$.$:9* %3 ; -$ )* 1 )/*  (0):  1'*- L('!' MF * - /*-)*
 .. - ,0$.$:9*D * *(+*) )/ .+ $B* ( render . -7 /0'$5*F  - ./)/ 
+7"$)B*(* ./1D. (/0'$5:@ .F
www.algaworks.com 172
RTFTF +-*+-$   1 )/
 +-*+-$  event  <f:ajax> .+ $B * 1 )/* ,0 -$-7 0( )*1 - ,0$.$:9*
%3F * .*  0( <h:inputText>D +* (*. 0.-D +*- 3 (+'*D changeD keyupD
mouseoverD focusD blurD click  /F
0- (*. * 1 )/* ,0 $*)-7 0( )*1 - ,0$.$:9* %3  )*..* 3 (+'* +-
keyupF
<h:inputText value="#{meuBean.nome}">
<f:ajax event="keyup" render="ola" />
</h:inputText>
"*-D   )*1 ' /- $"$/ )* (+*D 0( )*1 - ,0$.$:9* %3 ; $.+- *
*(+*) )/ N*'N/0'$5*F
RTFUF +-*+-$  '$./ ) -
 +-*+-$  listener  1 . - $)!*-( *( 0( 3+- ..9*  '$":9* 
(;/**D ,0 . -7 3 0/* )* . -1$*- . (+- ,0 0( - ,0$.$:9* %3 !*-
$.+-F .. (;/** +* *0 )9* -   - 0( -"0( )/* * /$+*
AjaxBehaviorEventF
www.algaworks.com 173

)'0$- (*. 0( (;/** transformar )* ()"   )D ,0 . -7 #(* ).
- ,0$.$:@ .%3+-/-).!*-(-*)*( $"$/*+-' /-.($A.0'.F
public void transformar(AjaxBehaviorEvent event) {
this.nome = this.nome.toUpperCase();
}
"*-D!5 (*.- ! -<)$*(;/**0.)* F
<h:inputText value="#{meuBean.nome}">
<f:ajax event="keyup" render="ola"
listener="#{meuBean.transformar}" />
</h:inputText>
)*1' /-$"$/)*(+*D*(;/** transformar . -7#(*F
RTFVF  ) -$5:@ .(A'/$+'.
';(  /-).!*-(- * )*( $"$/* +- ($A.0'*D * (;/** transformar
/(;( *)/-7 ,0)/*. -/ - . !*-( $"$/*. /-$0$-7  0( )*1 1-$71 ' 
$)./8)$D#( quantidadeCaracteresF
@Named
@RequestScoped
public class MeuBean {
private String nome;
private int quantidadeCaracteres;
public void transformar(AjaxBehaviorEvent event) {
this.nome = this.nome.toUpperCase();
this.quantidadeCaracteres = this.nome.length();
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public int getQuantidadeCaracteres() {
return quantidadeCaracteres;
}
}
www.algaworks.com 174
 +7"$)   D $)'0=(*. 0( )*1* <h:outputText> +- 3$$-  ,0)/$ 
-/ - .$"$/*.F
<h:inputText value="#{meuBean.nome}">
<f:ajax event="keyup" render="ola"
listener="#{meuBean.transformar}" />
</h:inputText>
<br/>
<h:outputText value="Ol‡ #{meuBean.nome}" id="ola" />
<br/>
<h:outputText id="contador"
value="#{meuBean.quantidadeCaracteres} caracteres digitados" />
(*- *. -/ - . ./ %( . )* *)/*. + '* (;/** transformarD *
*(+*) )/ N*)/*-N)9*;/0'$5*H
* (*. .+ $B- ($.  0( *(+*) )/ ) +-*+-$  renderD . +-)* *.
$.+*- .+:*F
<h:inputText value="#{meuBean.nome}">
<f:ajax event="keyup" render="ola contador"
listener="#{meuBean.transformar}" />
</h:inputText>
* $"$/- 0( )*( D *. *$. *(+*) )/ . .9* /0'$5*. 0-)/ . - ,0$.$:@ . %3F
www.algaworks.com 175
RTFWF -* ..( )/*+-$'

- (*. '/ -- )*..* 3 (+'* +- $)$$- 0( - ,0$.$:9* %3  +-/$-  0( */9*D
#()*0((;/** :9**()"  )F
(*.  ,0- * >$"* * (;/** transformarD - (*1 )* * -"0( )/* * /$+*
AjaxBehaviorEventF
public void transformar() {
this.nome = this.nome.toUpperCase();
this.quantidadeCaracteres = this.nome.length();
}
* -,0$1*  +7"$)D - (*1 (*.  !0)$*)'$  %3 * <h:inputText>
$)'0=(*. (0()*1**/9*F
<h:inputText id="nome" value="#{meuBean.nome}" />
<br/>
<h:outputText value="Ol‡ #{meuBean.nome}" id="ola" />
<br/>
<h:outputText id="contador"
value="#{meuBean.quantidadeCaracteres} caracteres digitados" />
<br/>
<h:commandButton value="Transformar" action="#{meuBean.transformar}">
<f:ajax render="ola contador" />
</h:commandButton>
0)*  ..(*.  +7"$)D $"$/(*. 0( )*( $*)(*. * */9*D 0( 3 :9*
NullPointerException ; '):D +*$. * /-$0/* nome ./7 )0'*H ( - ,0$.$:9* %3
./7. )*$.+-D(.**(+*) )/ <h:inputText> )9* ./7. )*+-* ..*F
*- +-9*D + ). * +->+-$* *(+*) )/ ,0 $$*)(*.  !0)$*)'$  %3
; +-* ..* + '* 1 -1 -  .F - $$*)- *0/-*. *(+*) )/ .D 0.(*. 
+-*+-$  execute  <f:ajax>D$)'0$)*. 0.$.F
<h:commandButton value="Transformar" action="#{meuBean.transformar}">
<f:ajax render="ola contador" execute="nome" />
</h:commandButton>
 +7"$) !0)$*) "*-D $.+-)* - ,0$.$:@ . %3  +-/$- * '$,0 )* */9*
N-).!*-(-N +-* ..)**1'*-$"$/*)*(+*  )/-F
www.algaworks.com 176
RTFXF '1-.G#1 +-- ) -  3 0/
 ,0$. -(*. /0'$5- *0 +-* ..- 0( "-0+*  *(+*) )/ . ) +7"$)D +* (*.
0.-'"0(.+'1-.G#1 )*./-$0/*. render process  <f:ajax>F
%/+=!/ ,+"76(
O'' **.*.*(+*) )/ .+7"$)
**.*.*(+*) )/ .*!*-(0'7-$*,0 +*..0$*
O!*-(
*(+*) )/ ,0 $.+-*0- ,0$.$:9*%3
O)*)  )#0(*(+*) )/
O/#$. + ).**(+*) )/ ,0 $.+-*0- ,0$.$:9*%3
+ ). +- 3 (+'$B-D 1(*. '/ --  *)B"0-:9*  %3 * */9*
N-).!*-(-ND+-0.-'"0(.+'1-.G#1 F
<h:commandButton value="Transformar" action="#{meuBean.transformar}">
<f:ajax render="@all" execute="@form" />
</h:commandButton>
www.algaworks.com 177
 ./ .*D * - *( )* . -$ )9* 0.-  +'1-G#1 @all +- /0'$5-  +7"$)
$)/ $-D %7 ,0 + ).  /0'$5:9* *. (.-).-, ,0 +- $.(*. . -$ (0$/* ($.
-7+$F
RTFYF 7"$) ./-* '):( )/**(%3
- $$*)- %3 ) +7"$)  ./-*  '):( )/* * .$./ ( B)) $-*D
+- $.(*.+ ).0.-/" <f:ajax> )**/9*N'1-NF
<h:commandButton value="Salvar" action="#{cadastroLancamentoBean
.salvar}">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
-  $3-  +7"$) 0( +*0* ($. $)/ -/$1 !$'$/-  1$ *. 0.07-$*.D
$$*)- (*. 0( )*1 !0)$*)'$ ) ./ +7"$)F 0)* * 0.07-$* $)!*-(-
 /  1 )$( )/* * '):( )/*D 0/*(/$( )/ D /-$0$- (*.  / 
+"( )/* *(  ( .( /  1 )$( )/*F  * 0.07-$* +- )# -  / 
+"( )/* )/ .  /  1 )$( )/*D ) ./ .*D )9* $- (*. .0./$/0$- * ,0 %7 !*$
$)!*-(*F
(*. -$- 0( (;/** )*  ) CadastroLancamentoBean +- /-/- * 1 )/* 
(0): / 1 )$( )/*F
public void dataVencimentoAlterada(AjaxBehaviorEvent event) {
if (this.lancamento.getDataPagamento() == null) {
this.lancamento.setDataPagamento(this.lancamento
.getDataVencimento());
}
}
* (+*  )/-  /  1 )$( )/*D $$*)(*. <f:ajax> *( *. /-$0/*.
renderD execute listenerF
<h:outputLabel value="Data de vencimento" />
<h:inputText size="12" value="#{cadastroLancamentoBean.lancamento
.dataVencimento}" label="Data de vencimento">
<f:ajax render="@this dataPagamento"
execute="@this dataPagamento"
listener="#{cadastroLancamentoBean
.dataVencimentoAlterada}" />
<f:convertDateTime pattern="dd/MM/yyyy"
timeZone="America/Sao_Paulo" />
</h:inputText>
<h:outputLabel value="Data de pagamento" />
www.algaworks.com 178
<h:inputText size="12" value="#{cadastroLancamentoBean.lancamento
.dataPagamento}" label="Data de pagamento"
id="dataPagamento">
<f:convertDateTime pattern="dd/MM/yyyy"
timeZone="America/Sao_Paulo" />
</h:inputText>
www.algaworks.com 179
+=/0'*RU
-$(  .
RUFRF
)/-*0:9*
-$(  . ; 0( $'$*/ .  *(+*) )/ . -$*. ( 1 -1 -  .F  .0=/ 
*(+*) )/ . $)'0$ $1 -.*. (+*.  )/-D */@ .D / '.  *.D 7-1*- .D
"-7B*.D$7'*"*.D /F
. *(+*) )/ . * -$(  . +*..0 ( !0)$*)'$  %3 $)/ "-*D . *
) 
 %3 * F - *)#  - /**. *. *(+*) )/ . $.+*)=1 $.D  .. *
,!(0, ( #//+EJJ222F+-$( ! .F*-"J.#*2. JF
www.algaworks.com 180
RUFSF *)B"0-)**+-*% /*
 $'$*/  -$(  . ./7 $.+*)=1 ' +- *2)'* (
#//+EJJ222F+-$( ! .F*-"JF *(* ./(*. 0.)* 1 )D 1(*. + ). $$*)- 
 + )<)$)*-,0$1* )(&61&% *+-*% /*F
<!-- PrimeFaces (biblioteca de componentes) -->
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>5.1</version>
<scope>compile</scope>
</dependency>
-*)/*H 9* +- $.(*. *)B"0-- ($. )F "*-D ; .> *( :-  0.- *.
*(+*) )/ .*-$(  .F
RUFTF 0/+0/  ' 
)+0/ 3/
- 0.- *(+*) )/ . * -$(  .D +- $.(*. $(+*-/-  $'$*/  
*(+*) )/ . ) +7"$)   F
- (*. '/ -- * -,0$1* ,-+( '&'-(61!-&%
*.$./ (B)) $-*F
<!DOCTYPE html>
<ui:composition template="/WEB-INF/template/Layout.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
.- (*.*$.*(+*) )/ .*-$(  .E <p:outputLabel> <p:inputText>F
<p:outputLabel value="Descri‹o" for="descricao" />
<p:inputText id="descricao" size="60"
value="#{cadastroLancamentoBean.lancamento.descricao}" />
 *(+*) )/ <p:outputLabel> ; 0( 3/ ).9* * *(+*) )/  ( .(* )*(
 $'$*/  * F 0)* ..*$(*. .. *(+*) )/  0( <p:inputText>D
)9* +- $.(*. .+ $B- * label )* (+*  )/-D +*$. ; 0/*(/$( )/
/-$0=*F';($..*D --*. 1'$:9*'/ -(*-*/ 3/** %%F
 *(+*) )/ <p:inputText> ; 0( 3/ ).9* * *(+*) )/  ( .(* )*( 
$'$*/  * F  (+* ; - ) -$5* *( 0( ,$"' $! - )/ * *(+*) )/
www.algaworks.com 181
+-9*F ';( $..*D ( .*  --*. ..*$*. * *(+*) )/ D ' B  ./*
+-*0.07-$*F
(&(1.-+/%"78,&%"'-=,">
. 1'$:@ .   ) '$/$*) .9* 3 0/. )* '* * . -1$*-D (.
* -$(  . / ( .0+*-/  1'$:@ . '$ )/G.$ F .* / )# $)/ - .. (
*)#  - *(* $..* !0)$*)D ..$./ 6 1= * 0' "-/0$/ )* '*" 
'"*-&.F
#//+EJJ'*"F'"2*-&.F*(J1'$*G'$ )/G.$ G (G+-*% /*.G%.!G*(G
+-$( ! .J
RUFUF  ' /)  )0
 *(+*) )/ <p:selectOneMenu> ; . * )* *(+*) )/ +-9* * D (.
$)'0$ ,$"''"' D.0+*-/  $:9*D ! $/*.DB'/-* *)/ A*0./*($5*F
www.algaworks.com 182
 (0-(*. + ). * +- B3* h +- pD %7 ./- (*. 0.)* * *(+*) )/ *
-$(  .F
<p:outputLabel value="Pessoa" for="pessoa" />
<p:selectOneMenu value="#{cadastroLancamentoBean.lancamento.pessoa}"
id="pessoa">
<f:selectItem itemLabel="Selecione" noSelectionOption="true" />
<f:selectItems value="#{cadastroLancamentoBean.pessoas}"
var="pessoa" itemValue="#{pessoa}"
itemLabel="#{pessoa.nome}" />
</p:selectOneMenu>
- #$'$/-  !0)$*)'$  B'/-* * *(+*) )/ D ./ /-$0$- true 6
+-*+-$  filterF * (*. /(;( .*'# -  !*-( ,0 ,0 - (*. B'/-- *.
' ( )/*. /-1;.  filterMatchModeD ,0  $/ startsWithD containsD endsWith
customF
<p:outputLabel value="Pessoa" for="pessoa" />
<p:selectOneMenu value="#{cadastroLancamentoBean.lancamento.pessoa}"
id="pessoa" filter="true" filterMatchMode="contains">
<f:selectItem itemLabel="Selecione" noSelectionOption="true" />
<f:selectItems value="#{cadastroLancamentoBean.pessoas}"
var="pessoa" itemValue="#{pessoa}"
itemLabel="#{pessoa.nome}" />
</p:selectOneMenu>
 (*.0(( )0 . ' :9*+-7/$* $)/0$/$1*H
RUFVF  ' /) 0//*)
 -$(  . +*..0$ * *(+*) )/ <p:selectOneRadio>D ,0 ; . * )*
*(+*) )/  ( .(* )*(  $'$*/  * F ( *0/-* *(+*) )/ ,0
www.algaworks.com 183
!0)$*) *(* 0( +"( ; * <p:selectOneButton>F .- (*. ' ) +7"$) 
./-* '):( )/*.+-. ' :9**/$+**'):( )/*F
)/ .  '/ --  +7"$)D 1(*. !5 - 0( + ,0 ) $(+' ( )/:9* )* )0(
TipoLancamentoD+-..*$-(*.0( .-$:9* 0.07-$*+-*)./)/ F
public enum TipoLancamento {
RECEITA("Receita"),
DESPESA("Despesa");
private String descricao;
TipoLancamento(String descricao) {
this.descricao = descricao;
}
public String getDescricao() {
return descricao;
}
}
"*-D $- (*. '/ -- * *(+*) )/  . ' :9* * /$+*  '):( )/* +-
<p:selectOneButton>F
<p:outputLabel value="Tipo" for="tipo" />
<p:selectOneButton id="tipo"
value="#{cadastroLancamentoBean.lancamento.tipo}">
<f:selectItems value="#{cadastroLancamentoBean.tiposLancamentos}"
var="tipoLancamento" itemValue="#{tipoLancamento}"
itemLabel="#{tipoLancamento.descricao}" />
</p:selectOneButton>
 %*- .0'/*E
www.algaworks.com 184
RUFWF ' )-
 <p:calendar> ; 0( *(+*) )/ 0.* +- . ' $*)- /J#*-D *( .0+*-/ 
+"$):9*D'*'$5:9*D%3D /F
(*. '/ -- *. (+*.  /. ) +7"$)  ./-*  '):( )/*. +- 0.-
./ *(+*) )/ F+-*+-$  pattern  B) *!*-(/*/F
<p:outputLabel value="Data de vencimento" for="dataVencimento" />
<p:calendar id="dataVencimento" size="12" pattern="dd/MM/yyyy"
value="#{cadastroLancamentoBean.lancamento.dataVencimento}">
<p:ajax event="dateSelect" update="@this dataPagamento"
process="@this dataPagamento"
listener="#{cadastroLancamentoBean.dataVencimentoAlterada}" />
<p:ajax event="change" update="@this dataPagamento"
process="@this dataPagamento"
listener="#{cadastroLancamentoBean.dataVencimentoAlterada}" />
</p:calendar>
<p:outputLabel value="Data de pagamento" for="dataPagamento" />
<p:calendar size="12" id="dataPagamento" pattern="dd/MM/yyyy"
value="#{cadastroLancamentoBean.lancamento.dataPagamento}" />
 ./ 3 (+'*D 0.(*.  /" <p:ajax> +- #$'$/- %3 +- * +-$( $-*
*(+*) )/  ' )7-$*F ./ /" ; 0( 3/ ).9*  <f:ajax>D  +-$)$+'
$! - ): ; ,0  /" * -$(  . 0. %0 -4 +- !5 - - ,0$.$:@ . %3D ),0)/*
 /" +-9* *  0. 0( $(+' ( )/:9* +->+-$F ';( $..*D  /" <p:ajax> (0
*.)*( . '"0(.+-*+-$  .D*(*;*.* update processF

)'0=(*.  /" <p:ajax> 0. 1 5 . +- $.+-- - ,0$.$:@ . %3 )* 1 )/*


dateSelect changeF
 %*(***(+*) )/  ' )7-$*; 3$$*E
www.algaworks.com 185
RUFXF 0/**(+' /
 *(+*) )/ <p:autoComplete> !*-)  .0" ./@ . ),0)/* * 0.07-$* $"$/ 0(
1'*-+-*(+*F

- (*. '/ -- * (+*   .-$:9*  '):( )/*D +- .0" -$- * 0.07-$* .
 .-$:@ . %7 0.. L./-.M )/ -$*-( )/ F -$( $-( )/ D +- $.- (*. -$-
0()*1*(;/**)*- +*.$/>-$* LancamentosF
public List<String> descricoesQueContem(String descricao) {
TypedQuery<String> query = manager.createQuery(
"select distinct descricao from Lancamento "
+ "where upper(descricao) like upper(:descricao)",
String.class);
query.setParameter("descricao", "%" + descricao + "%");
return query.getResultList();
}
 (;/** descricoesQueContem . ' $*) /*. .  .-$:@ . $./$)/. 
'):( )/*.DB'/-)*+*-0(+-/  .-$:9*F
*  ) CadastroLancamentoBeanD $)'0$- (*. 0( (;/** ,0 . -7 0.* +-
+- )# -..0" ./@ .**(+*) )/ F
@Inject
private Lancamentos lancamentos;
public List<String> pesquisarDescricoes(String descricao) {
return this.lancamentos.descricoesQueContem(descricao);
}
 +7"$)  ./-*D ./ $)'0$-(*. * *(+*) )/ <p:autoComplete> ..*$-
*(;/** pesquisarDescricoesD/-1;.+-*+-$  completeMethodF
<p:outputLabel value="Descri‹o" for="descricao" />
<p:autoComplete id="descricao" size="60"
value="#{cadastroLancamentoBean.lancamento.descricao}"
completeMethod="#{cadastroLancamentoBean.pesquisarDescricoes}" />
"*- * (+*  )/-   .-$:9* * '):( )/* .0" -  .-$:@ . *(+' /.
,0 !*-(0.. (*0/-*.'):( )/*.F
www.algaworks.com 186
RUFYF  .." .
 *(+*) )/ <p:messages> ; 0( 3/ ).9* * *(+*) )/ +-9* * D *( 0(
1$.0'($./- )/  .0+*-/ %3F
<h:form id="frm">
<p:messages showDetail="false" showSummary="true"
autoUpdate="true" />
...
</h:form>

)'0=(*. * /-$0/* autoUpdate *( 1'*- true +- * *(+*) )/ . - /0'$5*


0/*(/$( )/ ,0)* #*01 - 0( - ,0$.$:9* %3D . ( ,0 . % )  ..7-$* '$./-
*$**(+*) )/  ( )." ()*/-$0/* update  <p:ajax>F
www.algaworks.com 187
RUFZF *(()0//*)
 *(+*) )/ <p:commandButton> ; 0( 1 -.9* ($. /- )/ * */9* +-9*
* D ';(  / -  !0)$*)'$  %3 $)/ "-D . ( )  ..$  0.-
<p:ajax>F
<p:commandButton value="Salvar"
action="#{cadastroLancamentoBean.salvar}"
icon="ui-icon-disk" update="@form" />
 +-*+-$  icon +* . - 0. +-  B)$- 0( '..   0( =*) * */9*F
 -$(  . %7 +*..0$ '"0(. '.. .  =*) .D ,0 +* ( . - *).0'/. (
#//+EJJ%,0 -40$F*(J/# ( -*'' -JF
RUFRQF ) '-$
 *(+*) )/ <p:panelGrid> ; 0( 1 -.9* 3/ )$ * *(+*) )/ +-9* * D
*($)'0.9**/ (*-$(  . .0+*-/ ( .'" ( *'0). '$)#.F
<p:panelGrid columns="2">
...
</p:panelGrid>
www.algaworks.com 188
RUFRRF /'
 *(+*) )/ <p:dataTable> ; 0( 1 -.9* 1*'0=  / '  *. +-9* *
D*(.0+*-/ +"$):9*D*- ):9*D. ' :9*DB'/-*. (0$/*($.F
.(*.  /" <p:column> +- .+ $B- . *'0).  / '  *. *
-$(  .F .. /" +*..0$  +-*+-$  headerTextD ,0 -   * / 3/* *
 :'#**'0)F
0- (*.  +7"$)  *).0'/  '):( )/*. * .$./ ( B)) $-* +- 0.-
<p:dataTable>F
<p:dataTable value="#{consultaLancamentosBean.lancamentos}"
var="lancamento" border="1" cellspacing="0"
cellpadding="2">
<p:column headerText="Pessoa">
<h:outputText value="#{lancamento.pessoa.nome}" />
</p:column>
<p:column headerText="Descri‹o">
<h:outputText value="#{lancamento.descricao}" />
</p:column>
<p:column headerText="Tipo">
www.algaworks.com 189
<h:outputText value="#{lancamento.tipo.descricao}" />
</p:column>
<p:column headerText="Valor" style="text-align: right">
<h:outputText value="#{lancamento.valor}">
<f:convertNumber type="currency" locale="pt_BR" />
</h:outputText>
</p:column>
<p:column headerText="Vencimento" style="text-align: center">
<h:outputText value="#{lancamento.dataVencimento}">
<f:convertDateTime pattern="dd/MM/yyyy"
timeZone="America/Sao_Paulo" />
</h:outputText>
</p:column>
<p:column headerText="Pagamento" style="text-align: center">
<h:outputText value="#{lancamento.dataPagamento}">
<f:convertDateTime pattern="dd/MM/yyyy"
timeZone="America/Sao_Paulo" />
</h:outputText>
</p:column>
</p:dataTable>
 %*- .0'/*E
$$*)- (*. * - 0-.*  +"$):9* ) / '  *.D /-1;. . +-*+-$  .
paginatorD paginatorPosition rowsF $'$/- (*. /(;(  *- ):9* +*-
'"0(.*'0).D .+ $B)*+-*+-$  sortBy  <p:column>F
www.algaworks.com 190
<p:dataTable value="#{consultaLancamentosBean.lancamentos}"
var="lancamento" border="1" cellspacing="0"
cellpadding="2" paginator="true" rows="5"
paginatorPosition="bottom">
<p:column headerText="Pessoa" sortBy="#{lancamento.pessoa.nome}">
<h:outputText value="#{lancamento.pessoa.nome}" />
</p:column>
<p:column headerText="Descri‹o" sortBy="#{lancamento.descricao}">
<h:outputText value="#{lancamento.descricao}" />
</p:column>
<p:column headerText="Tipo">
<h:outputText value="#{lancamento.tipo.descricao}" />
</p:column>
<p:column headerText="Valor" style="text-align: right"
sortBy="#{lancamento.valor}">
<h:outputText value="#{lancamento.valor}">
<f:convertNumber type="currency" locale="pt_BR" />
</h:outputText>
</p:column>
<p:column headerText="Vencimento" style="text-align: center"
sortBy="#{lancamento.dataVencimento}">
<h:outputText value="#{lancamento.dataVencimento}">
<f:convertDateTime pattern="dd/MM/yyyy"
timeZone="America/Sao_Paulo" />
</h:outputText>
</p:column>
<p:column headerText="Pagamento" style="text-align: center"
sortBy="#{lancamento.dataPagamento}">
<h:outputText value="#{lancamento.dataPagamento}">
<f:convertDateTime pattern="dd/MM/yyyy"
timeZone="America/Sao_Paulo" />
</h:outputText>
</p:column>
</p:dataTable>
 (*.0(/ ' *.*(+"$):9* *- ):9*F
www.algaworks.com 191
(&((',.%-+(,+ ",-+(,,)4 "','('((,)(+
&'>
 *(+*) )/ <p:dataTable> * -$(  . +*..0$ * - 0-.*  +"$):9*
 *.D (. (*- +*.. +-  - ,0 3$$- *. *. +"$)*. +- *
0.07-$* . % .0B$ )/ D ,0)* /-'#(*. *( "-) . *)%0)/*.  *.D
)9*; (..$(H
..$./61= *0'"-/0$/,0  ).$)*(*!5 -+"$):9* *.*(*
*(+*) )/ /' *-$(  .0.)**- 0-.*  54 *$)"F
#//+EJJ'*"F'"2*-&.F*(J+"$)*G G//' G*G+-$( ! .G*(G'54G
'*$)"J
RUFRSF  )0-
 *(+*) )/ <p:menubar> - ) -$5 0( --  ( )0 #*-$5*)/'D +- $ *(
.--. (.$./ (. .&/*+F

)'0$- (*.0(-- ( )0)*-,0$1* 2(.-61!-&% *.$./ (B)) $-*F


www.algaworks.com 192
<h:body>
...
<h:form>
<p:menubar style="margin-top: -20px; margin-bottom: 20px">
<p:submenu label="Cadastros">
<p:menuitem value="Pessoa" />
<p:menuitem value="Lanamento"
action="/CadastroLancamento?faces-redirect=true" />
</p:submenu>
<p:submenu label="Consultas">
<p:menuitem value="Pessoas" />
<p:menuitem value="Lanamentos"
action="/ConsultaLancamentos?faces-redirect=true" />
</p:submenu>
<p:submenu label="Sistema">
<p:menuitem value="Sair" />
</p:submenu>
</p:menubar>
</h:form>
...
</h:body>
 % ,0 D +- 0.- 0( <p:menubar>D /$1 (*. ,0 0.- *0/-*. *(+*) )/ . 03$'$- .D
*(* <p:submenu> <p:menuitem>F
www.algaworks.com 193
RUFRTF %3//0.
 *(+*) )/ <p:ajaxStatus> ; 0( $)$*- "'*'  - ,0$.$:@ . %3F * (*.D
+*- 3 (+'*D $$*)- 0( $(" ( ,0 $)$ ,0 '"* ./7 . )* -- "*D +-
,0 *0.07-$* .+ - *+-* ..( )/*+7"$)F
*-,0$1* 2(.-61!-&% *.$./ (B)) $-*D$)'0$- (*.*. "0$)/ >$"*E
<p:ajaxStatus styleClass="ajax-status">
<f:facet name="start">
<h:graphicImage library="algaworks" name="loading.gif" />
</f:facet>
<f:facet name="complete">
<h:outputText value="" />
</f:facet>
</p:ajaxStatus>
 - start  B) * ,0 . -7 3$$* ,0)* 0( - ,0$.$:9* %3 !*- $)$$D
* - complete .0./$/0$ * *)/ A* * *(+*) )/ ,0)*  - ,0$.$:9* %3 !*-
) --F
.(*. 0( '..  #( ajax-status +- +*.$$*)- * *(+*) )/ ) / 'D
+*-$..*D+- $.(*.$)'0$-*>$"*$3*)*-,0$1* ,-"%(6,,F
.ajax-status {
position: fixed;
top: 85px;
right: 10px;
width: 35px;
height: 35px
}
RUFRUF -*"-()*'/ -:9* '):( )/*.
-  $3- * .$./ ( B)) $-* 0( +*0* ($. *(+' /*D $(+' ( )/- (*.  $:9*
 '):( )/*.) ./. :9*F
* - +*.$/>-$* LancamentosD -$- (*. 0( (;/** guardarD ,0 . -1$-7 /)/* +-
$). -$- *(* /0'$5- *% /*.D *0/-* porIdD ,0 - /*-) 0( '):( )/* + '*
$ )/$B*-F
public Lancamento porId(Long id) {
return manager.find(Lancamento.class, id);
}
www.algaworks.com 194
public Lancamento guardar(Lancamento lancamento) {
return this.manager.merge(lancamento);
}
 '.. CadastroLancamentosD #(- (*. * (;/** guardarD )9* ($.
adicionarF
public void salvar(Lancamento lancamento) throws NegocioException {
...
this.lancamentos.guardar(lancamento);
}
- $.- (*. 0(*)1 -.*- '):( )/*.D+*-$..*D%71(*. $37G'*+-*)/*F
@FacesConverter(forClass = Lancamento.class)
public class LancamentosConverter implements Converter {
@Inject
private Lancamentos lancamentos;
@Override
public Object getAsObject(FacesContext context,
UIComponent component, String value) {
Lancamento retorno = null;
if (value != null && !"".equals(value)) {
retorno = this.lancamentos.porId(new Long(value));
}
return retorno;
}
@Override
public String getAsString(FacesContext context,
UIComponent component, Object value) {
if (value != null) {
Lancamento lancamento = ((Lancamento) value);
return lancamento.getId() == null ? null
: lancamento.getId().toString();
}
return null;
}
}
 / '  *.  +7"$) (',.%- '&'-(,61!-&%D $$*)- (*. 0(
*'0) *( 0( */9*  $:9*F .(*. * *(+*) )/ <p:button>D ,0 -  
0( (.-(& +- $- $*)- * 0.A-$* +-  +7"$)  ./-*  '):( )/*F
..- (*. *(* +-8( /-* * $ * '):( )/*D +*$. 1(*. +- $.-  ..
www.algaworks.com 195
$)!*-(:9* +- 3$$-  +7"$)  ./-* +- )#$ *( . $)!*-(:@ . *
'):( )/*. ' $*)*F
<p:column>
<p:button icon="ui-icon-pencil" title="Editar"
outcome="/CadastroLancamento">
<f:param name="id" value="#{lancamento.id}" />
</p:button>
</p:column>
.- (*. 0( $'$*/   0/$'$/7-$*. +- D #( ()$ .F  -/ !/* %7
./7)*-,0$1* )(&61&% *)*..*+-*% /*F
 +7"$) ,-+( '&'-(61!-&%D $(+*-/(*.  $'$*/  * ()$ .
$)'0=(*. <o:viewParam> ( <f:metadata>F
<!DOCTYPE html>
<ui:composition template="/WEB-INF/template/Layout.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:o="http://omnifaces.org/ui">
<f:metadata>
<o:viewParam name="id"
value="#{cadastroLancamentoBean.lancamento}" />
<f:viewAction
action="#{cadastroLancamentoBean.prepararCadastro}" />
</f:metadata>
 /" <o:viewParam> * ()$ . -   -7 * $ * '):( )/* *(* +-8( /-* )
*.+2,-+"' /-$0$-7 6 1-$71 ' lancamento *  )F  *)1 -.*-  '):( )/*.
)/--7 (:9*) ./ (*( )/*F
 $! - ): )/- <f:viewParam> <o:viewParam>D ; ,0  /" * ()$ . )9*
#( * ,--+  +-*+-$  ( )(,-$D $..* ; $(+*-/)/ +- * *% /* )9* . -
.0./$/0=*)*( $* 0( $:9*F
*  ) CadastroLancamentoBeanD '/ -(*. * >$"* * (;/**
prepararCadastroD - ! - )$* + ' /" <f:viewAction>D +- $)./)$- 0( )*1*
Lancamento ,0)* '  ./$1 -)0'*F
public void prepararCadastro() {
this.todasPessoas = this.pessoas.todas();
if (this.lancamento == null) {
www.algaworks.com 196
this.lancamento = new Lancamento();
}
}
 (*. $:9* '):( )/*.!0)$*))*H
RUFRVF -*"-()* 3'0.9* '):( )/*.
$$*)- (*.  !0)$*)'$  3'0.9*  '):( )/*. ) +7"$)  *).0'/ 
'):( )/*.*.$./ (B)) $-*F
* - +*.$/>-$*  '):( )/*.D 1(*. $$*)- 0( )*1* (;/** +- - (*1 -
'):( )/*.F
public void remover(Lancamento lancamento) {
this.manager.remove(lancamento);
}
 '.. CadastroLancamentosD $(+' ( )/- (*. . - "-.  ) ">$* +- 3'0$-
'):( )/*.)*(;/** excluirF
@Transactional
public void excluir(Lancamento lancamento) throws NegocioException {
lancamento = this.lancamentos.porId(lancamento.getId());
www.algaworks.com 197
if (lancamento.getDataPagamento() != null) {
throw new NegocioException(
"N‹o Ž poss’vel excluir um lanamento pago!");
}
this.lancamentos.remover(lancamento);
}
* ()"   ) ConsultaLancamentosBeanD $$*)(*. 0( (;/** excluir
0( 1-$71 '  $)./8)$ lancamentoSelecionadoF ./ 1-$71 ' . -7 /-$0= + '
+7"$)D,0)**0.07-$*. ' $*)-*'):( )/*,0  . % 3'0$-F
public class ConsultaLancamentosBean implements Serializable {
...
private Lancamento lancamentoSelecionado;
public void excluir() {
FacesContext context = FacesContext.getCurrentInstance();
try {
this.cadastro.excluir(this.lancamentoSelecionado);
this.consultar();
context.addMessage(null, new FacesMessage(
"Lanamento exclu’do com sucesso!"));
} catch (NegocioException e) {
FacesMessage mensagem = new FacesMessage(e.getMessage());
mensagem.setSeverity(FacesMessage.SEVERITY_ERROR);
context.addMessage(null, mensagem);
}
}
...
}
- $.- (*. $$*)- * *(+*) )/ <h:messages> ) +7"$)
(',.%- '&'-(,61!-&%D +- -(*. ! & +- * 0.07-$* .*-  .*'$$/:9*
  3'0.9* '):( )/*.F
<h:form id="frm">
<p:messages showDetail="false" showSummary="true"
autoUpdate="true" />
...
</h:form>
www.algaworks.com 198
 A'/$( *'0)  / '  *.D $$*)(*. 0( */9*  *()* ,0 $)1* *
(;/** excluir * ()"   )F .(*.  /" <f:setPropertyActionListener>D
,0 ; 0( -"(' %",-'+ ,0 /-$0$ 0( *% /* .+ $B* ( value ( 0(  ./$)*
.+ $B* ( targetF -"(' %",-'+, .9* 3 0/*. )/ .  -"(',D +*-/)/*D
,0)* * (;/** excluir !*- #(*D * '):( )/* . ' $*)* %7 ./-7 /-$0=*
)1-$71 ' $)./8)$*()"  )F
<p:commandButton icon="ui-icon-trash" title="Excluir" process="@this"
update="@form" action="#{consultaLancamentosBean.excluir}">
<f:setPropertyActionListener value="#{lancamento}"
target="#{consultaLancamentosBean.lancamentoSelecionado}" />
</p:commandButton>
-*)/*H 7+* (*. 3'0$-'):( )/*.+-/$-/ ' *).0'/F
www.algaworks.com 199
+=/0'*RV
 "0-):+'$:9*
RVFRF .*'# )*0(.*'0:9*
3$./ ( $1 -.. () $-.  $(+' ( )/- 0/ )/$:9* 0/*-$5:9* ( .$./ (.
D/$.*(*E
K %&,  . "0-):**)/$) -
K -( 2*-&. . "0-):D*(*+-$)" 0-$/4
K )0'( )/ D*(-$:9* B'/-*.. -1' /
.*'# - (*.A'/$(*+:9*D,0 ;($..$(+' .F
RVFSF *"$)
(*. -$- 0( ( )$.(*  '*"$) .$(+' .D (. ,0 +* . - 0.* ( .$./ (.
- $.F 9* 0.- (*. 0.07-$*. . )#.  0( )*  *.D (.  $3- (*.
!+(F '-* ,0 1*<  1 0.- *. *.  '"0( *0/-* '0"-D +*- 3 (+'*
0.)* D (. ) ./ +=/0'* !*- (*. ( 0( .*'0:9*  0/ )/$:9*
0/*-$5:9*F
-$- (*.0('.. Usuario ,0 - +- . )/-70(0.07-$**.$./ (F
package com.algaworks.financeiro.controller;
// imports
@Named
@SessionScoped
public class Usuario implements Serializable {
www.algaworks.com 200
private static final long serialVersionUID = 1L;
private String nome;
private Date dataLogin;
public boolean isLogado() {
return nome != null;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Date getDataLogin() {
return dataLogin;
}
public void setDataLogin(Date dataLogin) {
this.dataLogin = dataLogin;
}
}
"*-D -$- (*. 0( ()"   ) ,0 . -7 0.* + ' +7"$)  '*"$)F ./  )
$)% / Usuario /-$0$'"0(.1-$71 $.,0)*0/ )/$:9***-- *(.0 ..*F
package com.algaworks.financeiro.controller;
// imports
@Named
@RequestScoped
public class LoginBean {
@Inject
private Usuario usuario;
private String nomeUsuario;
private String senha;
public String login() {
FacesContext context = FacesContext.getCurrentInstance();
if ("admin".equals(this.nomeUsuario)
&& "123".equals(this.senha)) {
this.usuario.setNome(this.nomeUsuario);
this.usuario.setDataLogin(new Date());
return "/ConsultaLancamentos?faces-redirect=true";
www.algaworks.com 201
} else {
FacesMessage mensagem = new FacesMessage(
"Usu‡rio/senha inv‡lidos!");
mensagem.setSeverity(FacesMessage.SEVERITY_ERROR);
context.addMessage(null, mensagem);
}
return null;
}
public String getNomeUsuario() {
return nomeUsuario;
}
public void setNomeUsuario(String nomeUsuario) {
this.nomeUsuario = nomeUsuario;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
}
-$(*.0(+7"$)#( ( "'61!-&%D*(*. "0$)/ >$"*E
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Login</title>
<h:outputStylesheet library="algaworks" name="estilo.css" />
</h:head>
<h:body>
<div id="login-dialog">
<h1>Login</h1>
<h:form id="frm">
<p:messages autoUpdate="true" />
<h:panelGrid columns="2" styleClass="grid-login">
<p:outputLabel value="Usu‡rio" for="usuario" />
<p:inputText id="usuario" size="20"
value="#{loginBean.nomeUsuario}" />
<p:outputLabel value="Senha" for="senha" />
www.algaworks.com 202
<p:password id="senha" size="20"
value="#{loginBean.senha}" />
<p:outputLabel />
<p:commandButton value="Acessar"
action="#{loginBean.login}" />
</h:panelGrid>
</h:form>
</div>
</h:body>
</html>
 +7"$)  '*"$) 0. '"0(. '.. .  +-  $3- * !*-(0'7-$* ($. /- )/ F
- $.(*.$)'0$-*>$"*$3*)*-,0$1* ,-"%(6,,F
#login-dialog {
width: 260px;
margin: auto;
margin-top: 150px;
}
.grid-login {
background-color: #f2f2f2;
border-radius: 8px;
border: 1px solid #ccc;
margin-top: 8px;
padding: 10px;
width: 100%
}
* -,0$1* 2(.-61!-&%D $)'0=(*. 0( ( )." ( *( * )*( * 0.07-$* '*"*
)* :'#*F
<header>
<h:graphicImage library="algaworks" name="logo.png" />
<div style="float: right; margin-right: 20px; margin-top: 8px">
Ol‡ #{usuario.nome}!
</div>
</header>
7+* (*. ..-+7"$) '*"$)$- /( )/  '*"-*(*0.07-$*N($)NF
www.algaworks.com 203
RVFTF *"*0/
- !5 - '*"*0/ * .$./ (D ./ $)1'$-(*.  . ..9* * 0.07-$*F -$- (*. 0(
(;/** logout )*()"  ) LoginBean +-!5 -$..*F
public String logout() {
FacesContext.getCurrentInstance().getExternalContext()
.invalidateSession();
return "/Login?faces-redirect=true";
}
./ (;/**;#(*)*$/ ( ( )0N$-ND,0 B)*-,0$1* 2(.-61!-&%F
<p:submenu label="Sistema">
<p:menuitem value="Sair" action="#{loginBean.logout}" />
</p:submenu>
www.algaworks.com 204
RVFUF $'/-* 0/*-$5:9*
 0.07-$* %7 +* !5 - '*"$) '*"*0/ )* .$./ (D (. . +7"$). $) ./9*  -/.D
*0. %D. *0.07-$*.*0 -. ..+7"$).D$))9*'*,0 - (*.* ..*F
- !5 -  0/*-$5:9*  0.07-$*.D -$- (*. 0( B'/-* . -1' /D ,0 - $- $*) *
0.07-$* +-  +7"$)  '*"$) ,0)* * ( .(* )9* ; - *)# $* *(* 0( 0.07-$*
'*"*F
package com.algaworks.financeiro.filter;
// imports...
@WebFilter("*.xhtml")
public class AutorizacaoFilter implements Filter {
@Inject
private Usuario usuario;
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
if (!usuario.isLogado()
&& !request.getRequestURI().endsWith("/Login.xhtml")
&& !request.getRequestURI()
.contains("/javax.faces.resource/")) {
response.sendRedirect(request.getContextPath()
+ "/Login.xhtml");
} else {
chain.doFilter(req, res);
}
}
@Override
public void init(FilterConfig config) throws ServletException {
}
@Override
public void destroy() {
}
}
.$./ ( . "0-): ./7!0)$*))**(+' /( )/ H
www.algaworks.com 205
+=/0'*RW
*)'0.9*
0)9*$.. ,0 . -$!7$'FFF(. ./*0! '$5,0 1*<*)'0$0' $/0- .. '$1-*F
.+ -* ,0 / )# *'** ( +-7/$ /0* ,0 +- ) 0F  1*< + ). ' - ..
'$1-* )9* +-/$-D + .,0$.- ,0 --   : *( *. +-*' (. ,0 .0-" (D (
'"0(.. ().1*<%71$/ - .,0 $*"-) +-/ **)/ A*F
9* 3$./ ($'"- F*<+- $.*'*-(9*)(..H
 1*< "*./*0  .. '$1-*D +*- !1*-D ( %0  ()/ - .. /-'#*F  *( ) ..
'$1-* +- . 0. ($"*.  /-'#*D !0' J*0 *(+-/$'# )*  **& 2$// -F
RWFRF ->3$(*.+..*.
(*- 0 / )# (  $* ./)/ +- .- 1 - .. '$1-*D * *)/ A* ,0 1*<
+- ) 0) ' ;.>+*)/*$  -"H
 '-* ,0 1*< )9* + - 0 / (+* *( * ,0 *0  ./0-D * ,0 0 ,0 -* $5 -
;,0 #7(0$/*($.*$..+-+-*!0)-F
.* 1*< / )# $)/ - .. ( ( -"0'#- !0)* ( *)/ A*. $) ($. 1):*.D
- *( )* ,0 1*< < 0( *'# ( )*..*. 0-.*. *)'$) D +-$)$+'( )/ .. .
*$.E
K  . )1*'1$( )/* *( S
#//+EJJ222F'"2*-&.F*(J0-.*J . )1*'1$( )/*G2 G*(G%.!GSJ
www.algaworks.com 206
K $./ (.*( -$$. 1*(
D  -$(  .
#//+EJJ222F'"2*-&.F*(J0-.*J.$./ (.G*( -$$.G%1G G*(G$G%+G
G+-$( ! .J
*( .. .*$.0-.*.%0)/*.D1*<1$+- ) -+..*+..*E
K 0)( )/*.   L$'*  1$D )1 ":9*D 1'$*- .D *)1 -.*- .D /M
K -@ .+ ) ..$*)
)$ 2  +*.$/*-4
K %3*(  -$(  .
K
)/ -)$*)'$5:9*
K -$:9* *(+*) )/ .0./*($5*.
K  "0-):*(  +-$)" 0-$/4
K -*/ :9* +7"$). *(+*) )/ .
K  +'*4 +'$:@ .))01 (
K  V-$ )'4-&0+
K  .*0-  $--4*)/-/.
K +'* -,0$1*.*($' +'*
K 0$/*($.*(+*) )/ .1):*.*-$(  .
K 
L .*+*.D+*)/*. $)% :9*D,0'$B*- .D+-*0/*- .D /M
K -/( )/*  3 :@ .
K *""$)" ( )." ).*( *"U%
K .*  )'$/$*) -$:9* - ./-$:@ .0./*($5.
K
(+' ( )/:9* +7"$).( ./- G /'#
K )1$*  G($'
K +#  '*$/4 +- " -:9*  *)/ A*  G($'  +-/$-  0( / (+'/
K -7B*.*(-$(  .
K  '/>-$*.*( .+ - +*-/.
K **' *) 3@ .*(TQ
K .* $(+' ( )/:9* 1'$:@ .'$ )/G.$
K (0$/*($.H
www.algaworks.com 207
';(  /0* ,0 1*< 1$ +- ) -D )>. $) *! -  (*. .0+*-/ +- . .0.
A1$.H
www.algaworks.com 208