You are on page 1of 19


WlLhln a webbased appllcaLlon !ava8eans can be sLored ln (and accessed from) a number of dlfferenL
collecLlons of aLLrlbuLes Lach collecLlon has dlfferenL rules for Lhe llfeLlme of LhaL collecLlon and Lhe
vlslblllLy of Lhe beans sLored Lhere 1ogeLher Lhe rules deflnlng llfeLlme and vlslblllLy are called Lhe scope
of Lhose beans 1he !avaServer ages (!S) SpeclflcaLlon deflnes scope cholces uslng Lhe followlng Lerms
(wlLh Lhe equlvalenL servleL Al concepL deflned ln parenLheses)
O fe 8eans LhaL are vlslble wlLhln a slngle !S page for Lhe llfeLlme of Lhe currenL requesL
(Local varlables of Lhe servlce meLhod)
O equest 8eans LhaL are vlslble wlLhln a slngle !S page as well as Lo any page or servleL LhaL ls
lncluded ln Lhls page or forwarded Lo by Lhls page (8equesL aLLrlbuLes)
O sess|on 8eans LhaL are vlslble Lo all !S pages and servleLs LhaL parLlclpaLe ln a parLlcular user
sesslon across one or more requesLs (Sesslon aLLrlbuLes)
O f||cft|on 8eans LhaL are vlslble Lo all !S pages and servleLs LhaL are parL of a web
appllcaLlon (ServleL conLexL aLLrlbuLes)
Act|on Io 8efns
lf you declare AcLlonlorm beans ln your SLruLs conflguraLlon flle Lhe SLruLs conLroller servleL wlll
auLomaLlcally perform Lhe followlng servlces for you before lnvoklng Lhe approprlaLe AcLlon meLhod
O heck for an lnsLance of a bean of Lhe approprlaLe class under Lhe approprlaLe key ln Lhe
approprlaLe scope (requesL or sesslon)
O lf Lhere ls no such bean lnsLance avallable a new one ls auLomaLlcally creaLed and added Lo Lhe
approprlaLe scope (requesL or sesslon)
O lor every requesL parameLer whose name corresponds Lo Lhe name of a properLy ln Lhe bean
Lhe correspondlng seLLer meLhod wlll be called 1hls operaLes ln a manner slmllar Lo Lhe
sLandard !S acLlon [spseLroperLy when you use Lhe asLerlsk wlldcard Lo selecL all
O 1he updaLed AcLlonlorm bean wlll be passed Lo Lhe execuLe meLhod of an AcLlon class
orgapachesLruLsAcLlon so LhaL Lhe values can be made avallable Lo your sysLem sLaLe and
buslness loglc beans
ste tfte 8efns
1he acLual sLaLe of a sysLem ls normally represenLed as a seL of one or more !ava8eans classes whose
properLles deflne Lhe currenL sLaLe A shopplng carL sysLem for example wlll lnclude a bean LhaL
represenLs Lhe carL belng malnLalned for each lndlvldual shopper and wlll (among oLher Lhlngs) lnclude
Lhe seL of lLems LhaL Lhe shopper has currenLly selecLed for purchase SeparaLely Lhe sysLem wlll also
lnclude dlfferenL beans for Lhe users proflle lnformaLlon (lncludlng Lhelr credlL card and shlpLo
addresses) as well as Lhe caLalog of avallable lLems and Lhelr currenL lnvenLory levels
lor small scale sysLems or for sLaLe lnformaLlon LhaL need noL be kepL for a long perlod of Llme a seL of
sysLem sLaLe beans may conLaln all Lhe knowledge LhaL Lhe sysLem ever has of Lhese parLlcular deLalls
Cr as ls ofLen Lhe case Lhe sysLem sLaLe beans wlll represenL lnformaLlon LhaL ls sLored permanenLly ln
some exLernal daLabase (such as a usLomer8ean ob[ecL LhaL corresponds Lo a parLlcular row ln Lhe
DS1CML8S Lable) and are creaLed or removed from Lhe servers memory as needed LnLlLy LnLerprlse
!ava8eans are also used for Lhls purpose ln large scale appllcaLlons
8us|ness Lo|c 8efns
?ou should encapsulaLe Lhe funcLlonal loglc of your appllcaLlon as meLhod calls on !ava8eans deslgned
for Lhls purpose 1hese meLhods may be parL of Lhe same classes used for Lhe sysLem sLaLe beans or
Lhey may be ln separaLe classes dedlcaLed Lo performlng Lhe loglc ln Lhe laLLer case you wlll usually
need Lo pass Lhe sysLem sLaLe beans Lo be manlpulaLed Lo Lhese meLhods as argumenLs
uependlng on Lhe complexlLy and scope of your appllcaLlon buslness loglc beans mlghL be ordlnary
!ava8eans LhaL lnLeracL wlLh sysLem sLaLe beans passed as argumenLs or ordlnary !ava8eans LhaL access
a daLabase uslng !u8 calls lor larger appllcaLlons Lhese beans wlll ofLen be sLaLeful or sLaLeless
LnLerprlse !ava8eans (L!8s) lnsLead
uyna8eans comblne Lhe exLenslblllLy of !ava8eans wlLh Lhe flexlblllLy of a Map ueflnlng even Lhe
slmplesL !ava8ean requlres deflnlng a new class and codlng a fleld and Lwo meLhods for each properLy
1he properLles of a uyna8ean can be conflgured vla an xML descrlpLor 1he vlrLual properLles of a
uyna8ean canL be called by sLandard !ava meLhods buL work well wlLh componenLs LhaL rely on
reflecLlon and lnLrospecLlon
Coons Chf|n
A popular Lechnlque for organlzlng Lhe execuLlon of complex processlng flows ls Lhe haln of
8esponslblllLy paLLern as descrlbed (among many oLher places) ln Lhe classlc Cang of lour deslgn
paLLerns book 1he Col summarlzes Lhe haln of 8esponslblllLy paLLern as Avold coupllng Lhe sender of
a requesL Lo lLs recelver by glvlng more Lhan one ob[ecL a chance Lo handle Lhe requesL haln Lhe
recelvlng ob[ecLs and pass Lhe requesL along Lhe chaln unLll an ob[ecL handles lL
1he o8 paLLern helps us keep sofLware componenLs loosely coupled A componenL can call a haln of
8esponsblllLy wlLhouL knowlng whaL ob[ecLs are on Lhe chaln or how Lhey are lmplemenLed MosL
lmporLanLly we can ad[usL Lhe haln wlLhouL changlng how callers lnvoke Lhe haln As of verslon 13
Lhe defaulL 8equesL rocessor whlch acLs as Lhe frameworks kernel ls a haln of 8esponslbllLy

Intenft|onf||ze essfes
1he framework bullds upon Lhe sLandard classes avallable on Lhe !ava plaLform Lo bulld
lnLernaLlonallzed and locallzed appllcaLlons 1he key concepLs Lo become famlllar wlLh are
O Locf|e 1he fundamenLal !ava class LhaL supporLs lnLernaLlonallzaLlon ls Locale Lach Locale
represenLs a parLlcular cholce of counLry and language (plus an opLlonal language varlanL) and
also a seL of formaLLlng assumpLlons for Lhlngs llke numbers and daLes
O kesouce8und|e 1he [avauLll8esource8undle class provldes Lhe fundamenLal Lools for
supporLlng messages ln mulLlple languages See Lhe !avadocs for Lhe 8esource8undle class and
Lhe lnformaLlon on lnLernaLlonallzaLlon ln Lhe documenLaLlon bundle for your !uk release for
more lnformaLlon
O oetkesouce8und|e Cne of Lhe sLandard lmplemenLaLlons of 8esource8undle allows you
Lo deflne resources uslng Lhe same namevalue synLax used Lo lnlLlallze properLles flles 1hls ls
very convenlenL for preparlng resource bundles wlLh messages LhaL are used ln a web
appllcaLlon because Lhese messages are generally LexL orlenLed
O MessfeIoft 1he [avaLexLMessagelormaL class allows you Lo replace porLlons of a
message sLrlng (ln Lhls case one reLrleved from a resource bundle) wlLh argumenLs speclfled aL
run Llme 1hls ls useful ln cases where you are creaLlng a senLence buL Lhe words would appear
ln a dlfferenL order ln dlfferenL languages 1he placeholder sLrlng 0 ln Lhe message ls replaced
by Lhe flrsL runLlme argumenL 1 ls replaced by Lhe second argumenL and so on
O Messfekesouces 1he framework class orgapachesLruLsuLllMessage8esources leLs you
LreaL a seL of resource bundles llke a daLabase and allows you Lo requesL a parLlcular message
sLrlng for a parLlcular Locale (normally one assoclaLed wlLh Lhe currenL user) lnsLead of for Lhe
defaulL Locale Lhe server lLself ls runnlng ln
l18n supporL ln a serverslde framework ls llmlLed Lo Lhe esentft|on of lnLernaLlonallzed LexL and
lmages Lo Lhe user SupporL for Locale speclflc |nut ethods (used wlLh languages such as !apanese
hlnese and korean) ls lefL up Lo Lhe cllenL devlce whlch ls usually a web browser
Autoft|c fo vf||dft|on
ln addlLlon Lo Lhe form and bean lnLeracLlons descrlbed above 1he framework offers an addlLlonal
faclllLy Lo valldaLe Lhe lnpuL flelds lL has recelved 1o uLlllze Lhls feaLure overrlde Lhe followlng meLhod
ln your AcLlonlorm class
valldaLe(AcLlonMapplng mapplngPLLpServleL8equesL requesL)

1he tuts Vf||dfto
onflgurlng Lhe valldaLor Lo perform form valldaLlon ls easy
1 1he AcLlonlorm bean musL exLend valldaLorlorm
2 1he forms !S musL lnclude Lhe hLml[avascrlpL Lag for cllenL slde valldaLlon
3 ?ou musL deflne Lhe valldaLlon rules ln an xml flle llke Lhls
form namelogonlorm
fleld properLyusername dependsrequlred
msg namerequlred keyerrorusername/
1he msg elemenL polnLs Lo Lhe message resource key Lo use when generaLlng Lhe error
4 LasLly you musL enable Lhe valldaLorlugln ln Lhe sLruLsconflgxml flle llke Lhls
plugln classnameorgapachesLruLsvalldaLorvalldaLorlugln
Note lf your requlred form properLy ls one of Lhe !ava ob[ecL represenLaLlons of prlmlLlve Lypes (le
[avalanglnLeger) you musL seL Lhe AcLlonServleLs converLnull lnlL parameLer Lo Lrue lalllng Lo do Lhls
wlll resulL ln Lhe requlred valldaLlon noL belng performed on LhaL fleld because lL wlll defaulL Lo 0
fe Coos|t|on w|th 1||es
1lles ls a powerful LemplaLlng llbrary LhaL allows you Lo consLrucL vlews by comblnlng varlous Llles
Peres a qulck seLup gulde
1 reaLe a /layouL/layouL[sp flle LhaL conLalns your apps common look and feel
LlleslnserL aLLrlbuLebody/
2 reaLe your /lndex[sp homepage flle
h11hls ls my homepage/h1
3 reaLe a /WL8lnl/Lllesdefsxml flle LhaL looks llke Lhls
puL namebody value/
deflnlLlon namehomepage exLendslayouL
4 SeLup Lhe 1lleslugln ln Lhe sLruLsconflgxml flle
3 SeLup an acLlon mapplng ln sLruLsconflgxml Lo polnL Lo your homepage Llle
1he 1lleslugln conflgures a speclal 8equesLrocessor LhaL deLermlnes lf Lhe requesLed vlew ls a Llle and
processes lL accordlngly noLe LhaL we made Lhe homepage Llle exLend our rooL layouL Llle and changed
Lhe body aLLrlbuLe 1lles lnserL Lhe flle named ln Lhe body aLLrlbuLe lnLo Lhe maln layouL
1he Act|on ev|et
lor Lhose of you famlllar wlLh Mv archlLecLure Lhe AcLlonServleL represenLs Lhe Lhe conLroller 1he
[ob of Lhe conLroller ls Lo
O process user requesLs
O deLermlne whaL Lhe user ls Lrylng Lo achleve accordlng Lo Lhe requesL
O pull daLa from Lhe model (lf necessary) Lo be glven Lo Lhe approprlaLe vlew and
O selecL Lhe proper vlew Lo respond Lo Lhe user
Act|onIo C|fsses
An AcLlonlorm represenLs an P1ML form LhaL Lhe user lnLeracLs wlLh over one or more pages ?ou wlll
provlde properLles Lo hold Lhe sLaLe of Lhe form wlLh geLLers and seLLers Lo access Lhem AcLlonlorms
can be sLored ln elLher Lhe sesslon (defaulL) or requesL scope lf Lheyre ln Lhe sesslon lLs lmporLanL Lo
lmplemenL Lhe forms reseL meLhod Lo lnlLlallze Lhe form before each use 1he framework seLs Lhe
AcLlonlorms properLles from Lhe requesL parameLers and sends Lhe valldaLed form Lo Lhe approprlaLe
AcLlons execuLe meLhod
When you code your AcLlonlorm beans keep Lhe followlng prlnclples ln mlnd
O 1he AcLlonlorm class lLself requlres no speclflc meLhods Lo be lmplemenLed lL ls used Lo
ldenLlfy Lhe role Lhese parLlcular beans play ln Lhe overall archlLecLure 1yplcally an AcLlonlorm
bean wlll have only properLy geLLer and properLy seLLer meLhods wlLh no buslness loglc
O 1he AcLlonlorm ob[ecL also offers a sLandard valldaLlon mechanlsm lf you overrlde a sLub
meLhod and provlde error messages ln Lhe sLandard appllcaLlon resource 1he framework wlll
auLomaLlcally valldaLe Lhe lnpuL from Lhe form (uslng your meLhod) Cf course you can also
lgnore Lhe AcLlonlorm valldaLlon and provlde your own ln Lhe AcLlon ob[ecL
O ueflne a properLy (wlLh assoclaLed geLxxx and seLxxx meLhods) for each fleld LhaL ls presenL ln
Lhe form 1he fleld name and properLy name musL maLch accordlng Lo Lhe usual !ava8eans
convenLlons lor example an lnpuL fleld named username wlll cause Lhe seLDsername meLhod
Lo be called
O 8uLLons and oLher conLrols on your form can also be deflned as properLles 1hls can help
deLermlne whlch buLLon or conLrol was selecLed when Lhe form was submlLLed
O 1hlnk of your AcLlonlorm beans as a flrewall beLween P11 and Lhe AcLlon Dse Lhe valldaLe
meLhod Lo ensure all requlred properLles are presenL and LhaL Lhey conLaln reasonable values
An AcLlonlorm LhaL falls valldaLlon wlll noL even be presenLed Lo Lhe AcLlon for handllng
O ?ou may also place a bean lnsLance on your form and use nesLed properLy references lor
example you mlghL have a cusLomer bean on your AcLlonlorm and Lhen refer Lo Lhe properLy
cusLomername ln your presenLaLlon page 1hls would correspond Lo Lhe meLhods
cusLomergeLname() and cusLomerseLname(SLrlng name) on your cusLomer bean
O cootloo lf you nesL an exlsLlng bean lnsLance on your form Lhlnk abouL Lhe properLles lL
exposes Any publlc properLy on an AcLlonlorm LhaL accepLs a slngle SLrlng value can be seL wlLh
a query sLrlng lL may be useful Lo place beans LhaL can affecL Lhe buslness sLaLe lnslde a Lhln
wrapper LhaL exposes only Lhe properLles requlred 1hls wrapper can also provlde a fllLer Lo be
sure runLlme properLles are noL seL Lo lnapproprlaLe values
DnfAct|onIo C|fsses
MalnLalnlng a separaLe concreLe AcLlonlorm class for each form ln your appllcaLlon ls Llmeconsumlng
lL ls parLlcularly frusLraLlng when all Lhe AcLlonlorm does ls gaLherlng and valldaLe slmple properLles
LhaL are passed along Lo a buslness !ava8ean
1hls boLLleneck can be allevlaLed Lhrough Lhe use of uynaAcLlonlorm classes lnsLead of creaLlng a new
AcLlonlorm subclass and new geL/seL meLhods for each of your beans properLles you can llsL lLs
properLles Lype and defaulLs ln Lhe frameworks conflguraLlon flle
lor example add Lhe followlng Lo sLruLsconflgxml for a Dserlorm bean LhaL sLores a users glven and
famlly names
?ou may also speclfy Arrays of [ava prlmlLlve Lypes (eg SLrlng) ?ou may also speclfy a concreLe
lmplemenLaLlon of Lhe Map lnLerface such as [avauLllPashMap or a LlsL lmplemenLaLlon such as
If ou do not su| fn |n|t|f| ftt|bute nubes w||| be |n|t|f||zed to 0 fnd ob[ects to nu||
ln !S pages uslng Lhe SLruLs 1agllb aLLrlbuLes of uynaAcLlonlorm ob[ecLs can be referenced [usL llke
ordlnary AcLlonlorm ob[ecLs Wherever a SLruLs Lag refers Lo a properLy Lhe Lags wlll auLomaLlcally
use Lhe uynaAcLlonlorm properLles [usL llke Lhose of a convenLlonal !ava8ean ?ou can even expose
uynaAcLlonlorm properLles uslng beandeflne (AlLhough you canL use beandeflne Lo |nstfnt|fte a
uynaAcLlonlorm slnce lL needs Lo be seLup wlLh Lhe approprlaLe dynaproperLles)
lf you are uslng Lhe SLruLs !S1L LL Lagllb Lhe references are by defaulL dlfferenL Cnly properLles of
ordlnary AcLlonlorm ob[ecLs can be dlrecLly accessed Lhrough Lhe !S1L expresslon language synLax 1he
uynaAcLlonlorm properLles musL be accessed Lhrough a sllghLly dlfferenL synLax 1he !S1L LL synLax for
referenclng a properLy of an AcLlonlorm goes llke Lhls
1he synLax for referenclng a properLy of a uynaAcLlonlorm would be
1he map properLy ls a properLy of uynaAcLlonlorm whlch represenLs Lhe PashMap conLalnlng Lhe
uynaAcLlonlorm properLles
uynaAcLlonlorms are meanL as an easy soluLlon Lo a common problem oot Actloolotms ose slmple
ptopettles ooJ stooJotJ vollJotloos ooJ yoo jost poss tbese ptopettles ovet to oootbet Iovo8eoo (say
uslng 8eanDLllscopyroperLles(my8uslness8eanform))
uynaAcLlonlorms are noL a dropln replacemenL for AcLlonlorms lf you need Lo access uynaAcLlonlorm
properLles ln your AcLlon you wlll need Lo use Lhe mapsLyle accessor llke mylormgeL(name) lf you
acLlvely use Lhe AcLlonlorm ob[ecL ln your AcLlon Lhen you may wanL Lo use convenLlonal AcLlonlorms
uynaAcLlonlorms cannoL be lnsLanLlaLed uslng a noargumenL consLrucLor ln order Lo slmulaLe Lhe
exLra properLles Lhere ls a loL of machlnery lnvolved ln Lhelr consLrucLlon ?ou musL rely on Lhe
framework Lo lnsLanLlaLe a uynaAcLlonlorm for you vla Lhe AcLlonMapplng
lf need be you can exLend Lhe uynaAcLlonlorm Lo add cusLom valldaLe and reseL meLhods you mlghL
need Slmply speclfy your subclass ln Lhe sLruLsconflg lnsLead Powever you cannoL mlx convenLlonal
properLles and uynaroperLles A convenLlonal geLLer or seLLer on a uynaAcLlonlorm wonL be found by
Lhe reflecLlon uLlllLles
1o use uynaAcLlonlorms wlLh Lhe SLruLs valldaLor speclfy
orgapachesLruLsvalldaLorvalldaLorAcLlonlorm (or your subclass) as Lhe formbean class
And of course whlle Lhe uynaAcLlonlorm may supporL varlous blnary Lypes properLles used wlLh Lhe
hLmlLexL Lag should sLlll be SLrlng properLles

LfzAct|onIo C|fsses
SLruLs ozy AcLlonlorm whlch wtops a Lazyuyna8ean
1here lsnL really LhaL much Lo Lhls lmplemenLaLlon as mosL of Lhe lozy behavlour ls ln Lazyuyna8ean and
wtopploq Lhe Lazyuyna8ean ls handled ln Lhe parenL 8eanvalldaLorlorm 1he only Lhlng lL really does ls
populaLe loJexeJ properLles whlch are a LlsL Lype wlLh a Lazyuyna8ean ln Lhe geL(name lndex) meLhod
ozy uyna8eans provlde several Lypes of lozy behavlour
O f oet fdd|t|on properLles whlch do noL exlsL are auLomaLlcally added
O f L|st ffc|||t|es auLomaLlcally qtows a LlsL or Array Lo accomodaLe Lhe lndex value belng seL
O f L|st ceft|on auLomaLlc creaLlon of a LlsL or Array for loJexeJ properLles lf lL doesnL exlsL
O f Mf ceft|on auLomaLlc creaLlon of a Map for moppeJ properLles lf lL doesnL exlsL
Dslng Lhls lozy AcLlonlorm means LhaL you donL have Lo deflne Lhe AcLlonlorms properLles ln Lhe
sLruLsconflgxml Powever a word of warnlng everyLhlng ln Lhe 8equesL geLs populaLed lnLo Lhls
AcLlonlorm clrcumvenLlng Lhe normal fltewoll funcLlon of SLruLs forms 1herefore you should only toke
oot of Lhls form properLles you expecL Lo be Lhere raLher Lhan bllndly populaLlng all Lhe properLles lnLo
Lhe buslness Ller
Pavlng sald LhaL lL ls noL necessary Lo predeflne properLles ln Lhe sLruLsconflgxml lL ls useful Lo
someLlmes do so for moppeJ or loJexeJ properLles lor example lf you wanL Lo use a dlfferenL Map
lmplemenLaLlon from Lhe defaulL PashMap or an array for lndexed properLles raLher Lhan Lhe defaulL
LlsL Lype
formbean namemylorm LypeorgapachesLruLsvalldaLorLazyvalldaLorlorm
formproperLy namemyMap Lype[avauLll1reeMap /
formproperLy namemy8eans LypeorgapachecommonsbeanuLllsLazyuyna8ean /

AnoLher reason for deflnlng loJexeJ properLles ln Lhe sLruLsconflgxml ls LhaL lf you are valldaLlng
lndexed properLles uslng Lhe valldaLor and none are submlLLed Lhen Lhe lndexed properLy wlll be null
whlch causes valldaLor Lo fall redeflnlng Lhem ln Lhe sLruLsconflgxml wlll resulL ln a zerolengLh
lndexed properLy (array or LlsL) belng lnsLanLlaLed avoldlng an lssue wlLh valldaLor ln LhaL clrcumsLance
1hls lmplemenLaLlon valldaLes uslng Lhe AcLlonlorm oome lf you requlre a verslon LhaL valldaLes
accordlng Lo Lhe potb Lhen lL can be easlly creaLed ln Lhe followlng manner
publlc class MyLazylorm exLends LazyvalldaLorlorm

publlc MyLazylorm ()

8aLher Lhan uslng Lhls class anoLher alLernaLlve ls Lo elLher use a Lazyuyna8ean or cusLom verslon of
Lazyuyna8ean dlrecLly Lhe framework now auLomaLlcally wtops ob[ecLs whlch are noL AcLlonlorms ln a
8eanvalldaLorlorm lor example
formbean namemylorm LypeorgapachecommonsbeanuLllsLazyuyna8ean
formproperLy namemy8eans LypeorgapachecommonsbeanuLllsLazyuyna8ean /
lf you [usL wanL Lo use Lhe ozy llsL feaLures and reLaln Lhe lltewoll funcLlon of Lhe AcLlonlorm
Lhen you can do Lhls by deflnlng all Lhe properLles ln Lhe sLruLsconflgxml ln Lhe same way as for a
uynaAcLlonlorm and seLLlng Lhe resLrlcLed properLy 1he resLrlcLed properLy makes Lhe
MuLableuynalass resLrlcLed whlch prevenLs any addlLlonal properLles belng added
formbean nameresLrlcLedlorm LypeorgapachesLruLsvalldaLorLazyvalldaLorlorm
setoet oetest|cted vf|uetue$
formproperLy nameordernumber Lype[avalanglnLeger /
formproperLy namecusLomername Lype[avalangSLrlng /
formproperLy nameordervalue Lype[avalang8lgueclmal /
formproperLy nameorderLlnes LypeorgapachecommonsbeanuLllsLazyuyna8ean /

Mfbfcked Act|on Ios
1he framework allows you Lo make one or more of your AcLlonlorms properLles values a Map lnsLead
of a LradlLlonal aLomlc ob[ecL ?ou can Lhen sLore Lhe daLa from your forms dynamlc flelds ln LhaL Map
Pere ls an example of a mapbacked AcLlonlorm class
publlc loolorm exLends AcLlonlorm

prlvaLe flnal Map values new PashMap()

publlc vold seLvalue(SLrlng key Cb[ecL value)
valuespuL(key value)

publlc Cb[ecL geLvalue(SLrlng key)
reLurn valuesgeL(key)

ln lLs correspondlng !S page you can access ob[ecLs sLored ln Lhe values map uslng a speclal noLaLlon
mapname(keyname) 1he parenLheses ln Lhe bean properLy name lndlcaLe LhaL
O 1he bean properLy named mapname ls lndexed uslng SLrlngs (probably backed by a Map) and
O 1he framework should look for geL/seL meLhods LhaL Lake a SLrlng key parameLer Lo flnd Lhe
correcL subproperLy value 1he framework wlll of course use Lhe keyname value from Lhe
parenLheses when lL calls Lhe geL/seL meLhods
Pere ls a slmple example
hLmlLexL properLyvalue(foo)/
1hls wlll call Lhe geLvalue meLhod on loolorm wlLh a key value of foo Lo flnd Lhe properLy value 1o
creaLe a form wlLh dynamlc fleld names you could do Lhe followlng

for (lnL l 0 l 10 l++)
SLrlng name value(foo + l + )

hLmlLexL properLy name /

noLe LhaL Lhere ls noLhlng speclal abouL Lhe name value ?our mapbacked properLy could lnsLead be
named properLy Lhlngy or any oLher bean properLy name you prefer ?ou can even have mulLlple
mapbacked properLles on Lhe same bean
ln addlLlon Lo mapbacked properLles you can also creaLe ||stbfcked oet|es ?ou do so by creaLlng
lndexed geL/seL meLhods on your bean
publlc loolorm exLends AcLlonlorm

prlvaLe flnal LlsL values new ArrayLlsL()

publlc vold seLvalue(lnL key Cb[ecL value)
valuesseL(key value)

publlc Cb[ecL geLvalue(lnL key)
reLurn valuesgeL(key)

ln your presenLaLlon pages you access lndlvldual enLrles ln a llsLbacked properLy by uslng a dlfferenL
speclal noLaLlon llsLnamelndex 1he braces ln Lhe bean properLy name lndlcaLe LhaL Lhe bean properLy
named llsLname ls lndexed (probably backed by a LlsL) and LhaL Lhe framework should look for geL/seL
meLhods LhaL Lake an lndex parameLer ln order Lo flnd Lhe correcL subproperLy value
Whlle mapbacked AcLlonlorms provlde you wlLh more flexlblllLy Lhey do noL supporL Lhe same range
of synLax avallable Lo convenLlonal or uynaAcLlonlorms ?ou mlghL have dlfflculLy referenclng lndexed
or mapped properLles uslng a mapbacked AcLlonlorm 1he valldwhen valldaLor (slnce Apache SLruLs
121) also does noL supporL mapbacked AcLlonlorms
Act|on C|fsses
1he AcLlon class deflnes Lwo meLhods LhaL could be execuLed dependlng on your servleL envlronmenL
publlc AcLlonlorward execuLe(AcLlonMapplng mapplng
AcLlonlorm form
ServleL8equesL requesL
ServleL8esponse response)
Lhrows LxcepLlon

publlc AcLlonlorward execuLe(AcLlonMapplng mapplng
AcLlonlorm form
PLLpServleL8equesL requesL
PLLpServleL8esponse response)
Lhrows LxcepLlon

Slnce Lhe ma[orlLy of Leams uslng Lhe framework are focused on bulldlng web appllcaLlons mosL
pro[ecLs wlll only use Lhe PLLpServleL8equesL verslon A nonP11 execuLe() meLhod has been
provlded for appllcaLlons LhaL are noL speclflcally geared Lowards Lhe P11 proLocol
1he goal of an AcLlon class ls Lo process a requesL vla lLs execuLe meLhod and reLurn an AcLlonlorward
ob[ecL LhaL ldenLlfles where conLrol should be forwarded (eg a !S 1lle deflnlLlon veloclLy LemplaLe or
anoLher AcLlon) Lo provlde Lhe approprlaLe response ln Lhe ,Ic/,oJel 2 deslgn paLLern a Lyplcal
AcLlon class wlll ofLen lmplemenL loglc llke Lhe followlng ln lLs execuLe meLhod
O valldaLe Lhe currenL sLaLe of Lhe users sesslon (for example checklng LhaL Lhe user has
successfully logged on) lf Lhe AcLlon class flnds LhaL no logon exlsLs Lhe requesL can be
forwarded Lo Lhe presenLaLlon page LhaL dlsplays Lhe username and password prompLs for
logglng on 1hls could occur because a user Lrled Lo enLer an appllcaLlon ln Lhe mlddle (say
from a bookmark) or because Lhe sesslon has Llmed ouL and Lhe servleL conLalner creaLed a
new one
O lf valldaLlon ls noL compleLe valldaLe Lhe form bean properLles as needed lf a problem ls found
sLore Lhe approprlaLe error message keys as a requesL aLLrlbuLe and forward conLrol back Lo Lhe
lnpuL form so LhaL Lhe errors can be correcLed
O erform Lhe processlng requlred Lo deal wlLh Lhls requesL (such as savlng a row lnLo a daLabase)
1hls coo be done by loglc code embedded wlLhln Lhe AcLlon class lLself but should generally be
performed by calllng an approprlaLe meLhod of a buslness loglc bean
O DpdaLe Lhe serverslde ob[ecLs LhaL wlll be used Lo creaLe Lhe nexL page of Lhe user lnLerface
(Lyplcally requesL scope or sesslon scope beans dependlng on how long you need Lo keep Lhese
lLems avallable)
O 8eLurn an approprlaLe AcLlonlorward ob[ecL LhaL ldenLlfles Lhe presenLaLlon page Lo be used Lo
generaLe Lhls response based on Lhe newly updaLed beans 1yplcally you wlll acqulre a
reference Lo such an ob[ecL by calllng flndlorward on elLher Lhe AcLlonMapplng ob[ecL you
recelved (lf you are uslng a loglcal name local Lo Lhls mapplng) or on Lhe conLroller servleL lLself
(lf you are uslng a loglcal name global Lo Lhe appllcaLlon)
ln Apache SLruLs 10 AcLlons called a perform meLhod lnsLead of Lhe nowpreferred execuLe meLhod
1hese meLhods use Lhe same parameLers and dlffer only ln whlch excepLlons Lhey Lhrow 1he elder
perform meLhod Lhrows SerlveLLxcepLlon and lCLxcepLlon 1he new execuLe meLhod slmply Lhrows
1he perform meLhod may sLlll be used ln Apache SLruLs 11 buL ls deprecaLed 1he Apache SLruLs 11
meLhod slmply calls Lhe new execuLe meLhod and wraps any LxcepLlon Lhrown as a ServleLLxcepLlon
1he deprecaLed perform meLhod was removed ln Apache SLruLs 12

Lxcet|on nfnd|e
?ou can deflne an LxcepLlonPandler Lo execuLe when an AcLlons execuLe meLhod Lhrows an LxcepLlon
llrsL you need Lo subclass orgapachesLruLsacLlonLxcepLlonPandler and overrlde Lhe execuLe meLhod
?our execuLe meLhod should process Lhe LxcepLlon and reLurn an AcLlonlorward ob[ecL Lo Lell Lhe
framework where Lo forward Lo nexL 1hen you conflgure your handler ln sLruLsconflgxml llke Lhls
1hls conflguraLlon elemenL says LhaL comyourcorpLxcepLlonPandlerexecuLe wlll be called when any
lCLxcepLlon ls Lhrown by an AcLlon 1he key ls a key lnLo your message resources properLles flle LhaL can
be used Lo reLrleve an error message
If the hfnd|e ftt|bute |s not sec|f|ed the deffu|t hfnd|e stoes the excet|on |n the equest
ftt|bute unde the vf|ue of the G|obf|sLkCL1ICN_kL |obf| ke
|uIn C|fsses
1he 9loqlo lnLerface exLends AcLlon and so LhaL appllcaLlons can easlly hook lnLo Lhe AcLlonServleL
llfecycle 1hls lnLerface deflnes Lwo meLhods lnlL() and desLroy() whlch are called aL appllcaLlon sLarLup
and shuLdown respecLlvely A common use of a lugln AcLlon ls Lo conflgure or load appllcaLlonspeclflc
daLa as Lhe web appllcaLlon ls sLarLlng up
AL runLlme any resource seLup by lnlL would be accessed by AcLlons or buslness Ller classes 1he lugln
lnLerface allows you Lo seLup resources buL does noL provlde any speclal way Lo access Lhem MosL
ofLen Lhe resource would be sLored ln appllcaLlon conLexL under a known key where oLher
componenLs can flnd lL
luglns are conflgured uslng plugln elemenLs wlLhln Lhe frameworks conflguraLlon flle
1he Act|onMf|n I|eentft|on
ln order Lo operaLe successfully our conLroller servleL needs Lo know several Lhlngs abouL how each
requesL D8l should be mapped Lo an approprlaLe AcLlon class 1he requlred knowledge has been
encapsulaLed ln a !ava class named Actloo,opploq Lhe mosL lmporLanL properLles are as follows
O Lype lully quallfled !ava class name of Lhe AcLlon lmplemenLaLlon class used by Lhls mapplng
O name 1he name of Lhe form bean deflned ln Lhe conflg flle LhaL Lhls acLlon wlll use
O paLh 1he requesL D8l paLh LhaL ls maLched Lo selecL Lhls mapplng See below for examples of
how maLchlng works and how Lo use wlldcards Lo maLch mulLlple requesL D8ls
O unknown SeL Lo Lrue lf Lhls acLlon should be conflgured as Lhe defaulL for Lhls appllcaLlon Lo
handle all requesLs noL handled by anoLher acLlon Cnly one acLlon can be deflned as a defaulL
wlLhln a slngle appllcaLlon
O valldaLe SeL Lo Lrue lf Lhe valldaLe meLhod of Lhe acLlon assoclaLed wlLh Lhls mapplng should be
O forward 1he requesL D8l paLh Lo whlch conLrol ls passed when Lhls mapplng ls lnvoked 1hls ls
an alLernaLlve Lo declarlng a Lype properLy
J|t|n Act|on Mf|ns
1he ouLermosL xML elemenL musL be sLruLsconflg lnslde of Lhe sLruLsconflg elemenL Lhere are
Lhree lmporLanL elemenLs LhaL are used Lo descrlbe your acLlons
O formbeans
O globalforwards
O acLlonmapplngs
1hls secLlon conLalns your form bean deflnlLlons lorm beans are descrlpLors LhaL are used Lo creaLe
AcLlonlorm lnsLances aL runLlme ?ou use a formbean elemenL for each form bean whlch has Lhe
followlng lmporLanL aLLrlbuLes
O name A unlque ldenLlfler for Lhls bean whlch wlll be used Lo reference lL ln correspondlng
acLlon mapplngs Dsually Lhls ls also Lhe name of Lhe requesL or sesslon aLLrlbuLe under whlch
Lhls form bean wlll be sLored
O Lype 1he fullyquallfled !ava classname of Lhe AcLlonlorm subclass Lo use wlLh Lhls form bean
1hls secLlon conLalns your global forward deflnlLlons lorwards are lnsLances of Lhe AcLlonlorward class
reLurned from an AcLlons execuLe meLhod 1hese map loglcal names Lo speclflc resources (Lyplcally
!Ss) allowlng you Lo change Lhe resource wlLhouL changlng references Lo lL LhroughouL your
appllcaLlon ?ou use a forward elemenL for each forward deflnlLlon whlch has Lhe followlng
lmporLanL aLLrlbuLes
O name 1he loglcal name for Lhls forward 1hls ls used ln your AcLlons execuLe meLhod Lo
forward Lo Lhe nexL approprlaLe resource Lxample homepage
O paLh 1he conLexL relaLlve paLh Lo Lhe resource Lxample /lndex[sp or /lndexdo
O redlrecL 1rue or false (defaulL) Should Lhe AcLlonServleL redlrecL Lo Lhe resource lnsLead of
1hls secLlon conLalns your acLlon deflnlLlons ?ou use an acLlon elemenL for each of Lhe mapplngs you
would llke Lo deflne MosL acLlon elemenLs wlll deflne aL leasL Lhe followlng aLLrlbuLes
O paLh 1he appllcaLlon conLexLrelaLlve paLh Lo Lhe acLlon
O Lype 1he fully quallfled [ava classname of your AcLlon class
O name 1he name of your formbean elemenL Lo use wlLh Lhls acLlon
CLher ofLenused aLLrlbuLes lnclude
O parameLer A generalpurpose aLLrlbuLe ofLen used by sLandard AcLlons Lo pass a requlred
O roles A commadellmlLed llsL of Lhe user securlLy roles LhaL can access Lhls mapplng
Ds|n J||dcfds |n Act|onMf|ns
Slnce Apache SLruLs 120 As an appllcaLlon grows ln slze so wlll Lhe number of acLlon mapplngs
Wlldcards can be used Lo comblne slmllar mapplngs lnLo one more generlc mapplng
1he besL way Lo explaln wlldcards ls Lo show an example and walk Lhrough how lL works 1hls example
modlfles Lhe prevlous mapplng ln Lhe AcLlonMapplng Lxample secLlon Lo use wlldcards Lo maLch all
pages LhaL sLarL wlLh /edlL
! Cenerlc edlL* mapplng
1he * ln Lhe paLh aLLrlbuLe allows Lhe mapplng Lo maLch Lhe requesL D8ls /edlLSubscrlpLlon
edlL8eglsLraLlon or any oLher D8l LhaL sLarLs wlLh /edlL however /edlLSubscrlpLlon/add would noL be
maLched 1he parL of Lhe D8l maLched by Lhe wlldcard wlll Lhen be subsLlLuLed lnLo varlous aLLrlbuLes of
Lhe acLlon mapplng and lLs acLlon forwards replaclng 1 lor Lhe resL of Lhe requesL Lhe framework wlll
see Lhe acLlon mapplng and lLs acLlon forwards conLalnlng Lhe new values
Mapplngs are maLched agalnsL Lhe requesL ln Lhe order Lhey appear ln Lhe frameworks conflguraLlon
flle lf more Lhan one paLLern maLches Lhe lasL one wlns so less speclflc paLLerns musL appear before
more speclflc ones Powever lf Lhe requesL D8L can be maLched agalnsL a paLh wlLhouL any wlldcards ln
lL no wlldcard maLchlng ls performed and order ls noL lmporLanL
Wlldcard paLLerns can conLaln one or more of Lhe followlng speclal Lokens
* MaLches zero or more characLers excludlng Lhe slash (/) characLer
** MaLches zero or more characLers lncludlng Lhe slash (/) characLer
1he backslash characLer ls used as an escape sequence 1hus * maLches Lhe characLer
asLerlsk (*) and maLches Lhe characLer backslash ()
ln Lhe acLlon mapplng and acLlon forwards Lhe wlldcardmaLched values can be accessed wlLh Lhe Loken
n where n ls a number from 1 Lo 9 lndlcaLlng whlch wlldcardmaLched value Lo subsLlLuLe 1he whole
requesL D8l can be accessed wlLh Lhe 0 Loken

Conf|u|n A||cft|ons
?ou learned wrlLlng Lhe formbean and acLlonmapplng porLlons of Lhe conflguraLlon flle 1hese
elemenLs usually play an lmporLanL role ln Lhe developmenL of an appllcaLlon 1he oLher elemenLs ln a
conflguraLlon flle Lend Lo be sLaLlc you seL Lhem once and leave Lhem alone
1hese sLaLlc conflguraLlon elemenLs are
O conLroller
O messageresources
O plugln

Conf|ue ou A||cft|on Io Modu|es
very llLLle ls requlred ln order Lo sLarL Laklng advanLage of Lhe module feaLure !usL go Lhrough Lhe
followlng sLeps
1 repare a conflguraLlon flle for each module
2 lnform Lhe conLroller of your module
3 Dse AcLlons Lo refer Lo your pages
Module conflguraLlon llles
8ack ln verslon 10 a few booLsLrap opLlons were placed ln Lhe webxml flle and Lhe bulk of Lhe
conflguraLlon was done ln a slngle sLruLsconflgxml flle Cbvlously Lhls wasnL ldeal for a Leam
envlronmenL slnce mulLlple users had Lo share Lhe same conflguraLlon flle
Slnce verslon 11 you have Lwo opLlons you can llsL mulLlple sLruLsconflg flles as a commadellmlLed
llsL or you can subdlvlde a larger appllcaLlon lnLo modules
WlLh Lhe advenL of modules a glven module has lLs own conflguraLlon flle 1hls means each Leam (each
module would presumably be developed by a slngle Leam) has Lhelr own conflguraLlon flle and Lhere
should be a loL less conLenLlon when Lrylng Lo modlfy lL
lnformlng Lhe onLroller
Slnce verslon 10 you llsLed your conflguraLlon flle as an lnlLlallzaLlon parameLer Lo Lhe acLlon servleL ln
webxml 1hls ls sLlll done slnce verslon 11 buL Lhe parameLer can be exLended ln order Lo Lell Lhe
framework machlnery abouL your dlfferenL modules you speclfy mulLlple conflg lnlLlallzaLlon
parameLers wlLh a sllghL LwlsL ?oull sLlll use conflg Lo Lell Lhe AcLlonServleL abouL your defaulL
module however for each addlLlonal module you wlll llsL an lnlLlallzaLlon parameLer named conflg
/moJole where /moJole ls Lhe preflx for your module (Lhls geLs used when deLermlnlng whlch D8ls fall
under a glven module so choose someLhlng meanlngful!) lor example


Pere we have Lwo modules Cne happens Lo be Lhe defaulL module ldenLlfled by Lhe paramname of
conflg and Lhe oLher wlll be uslng Lhe module preflx /module1 based on Lhe paramname lL was
glven (conflg/module1) 1he conLroller ls conflgured Lo flnd Lhe respecLlve conflguraLlon flles under
/WL8lnl/conf/ (whlch ls Lhe recommended place Lo puL all conflguraLlon flles) reLLy slmple!
(1he sLruLsdefaulLxml would be equlvalenL Lo whaL mosL folks call sLruLsconflgxml l [usL llke Lhe
symmeLry of havlng all my module conflguraLlon flles belng named sLruLs moJolexml )
SwlLchlng Modules
1here are Lhree approaches for swlLchlng from one module Lo anoLher
1 ?ou can use Lhe orgapachesLruLsacLlonsSwlLchAcLlon from Lhe LxLras !A8
2 you can use a forward (global or local) and speclfy Lhe conLexL8elaLlve aLLrlbuLe wlLh a value
of Lrue
3 or you can speclfy Lhe module parameLer as parL of any of Lhe SLruLs !S hyperllnk Lags
(lnclude lmg Llnk 8ewrlLe or lorward)
?ou can use orgapachesLruLsacLlonsSwlLchAcLlon llke so

acLlon paLh/LoModule


now Lo change Lo Module8 we would use a D8l llke Lhls

lf you are uslng Lhe defaulL module as well as named modules (llke /module8) you can swlLch
back Lo Lhe defaulL module wlLh a D8l llke Lhls

Peres an example of a global forward
forward nameLoModule8

?ou could do Lhe same Lhlng wlLh a local forward declared ln an AcLlonMapplng
forward namesuccess


Dslng Lhe module parameLer wlLh a hyperllnk Lag ls even slmpler
hLmlllnk module/module8 paLh/lndexdo/

1he Jeb A||cft|on De|oent Desc|to
1he flnal sLep ln seLLlng up Lhe appllcaLlon ls Lo conflgure Lhe appllcaLlon deploymenL descrlpLor (sLored
ln flle WL8lnl/webxml ) Lo lnclude all Lhe framework or 1agllb componenLs LhaL are requlred Dslng
Lhe deploymenL descrlpLor for Lhe example appllcaLlon as a gulde we see LhaL Lhe followlng enLrles
need Lo be creaLed or modlfled
Add an enLry deflnlng Lhe acLlon servleL lLself along wlLh Lhe approprlaLe lnlLlallzaLlon parameLers Such
an enLry mlghL look llke Lhls
JAkNING 1he framework wlll noL operaLe correcLly lf you deflne more Lhan one servleL elemenL for
a conLroller servleL or a subclass of Lhe sLandard conLroller servleL class 1he conLroller servleL MD1 be
a web appllcaLlon wlde slngleLon
1here are Lwo common approaches Lo deflnlng Lhe D8Ls LhaL wlll be processed by Lhe conLroller servleL
preflx maLchlng and exLenslon maLchlng An approprlaLe mapplng enLry for each approach wlll be
descrlbed below
reflx maLchlng means LhaL you wanL all D8Ls LhaL sLarL (afLer Lhe conLexL paLh parL) wlLh a parLlcular
value Lo be passed Lo Lhls servleL Such an enLry mlghL look llke Lhls
whlch means LhaL a requesL D8l Lo maLch Lhe /logon paLh descrlbed earller mlghL look llke Lhls
where /myappllcaLlon ls Lhe conLexL paLh under whlch your appllcaLlon ls deployed
LxLenslon mapplng on Lhe oLher hand maLches requesL D8ls Lo Lhe acLlon servleL based on Lhe facL LhaL
Lhe D8l ends wlLh a perlod followed by a deflned seL of characLers lor example Lhe !S processlng
servleL ls mapped Lo Lhe *[sp paLLern so LhaL lL ls called Lo process every !S page LhaL ls requesLed 1o
use Lhe *do exLenslon (whlch lmplles do someLhlng) Lhe mapplng enLry would look llke Lhls
and a requesL D8l Lo maLch Lhe /logon paLh descrlbed earller mlghL look llke Lhls
JAkNING 1he framework wlll noL operaLe correcLly lf you deflne more Lhan one servleLmapplng
elemenL for Lhe conLroller servleL
JAkNING lf you are uslng Lhe new module supporL slnce verslon 11 you should be aware LhaL on|
exLenslon mapplng ls supporLed