Jsf Best Practices Javaserver Faces Session Tips

download Jsf Best Practices Javaserver Faces Session Tips

of 8

Transcript of Jsf Best Practices Javaserver Faces Session Tips

  • 8/17/2019 Jsf Best Practices Javaserver Faces Session Tips

    1/8

    AUTHOR

    Thomas Asel

    Orient at ion in Object s GmbH

    P u b l is h e d : 1 1 . 3. 2 0 11

    ABSTRACT

    W h e n s t ar t i n g w i t h J S F o ne o f t h e c o m mo n p i t f al l s i s h o w t o p a s s v a l ue s o r p a r a me t e rs

    effic iently. In many c ases developers end up putting M anaged Beans in Session Sc ope

    t o s h a r e B e a n a t tr i b u te s t h o ug h m o r e a p p ro p r i at e s o l u ti o n s a r e a v a i l ab l e . T h i s a rt i c l e

    lists some routine tasks for JSF developers and gives tips and tric ks on how to pass

    v a l u e s e ff i c i en t l y w i t ho u t h a v i ng t o p o l l u te t h e s e s si o n - ob j e ct . T h e i n t e nd e d a u di e n c e

     for this artic le are novic e JSF developers as w ell as developers that are interested in

    alternative solutions to common problems.

    J av a, XM L, UM L, XS LT , O pe n S ou rc e, J Bo ss , S OA P, CV S, S pr in g, J SF , E cl ip se

    O r i en t a ti o n i n O b je c t s G m bH

    W e in h ei m er S t r. 6 8

    D-68309 Mannheim

    T el . + 49 (0) 6 21 - 7 18 39 - 0

    F ax + 49 (0) 6 21 - 7 18 39 - 5 0

    www .oio.de [email protected]

    JSF Best PracticesS c o pe m a n ag e m en t fo r c l e an s es s i on s

    ) S ch ul un g )

    ) B er at un g )

    ) E nt wi ck lu ng )

    )

          A    r     t 

          i      k    e 

          l)

    mailto:[email protected]://www.oio.de/

  • 8/17/2019 Jsf Best Practices Javaserver Faces Session Tips

    2/8

    S E SS I ON P O LL U TI O N AN D W H Y IT I S

    HAZARDOUS

    A d mi t t e d ly , p u t t i ng a l l y o u r M a n a ge d B e a ns i n S e s s i o n S co p e m a y

    b e a w o r ki n g s o l u ti o n f o r s o m e p r o bl e m s e n c ou n t e re d w h i l e

    developing w eb applic ations w ith JSF . But it bears a high potential

     for undesired behavior, w hic h is mostly not notic ed until realproblems arise. Problems c aused by w rong Bean Sc opes are often

    h a rd t o t r ac e a n d r e qu i re p r of o un d k n ow l ed g e o f t h e J S F f r a m ew o rk

    (and the fundamental princ iples of w eb applic ations in general).

    This is w hy partic ularly developers new to JSF get easily in trouble

    by exc essively using Session Sc oped Beans.

    T h e re a r e a f ew s im p l e q u e st i o n s t o co n s i de r w h e n d e a l in g w i t h

    Session Sc ope:

    1. Is t he inst ant iat ed object r equir ed available t hr oug hout

    t he ent ir e user session?

    T h e s e s s io n s p a n s o v er a l l r e q u es t s a n d m a y i n v o lv e

    t r av e rs i ng o v er y o ur w ho l e a p pl i ca t io n .

    2. I s y o u r c o d e t h r e ad - s a fe ?

    The Session objec t is shared among all threads initiatedb y t h e s a m e u s e r. T h i s ma y c a u se p r o bl e m s wh e n o bj e c t s

    a r e a c c es s e d c o n cu r r en t l y e . g . w h e n t h e u s e r o p e ns a

    s e c on d b r o ws e r t a b o f t h e a p p l ic a t i o n. C o n cu r r en c y

    p r o bl e m s a r e h a r d t o d i s c o ve r , p a r t ic u l l a rl y i n t h e

    p r e se n t a t io n t ie r w h e re u n i t t e s t i ng m a y be c o m pl i c a t ed ,

    d u e t o t e c hn i c a l d if f i c ul t i e s o r, i n s o m e c a s e s, s i m p ly i s n o t

    possible.

    3. How does a lar g er session object affect y our sy st ems

    per for mance?

    This question c an often only be answ ered by observing

    t h e s y st e m u n de r l o ad . U n fo r tu n at e ly , p e rf o rm a nc e

    p r ob l em s a r e o f t e n p e r ce i ve d v er y l a te : W he n t h e s o f t wa r e

    is already deployed and performs poor in the produc tion

    e n v i ro n m en t . A t l e a s t , k e e p i n m i nd t h a t h e a v ie r s e s s io no b j e ct s r e q u i r e m o r e p h y s ic a l m e m o ry .

    4. H o w d o e s s e s si o n - wi d e a c c e ss i b i li t y o f M a n ag e d B e a n s

    affect y our soft war e ar chit ect ur e?

    C o n si d e r a r c h it e c t ur a l p r i n c i pl e s l i k e l o o se c o u pl i n g ,

    i n f o rm a t i on h i d i n g , e t c . I s y o u r i n t e nd e d a r c hi t e c t ur e

    breac hed by exposing a c ertain objec t throughout a

    session? Even w hen you are frequently evaluating your

    implementation against your intended arc hitec ture (w hic h

    is highly rec ommended to do!), sc opes are a rather

    t e c h ni c a l a s p ec t . T h i s s t i p ul a t e s f o r y o u r a n a l y si s t oo l s to

    understand these tec hnic al aspec ts by evaluating

    proprietary annotations, metadata and c onfiguration

    artifac ts.

    W i t h t h e se c o n si d e ra t i on s a b o ut s e s si o n p o l lu t i on a n d t h e r e su l t i ng

    risks let’ s take a look at c ommon sc enarios in JSF development.

    T h e f o l l ow i n g s e c t io n s d i s c u ss t h r e e s c e na r i o s c o m mo n l y

    enc ountered in almost every w eb applic ation.

    P A S S IN G I N F O R M AT I O N T O H A N D LE R S

    C o n si d e r a s i m p l e v i e w r el a t e d t o a s i n g le M a n a ge d B e a n. F u r th e r ,

    think about a C ommand Button triggering an ac tion method. How

    c an the ac tion method know about the c ommand button it w as

    t r i g ge r e d f r om ? A n d e v e n m o r e i n t e re s t i n g: H o w c a n t h e c o n t ex t

    in w hic h the button w as pressed also be passed on to the method?A s i m p le e x a m pl e :

    F ig ur e 1: Ex ample 1

    R e g a rd l e s s wh i c h o ne o f t h e h : c om m a nd B u t to n s i s p re s s ed , t h e y

    a r e a l l t r i gg e ri n g t h e s a me a c ti o n m e th o d. T h e m e th o d c on t ai n s

    s o m e l o g i c t o pr o c es s th e r e s pe c t i ve s e t o f d a t a ( d i s pl a y e d i n th e

    tables row s). But how exac tly does the method know w hic h set ofd a t a i t i s t o p r oc e s s? L e t s t a k e a l o o k a t t h e v i e w d ef i n i t io n f o r t h i s

    example:

    ...

    ...

    E x a mp l e 1 : V i e w D e f i ni t i o n f o r E x a mp l e 1

    This is a standard task and enc ountered in almost every

    w e b -a p p l ic a t i o n. T h e f o l l ow i n g s e c ti o n s d i s c us s s o m e a p p ro a c h es

    t o s o l v e t h e p r o bl e m d e s cr i b ed a bo v e .

    J SF Best Pr act ices© Orientation in Objec ts G mbH

    S ei te 2 v on 8www.oio.de i nfo@oi o.de

    mailto:[email protected]://www.oio.de/

  • 8/17/2019 Jsf Best Practices Javaserver Faces Session Tips

    3/8

    SETTING A PROPERTY ON TH E MANAGED BEAN

    A n easy solution is to simply fill a c ertain property in the managed

    b e an . Y o ur M a na g ed B e an m a y c o nt a i n a p r op e rt y n a me d " s el e ct e d"

    t o h o l d th e s e t o f d a t a t h at w a s s e le c t e d b y t h e u s e r w h e n cl i c k i ng

    t h e h : c om m a n dB u t t on . S i n c e J S F 1 . 2 a t a g i s o ff e r ed t o

    c onveniently set a property in a c orresponding M anaged Bean

    c a l le d f : se t P ro p er t y Ac t i on L i st e n er . D o n’ t g e t c o nf u s ed b y t h e b u l kyt a g n a m e a s i t de s c ri b e s t h e t a g s pu r p os e p r e t ty w e ll . W h e n u s i n g

    t h e f : s e tP r o pe r t y Ac t i o nL i s t en e r w i t h in a c o m po n e nt , a n A c t i on

    L i s t en e r i s c r e at e d b y t he f r a m ew o r k. T h i s A ct i o n L i st e n e r f i l l s t he

    t a r g et e d v a lu e o f t h e M a n a ge d B e an w i t h a g iv e n v a lu e . T h e a b o v e

    e x a mp l e u s i n g a n f : s et P r o pe r t yA c t i on L i s te n e r l o o k s li k e t h i s :

    ...

    ...

    E x am p l e 2 : V i e w D e f i n it i o n f o r E x am p l e 1 u s i n g

    f:set Pr oper t y Act ionList ener

    W i t h t h e i m p l ic i t l y c r e at e d A c t i on L i s t e n er s f i l l i n g t h e p r o pe r t y i n

    t h e M a n a ge d B e a n, a l l y o u n e ed t o d o t o p r ov i d e a v a i l a bi l i t y o f t h e

    selec ted data in the A c tion M ethod is to simply ac c ess the beans

    "selec ted" property:

    ...

    public String delete(){

    addresses.remove(selected);

    return "";

    }

    ...

    E x am p l e 3 : A ct i o n M e t h od i n B a c k i ng B e an f o r E x a mp l e 1

    A l th o ug h v e ry s i mp l e a n d h a n dy t h i s s o lu t i on h a s s o me d r aw b ac k s:

    • S el ec te d v al ue s n ee d t o b e a ll of t he s am e t yp e

    • W it h l ot s o f d if fe re nt s el ec ti on s o n t he s am e p ag e, y ou r

    M anaged Bean may grow quic kly, resulting in reduced

    c o h es i o n a n d t h u s , m o r e i m p o rt a n t , p o o r m a i n t ai n a b il i t y .

    H o we v e r, u n l e ss y o u r v i e w is n ’ t v er y c o mp l e x a n d t he r e fo r e a

    s i n g le s e l e ct i o n i s a l l y o u n e e d, t h i s m a y be t h e m e t h od o f c h o i ce

     for you.

    A D D I NG PA R A M ET E R S T O T H E C O M M AN D

    COMPONENT

    T h e f o l l ow i n g a p p r oa c h e l i m in a t e s t h e d r a wb a c k s o f t h e p r i o r o n e ,

    b y i n t r o du c i n g p a r a me t e rs t o t h e C o m ma n d C o m po n e nt . W h i l e

     f:setPropertyAc tionListener requires to be plac ed (at least

    s o me w he r e) w i t hi n a c o mp o ne n t d e ri v ed f r om A c ti o nS o ur c e, e v er y

    c o m po n e nt d e r i v e d f r om U I C om p o ne n t ( w h i ch i s t r u e f o r e v e ry

    c o m po n e nt r e p re s e nt e d b y a t a g i n v i e ws ) i s c a pa b l e o f c a r ry i n g

    p a r a me t e rs . B y a d d in g t h e s e l e ct e d d at a s e t a s p a r a me t e r t o t h e

    h : c om m a n dB u t t o n, t h i s p a ra m e t er i s a v a i l ab l e w h e ne v e r y o u a r e

    d e a li n g w i t h t h i s c o mp o n en t . C o mp a r ed t o A c t io n M e th o d s, A c t io n

    L i s t en e r s a r e a w a r e o f t h e A c t i on E v en t t ha t t ri g g e re d t h e a c t i on .

    A c t io n E ve n t s r e f er t o t he c o m po n e nt t r ig g e r in g t h e e v e nt , i n t hi s

    c a s e t h e h : c om m a n dB u t t o n. H a v i ng A c c es s t o t h e c o m po n e nt , a l l

    parameters are ac c essible, so all you need to do is to c all the

    c omponents getC hildren() method to ac c ess the nested

    c omponents – inc luding the nested parameters.

    ...

    ...

    Ex ample 4: View Definit ion for Ex ample 2 using f:par am

    ...

    public void delete(ActionEvent event){

    for(UIComponent component :

    event.getComponent().getChildren()){

    if( component instanceof UIParameter ){

    UIParameter param = (UIParameter) component;

    if(param.getName().equals("selected")){

    addresses.remove(param.getValue());

    }

    }

    }

    }

    ...

    Ex ample 5: Act ionList ener in Backing Bean for Example 2

    A s y o u c an s e e , t h e A c t i on M e t h od f r o m t h e p r e vi o u s e xa m p l e h a s

    c h a n ge d to a n A c t i on L i st e n er . P l e a se n o t e t h a t si n c e t h e

    h : c om m a nd B u t t on m a y c o n t a i n m u l t i p l e p a r a me t e rs , t h e L i s t en e r

    i s r es p on s ib l e o f c h ec k in g f o r t h e p a ra m et e r n a me t o a v oi d

    e v a l ua t i o n o f w r o ng p a r a me t e rs .

    T h e m a j o r d r a wb a c k o f t h i s s o lu t i o n i s t ha t i t i s c o d e i n t e ns i v e . Y o u

    h a v e t o ac c e ss t he c o m po n e nt a nd n ee d to wr i t e s o m e l o g i c t o

    traverse its c hildren. Keep in mind that c omponents may also have

     further c hild c omponents besides f:param, therefore you also need

    t o c he ck f or t h e c o r re ct t y pe o f t h e n e s te d c hi l d c o m po ne n ts . W he n

    d e a li n g w i t h m u lt i p l e p a r a m e te r s y o u a l s o n e ed t o d i s ti n g ui s h t h e m

    b y n a m e , w h i ch r e q u i r es a d d i t i o na l c o d e . S i n c e A c t i on L i s t e n er s

    a r e u s e d qu i t e f r e qu e n t ly , t h i s a pp r o a ch c a n of t e n be f o u n d,

    espec ially in A jax-intensive applic ations.

    A D D I NG A T T R IB U T E S T O T H E C O M M AN DCOMPONENT

    A m o r e c o n ve n i e nt a p p r oa c h , d u e t o r e du c t i on o f r e q ui r e d co d e ,

    i s t o a d d t h e d e s ir e d d a ta s e t a s a n a t tr i b u te t o t h e c o m po n e nt

    i n st e ad o f n es t in g i t a s a c hi l d p a ra m et e r c o m po n en t . W h e n a d di n g

    o b j e ct s a s a t t r i b u te s t o c o m p on e n t s, t h e s e o b j e ct s a r e a v a i l a bl e

    t h r o ug h t he c o m po n e nt s a tt r i b ut e m a p . T h e f o l l ow i n g c o d e s h o ws

    t h e a b ov e e x am p le u s in g f : at t ri b ut e i n st e ad o f f : pa r am

    ...

    ...

    Ex ample 6: View Definit ion for Ex ample 3 using f:at t r ibut e

    ...

    public void delete(ActionEvent event){

    Address selected = (Address) event.getComponent().

    getAttributes().get("selected");

    addresses.remove(selected);

    }

    ...

    E x a mp l e 7 : A c ti o n L i s te n e r i n B a c k i ng B e a n f o r E xa m p le 3

    J SF Best Pr act ices© Orientation in Objec ts G mbH

    S ei te 3 v on 8www.oio.de i nfo@oi o.de

    mailto:[email protected]://www.oio.de/

  • 8/17/2019 Jsf Best Practices Javaserver Faces Session Tips

    4/8

    T h e m a i n d i f f er e n ce s b e t we e n u s a g e o f f : a t tr i b u te a n d f : p a ra m i s

    that parameters are added to the c losest U IC omponent assoc iated

    w i t h a cu s t o m a c t i o n, r e q ui r i n g f : p a ra m t o b e p l a c ed as ch i l d

    c omponent somewhere underneath a c omponent implementing

    t h e A c t i on S o ur c e i n t e rf a c e . W h i l e t h i s is v a l id o nl y f or

    C ommandC omponents, attributes may be added to every kind of

    U I C o m po n e nt , e . g . i n p u t c om p o ne n t s l i k e i n p u t t e xt f i e l d s ,

    i m p l em e n ti n g t h e V a l u e Ho l d er i n t e rf a c e i n s t ea d o f A c t i on S o ur c e .

    C o mp a r e d to t h e p r e vi o u s e xa m p l e, t h e A c t i on L i s t en e r b e c a mep r e tt y l e a n t h is t i m e . T h e L i s t en e r i n t h i s e x am p l e s i m p ly a s s u me s

    t h a t th e p a r a me t e r n a m e d " s e l ec t e d" i s a n i n s t an c e o f t h e A d d re s s

    c l a s s. P l e a se n o t e t h a t t h is m a y l e a d to e r r or s d u ri n g r u n t im e w h e n

    c h a ng i n g t h e a t t r i bu t e s v a l u e i n t he v i e w d e f i ni t i o n t o an o t h er E L

    e x p re s s i on r e f e r en c i n g a d i f f e r en t t y p e . U n t i l t o da y I D E ’ s l a c k o n

    t y p e c h e ck i n g m e c ha n i s ms f o r a t t r i bu t e s d e fi n e d i n J S F v i e w s .

    P A SS I NG VA L UE S T O C O NV E RT E RS A N D

    VALIDATORS

    T h e b e n ef i t o f V a l i d at o r s o r C o n ve r t e rs i s o f t e n i n c r ea s e d b y

    e n a bl i n g t h e m t o d e al w i t h pa r a m et e r s . I t i s w or t h t h in k i n g a b o u t

    a d e d i ca t e d V al i d a t or c l a s s to k e e p y o u r M a n a ge d B ea n s s l im a n dm a i n ta i n a b le w h e ne v e r a V a l i d a to r c o n ta i n s a l o t o f l o g i c. B u t e v en

    i f y o u de c i de t o k ee p v a li d a t i on i n t he M a n a ge d B ea n , p a s s in g

    parameters as described in the previous tw o sec tions is an option

    t o k e ep y o u r c o d e a b i t s ma r t e r b y s i mp l y a t t ac h i ng a n f : at t r i b ut e

    t o t h e c o m po n e nt . Y o u ca n a c ce s s t he a t t r i bu t e w i t h i n y o u r

    validation method via the c omponent sinc e it is passed as an

    argument to the validation method. Validation methods give

    d e v el o p er s a c c e s s t o a l l t h e i n f o rm a t i on n e e d e d, a s t h e i r s i g n a tu r e

    is like

    public void validate(FacesContext ctx,

    UIComponent component,

    Object toValidate)

    T h e s a m e p r i n ci p l e c a n b e a p p l ie d t o C o n ve r t e rs s i n c e t h e

    c omponent is passed as argument in both methods,

    getAsString(…) and getAsObject(…).

    P A S S IN G V A L U ES I N T A B L ES

    S o f a r t h e c o n ce p t s f o r p a s s in g v a l u es t o h a n d le r s w e r e a p p l ie d

    w ithin h:dataTables. Passing values to handlers is easily ac hieved

    by plac ing a c ommand c omponent in a table c olumn. C lic ks on the

    c ommand c omponent may trigger an A c tion M ethod or A c tion

    Listener, the information about w hic h data row to proc ess may be

    p a ss e d a s a t t ri b ut e o r p a ra m et e r. T h e f o ll o wi n g c o de s h ow s a n

    e x a mp l e w i t h a n A ct i o n Li s t e ne r u s i n g a n A tt r i b ut e t o d es c r i be t h e

    s e l ec t e d t a b l e r o w .

    ...

    ...

    E x am p le 8 : V i e w D e f i n i ti o n f o r E x am p l e 4 u s i n g a n A c t io n L is t e ne r

    w i t h f : a t tr i b u te

    ...

    @ManagedBean(name="addressTableBeanExample4")

    @ViewScoped

    public class ExampleBean4 implements Serializable{

    private static final long serialVersionUID = 1L;

    private transient List data = new

    ArrayList() ;

    private Customer selected;

    public ExampleBean4(){

    // create example data model

    data.add(new Customer("Homer","Simpson",80085));

    data.add(new Customer("Barney","Gumble",83321));

    data.add(new Customer("Ned","Flanders",81813));

    }

    public void selectionListener(ActionEvent event){Customer customer = (Customer) event.getComponent().

    getAttributes().get("selection");

    this.selected = customer;

    }

    public Customer getSelected() {

    return selected;

    }

    public void setSelected(Customer selected) {

    this.selected = selected;

    }

    public List getData() {

    return data;

    }

    public void setData(List data) {

    this.data = data;

    }

    }

    E x a mp l e 9 : B a c k i n g B e an D e f i ni t i o n f o r E x a mp l e 4

    W h i l e t h e p r e vi o u s ex a m p le r e q ui r e s ex p l i ci t d e fi n i t i on o f a n

     f:ac tionListener, JSF offers a more data c entric approac h using a

    d i st i nc t d a ta m o de l f o r D a ta T ab l es . T h e p r ec e di n g e x am p le u s ed

    a va l u e b i n d in g t o a c o l l ec t i o n c o n t ai n i n g t h e d a t a t o d i s p la y . U s i n g

    a r e f e r en c e t o a D a t a M o d el i n s t a n c e i n s t ea d o f a c o l l e ct i o n o f f er s

    a more c onvenient w ay to ac c ess the selec ted data set:

    J SF Best Pr act ices© Orientation in Objec ts G mbH

    S ei te 4 v on 8www.oio.de i nfo@oi o.de

    mailto:[email protected]://www.oio.de/

  • 8/17/2019 Jsf Best Practices Javaserver Faces Session Tips

    5/8

    ...

    ...

    E x am p l e 1 0 : V i e w D e f i ni t i o n f o r E x a mp l e 5

    ...

    @ManagedBean(name="addressTableBeanExample5")

    @ViewScoped

    public class ExampleBean5 implements Serializable{

    private static final long serialVersionUID = 1L;

    private transient ListDataModel data =

    new ListDataModel() ;

    private Customer selected;

    public ExampleBean5(){

    // create example data model

    List customers = new ArrayList();

    customers.add(new Customer("Homer","Simpson",80085));

    customers.add(new Customer("Barney","Gumble",83321));

    customers.add(new Customer("Ned","Flanders",81813));

    this.data.setWrappedData(customers);

    }

    public Customer getSelected() {

    return selected;

    }

    public void setSelected(Customer selected) {

    this.selected = selected;

    }

    public ListDataModel getData() {

    return data;

    }

    public void setData(ListDataModel data) {

    this.data = data;

    }

    public String select(){

    this.selected = data.getRowData();

    return "";

    }

    }

    ...

    E x am p l e 1 1: B a c k i n g B e a n u s i n g a Da t a Mo d e l i n E x a mp l e 5

    A s yo u ca n s ee i n t he c o de e x am p le a b ov e , J S F ta k es c ar e o f

    informing the data model w hic h distinct data set w as selected .

    W h e n a n A c ti o n S ou r c e i s t r i g g er e d , J S F t a k e s n o ti c e a b o u t t h e

    r e l a te d e l e me n t o f t h e w r a pp e d d a t a a nd u p d a t es t h e t a b l e m o d el .

    A c ce s s i ng t h e s e l e ct e d da t a s et i s m a d e e a s y u s i n g t h e

    g e tR o wD a ta ( ) M e th o d o f t h e T a bl e Mo d el . P l ea s e n o te t h e d i ff e re n ce

    b e t we e n t he A c t i on L i s te n e r i n E x a mp l e 4 a n d t he A c t i on M e th o d

    in this example.

    SHARING INFORMATION BETWEEN VIEWS

    USING F:SETPROPERTYACTIONLISTENER

    T h e u s ag e o f t h e f : se t Pr o pe r ty A ct i on L is t en e r t a g h a s b e en

    d i s c us s e d e ar l i e r. T h e e x a m pl e i n s e c t io n o n e d e s cr i b e d ho w t h e

    t a g c a n be u s ed t o s e t a m a na g ed b e an p r op e rt y . T h e s a me

    approac h c an be used to set a property on a managed bean that

    b e l o ng s t o t h e n e x t v i ew t o d i s p l ay . S i m p ly a d d t h e

     f:setPropertyA ctionListener tag as c hild c omponent to the

    c o m ma n d c o m po n e nt t r i g g e ri n g a n a c t i o n m e th o d ( o r u s i n g J S F

    2 ’ s i m p li c i t n a v i ga t i o n f ea t u r e) a n d a s s o ci a t e t h e t a r g et a t t r i b u te t o

    a property on the next view s managed bean:

    ...

    ...

    ...

    E x am p le 1 2 : V i ew D e fi n ti o n o f f i rs t v i ew i n E xa m pl e 6

    ...

    ...

    ...

    E x am p le 13 : V i ew D e fi n ti o n o f 2 n d v i ew i n E x am p le 6

    T h e B a ck i ng B e an s o f b o th v i ew s a re n o th i ng m o re b u t si m pl e

    P O J O 's c on t a i ni n g a c c es s o r m e t h od s fo r a s i ng l e a t t r i bu t e ( a n d o f

    c ou r se , b o th c ou l d e a s i ly b e i mp l em en t ed a s i n st a nc es o f t h e s a m e

    c lass):

    ...

    @ManagedBean

    @RequestScoped

    public class ExampleBean6a implements Serializable{

    private static final long serialVersionUID = 1L;

    private String input;

    public String getInput() {

    return input;

    }

    public void setInput(String input) {

    this.input = input;

    }

    }

    @ManagedBean

    @RequestScoped

    public class ExampleBean6b implements Serializable{

    private static final long serialVersionUID = 1L;public ExampleBean6b(){

    }

    private String value;

    public String getValue() {

    return value;

    }

    public void setValue(String value) {

    this.value = value;

    }

    }

    ...

    Ex ample 14: Backing Beans of Ex ample 6

    J SF Best Pr act ices© Orientation in Objec ts G mbH

    S ei te 5 v on 8www.oio.de i nfo@oi o.de

    mailto:[email protected]://www.oio.de/

  • 8/17/2019 Jsf Best Practices Javaserver Faces Session Tips

    6/8

    T h e q u e s ti o n h e re i s : W h y i s t h i s wo r k i ng ? A g o o d q ue s t i on , s i n c e

    t h e i n st a nc e o f t h e m a na g ed b ea n fo r t h e f o ll o wi n g v i ew i s n o t

    e x p ec t e d t o b e c r e at e d be f o re t h e " R e n de r R e s po n s e" P h a se o f t h e

    l i f e cy c l e ( u n l es s t h e m a n a ge d b e a ns sc o p e i s a n y t h in g e l s e t h e n

    r e q ue s t ) . W h e n f : s et P r op e r t yA c t i on L i s te n e r i s u s e d, i n s t an t i a t i on

    h a p pe n s b y t h e t i m e o f e x e cu t i n g t h e i m p l ic i t l y g e n e ra t e d A c t i on

    L i st e ne r , t h at i s b ef o re t h e " I nv o ke A pp l ic a ti o n" p h a s e o f t h e

    l i f e cy c l e . T h u s , t h e M a n a ge d B ea n f or t h e f o l l ow i n g v i e w i s a l r ea d y

    a v a i l ab l e w h e n r e a ch i n g t h e " R e nd e r R e s po n s e" p ha s e .

    ACTION LISTENER

    T h e u s a g e o f f : s et P r o pe r t y Ac t i o nL i s t en e r i s a c o n v en i e n t wa y t o

    s t o re v a l u e s i n M a na g e d B e a ns o f s u b s eq u e nt v i e w s . H o w ev e r ,

    t h ou g h m or e c o de i n te n si v e, t h e s a me e f fe c t c an b e a c hi e ve d b y

    manually designed A c tionListeners. This approac h gives you the

    a b i l i ty t o p r oc e s s va l u e s b e f or e s t o r in g t h e m i n a n o th e r M a n a ge d

    B ea n . H ow ev er , t h is m a y a l s o t e mp t t o p u t l o gi c i n t h e P r e se nt a ti o n

    L a y er t h a t b e l on g s e l s ew h e re . K e e p i n m in d , t h a t J S F o f fe r s

    c o nc e pt s f o r c o nv e r sa t i on a n d v a l id a t io n w h en e v er y o u t h i nk a b ou t

    data transformation when passing values to managed beans using

    an A c tion Listener.

    FLASH SCOPE

    W it h J S F2 a n ew f ea t ur e c a ll e d " F l a sh " w a s i n t r od u ce d. I t i s i n sp i re d

    by the R uby on R ails F ramew ork and offers a c onvenient w ay to

    p a s s i n f or m a ti o n b e tw e en v i e ws . T h o ug h o f t en m i s ta k e nl y r e fe r re d

    t o a s " F l a s h S c o pe " , t h e F l a s h i s n ot a s co p e l i k e t h e r e q ue s t or

    s e s si o n s co p e . I t i s r a th e r a m a p m an a g e d b y t h e f r a m ew o r k. I t i s

    c a p a bl e o f h o l d in g a v a l ue u n t i l t h e n e x t v i e w i s pr o c es s e d, s o yo u

    w ou l d n o t w a nt t o p ut a n e nt i re m an a ge d b ea n i n t h e " F l as h S co pe " .

    T h e f o l l ow i n g e x a m pl e s h o ws h ow t he F l a s h c a n b e u s e d t o pa s s

    a n i n pu t f r om o n e v i e w t o a n o t he r . T h e re i s a r e qu e s t sc o p ed

    b a ck i ng b ea n f or t h e s e co n d vi e w on l y. N o te h o w th e F l as h i s

    i n j e ct e d in t h e b a c k in g b e a n u s i n g t h e @ M a na g e d Pr o p er t yannotation. You c ould also ac c ess the F lash programmatic ally by

    c alling FacesContext.getCurrentInstance()

    .getExternalContext().getFlash() b u t ha v in g t h e F l as h

    i n j e ct e d i n t o t h e b e a n i s m o r e c o n ve n i e nt .

    ...

    ...

    ...

    E x am p l e 15 : V ie w D e f i ni t i o n f o r fi r s t v i e w i n E x a mp l e 7

    ...

    ...

    ...

    E xa m pl e 1 6 : V ie w D e fi n it i on f o r 2 nd v i ew i n E x am p le 7

    ...

    @ManagedBean

    @RequestScoped

    public class FlashExampleBean implements Serializable{

    @ManagedProperty("#{flash}")

    private Flash flash;

    public String getInputFromFlash(){

    String inputText = (String) flash.get("inputText");

    flash.keep("inputText");

    return inputText;

    }public void setFlash(Flash flash) {

    this.flash = flash;

    }

    public Flash getFlash() {

    return flash;

    }

    }

    ...

    E x a mp l e 1 7 : B a c k i ng Be a n D e f i n it i o n f o r 2 n d v i e w i n E x a mp l e 7

    Y o u ma y h a ve n o t i ce d t he c a l l t o f l a s h .k e e p () i n t h e b a c k in g b e a ns

    g e tt e r m e th o d. T h is t e l ls t h e f l as h t o k e ep t h e v a lu e f o r a n ot h er

    subsequent request as values stored in the F lash during request

    N a r e o n l y av a i l a bl e t h r o ug h o ut t h e r e q ue s t N +1 u n l es s t h e F l a s h

    i s t ol d t o k e e p i t fo r a n o t he r r e q ue s t . B y ca l l i n g F l a s h .k e e p( ) w e

    a s ur e t h at t h e i n pu t i s s t il l a v ai l ab l e w he n r et u rn i ng t o t h e f i r s t p a ge

    b y p re s s i ng t h e b a c k b u t t on .

    WH E N T O U S E S E SS I O N -S C O P E

    The previous sec tions disc ussed several methods to pass values

    t o h a n dl e rs a n d b e tw e en v i e ws w i t ho u t b l o at i n g t h e s e ss i o n. F i n al l y

    w e s h o u ld t a k e a l o o k a t sc e n ar i o s wh e r e s e s s io n s c op e s h o u ld b e

    u s e d. A s m en t i o ne d i n th e b e g i nn i n g o f t h i s a rt i c l e, o b j e ct s s t o re d

    i n s e ss i on s c o pe r e ma i n u nt i l t h e e n d of t h e u s er s e ss i on o r u n ti l

    they are removed programmatic ally. A c ommon sc enario w here

    a n ob j e c t i s u se d th r o ug h o u t t h e e n t i re l i f e t im e o f t h e u s e r s e s s io n

    i s a u t he n ti c at i on . C on s id e r a l o gi n s c re e n w he re t h e u s er h a s t oe n t er i t s c r ed e n ti a l s . I f a u t he n t i ca t i o n i s s u c ce s s fu l t h e u s e r s e s si o n

    is assoc iated w ith an objec t representing the authentic ated user.

    I t m a y c o n t ai n t h e u s e rs n a m e , i t s c u s t om e r i d e t c . T h i s o b je c t m a y

    b e u s ed t h r ou g ho u t t h e e n ti r e s e ss i on t o d e c i de f o r e x am p le t h e

    l o ok a n d f ee l o f t h e a p pl i ca t i on , t h e o p ti o ns g i v en t o t h e u s er a n d

    t h e g e ne r al b e ha v io r o f t h e a p pl i ca t io n f o r t h is p a rt i cu l ar u s er .

    It is totally vital to have a session sc oped managed bean in your

    J S F a p p li c a ti o n t h a t s t o re s i n f or m a ti o n n e ed e d t h r ou g h ou t t h e u s e r

    s e s s io n , b u t it i s go o d p r a ct i c e t o h av e o n l y o n e s e s s io n be a n . I f

    y o u fe e l y ou n e ed m o re , t h i n k a b o u t t h e b a s i c d e s i gn o f y o u r

    applic ation.

    CONCLUSION

    T o r e v i ew th e u s a g e o f b e a n s w i t h s e s s io n s c o pe i s on e o f t h e e a s y

    w i n n er s f o r e v e ry r e v i e we r o f m o s t r e al w o r l d J S F a p p l ic a t i on s .

    T h e m a i n g o a l o f t h i s a r t i cl e i s t o d i s c us s c o m mo n s c e na r i o s o f

    improperly session sc oped beans and to give advic e on how to

    p r e ve n t t h i s . O f c o u rs e a l l t h i s d o es n o t m e a n t h at s e s s i on s c o p e

    i s a b a d t h i n g . T h e u s a g e o f s e s si o n s c o pe d be a n s m a y b e t o t a l l y

    v a l i d . H o w ev e r , d e v e lo p e rs ne e d t o be s u r e t h a t t h e i r i n t e nd e d

    s o l u ti o n i s n o t on l y w or k i n g f o r a s i n gl e u s e c a s e b u t a ls o f re e f r o m

    s i d e e f f ec t s . T h i s a rt i c l e h o p ef u l l y he l p s t o s h e d so m e l i g h t on t h e

    s i de e f fe c ts o f i m pr o pe r u s ag e o f s e ss i on s c op e s. A n d wh e re n o t

    – i f y o u re mi n d o ne t h in g : A l wa y s h av e s o me r e as o ns f o r u s in g

    s t at e i n a s e ss i on .

    T he c om pl et e s ou rc e c od e o f t h e e x am pl es i s a va il ab le f ro m [ 1 ] .

      Plese read the license file license.txt

    J SF Best Pr act ices© Orientation in Objec ts G mbH

    S ei te 6 v on 8www.oio.de i nfo@oi o.de

    mailto:[email protected]://www.oio.de/

  • 8/17/2019 Jsf Best Practices Javaserver Faces Session Tips

    7/8

    T h e s o u rc e s a r e c o n ta i n e d i n a ZI P a r c h iv e a s E c l i ps e p r o je c t . T h e

    sourc es were developed on M ojarra 2.0.3, though any JSF 2.0

    c o m pl i a n t i m p l em e n t at i o n s h o ul d be f i n e . B e s u r e t o in c l ud e t h e

    J S F l i b r ar i e s j s f - ap i . j ar a n d j s f - im p l .j a r i n W e b Co n t en t / WE B - I NF / l i b.

    T h e l i b r ar i e s c a n b e o b t a in e d f r o m O R A CL E ' s M o j ar r a p r o je c t [ 2 ]

    o r M y F a ce s [ 3 ] , t h e o p e n s o u rc e i m p l em e n t at i o n o f J S F .

    J SF Best Pr act ices© Orientation in Objec ts G mbH

    S ei te 7 v on 8www.oio.de i nfo@oi o.de

    mailto:[email protected]://www.oio.de/

  • 8/17/2019 Jsf Best Practices Javaserver Faces Session Tips

    8/8

    REFERENCES:

    • [1] Source code of t he ex amples

    JSF -Best-Prac tic es.zip (http://w w w .oio.de/public /java/jsf/JSF -Best-Prac tic es.zip)

    • [2] JSF Implementati on Mojarra

    http://javaserverfac es.java.net/ (http://javaserverfac es.java.net/)

    • [3] JSF Implementati on MyFac eshttp://myfac es.apac he.org/ (http://myfac es.apac he.org/)

    J SF Best Pr act ices© Orientation in Objec ts G mbH

    S ei te 8 v on 8www.oio.de i nfo@oi o.de

    mailto:[email protected]://www.oio.de/http://myfaces.apache.org/http://javaserverfaces.java.net/http://www.oio.de/public/java/jsf/JSF-Best-Practices.zip