WhaL ls SLruLs?

W Framework Ior building Web applications Iastly and easily.
W Open source Iramework Irom Apache SoItware Foundation.
W Based on MVC-Model 2 pattern
W Struts Iramework was implemented based on 3 popular design
patterns.
W Front Controller design pattern.
W Application Controller design pattern.
W MVC design pattern
ronL ConLroller deslgn paLLernťŴ
!roblem:
I want a centralized access point Ior presentation tier request handling.
Description oI the problem:
Always system requires a centralized access point Ior request handling.
Without centralized access point control code that is common across multiple
requests are duplicated in various places. This gives you code duplication
problem, because oI this maintenance will be increased.
Solution:
Use the Front Controller as the initial point oI contact Ior handling all
the request. Front Controller centralizes controller logic and avoids the code
duplication problem.
ppllcaLlon ConLroller ueslgn paLLernťŴ
!roblem:
I want to centralize and modularize action management and the view
management.
Description oI the problem:
There are typically two decisions to be made upon the arrival oI each
request.
The incoming request must be resolved to an action that provides the service to
the incoming request. This is called Action Management.
The appropriate view is located and dispatched. This is called View Management.
You can encapsulate action management and view management within Front
Controller but as the application grows code becomes more complex. It is good
idea to separate this code and place this in a separate component Ior improving
modularity and reusability. Because oI this we can reuse action management and
view management code.
Solution:
Use an application controller to centralise and reuse action
management code and view management code
,IC deslgn paLLernťŴ
W Because oI MVC design pattern we have 3 layers in struts based application.
1) Struts !resentation Layer:-
Struts presentation layer contains the Iollowing components.
W JS!s/HTMLs
W Struts custom tags.
W Form Beans.
W Message Bundles.
2) Controller Layer oI struts:-
Following are the various components which you can use in struts
controller layer.
W ActionServlet.
W Request!rocessor.
W Action class.
3) Model Layer:- mainly contains business components and persistent
components.
W Struts is not dealing with model layer i.e in struts there is no predeIined
component Ior model layer like presentation layer and controller layer.
W You can use any oI the Iollowing in the model layer.
W Simple JDBC components.
W DAO ¹ JDBC.
W DAO ¹ Hibernate.
W Spring.
W EJB.
W Web Services. Etc.
Login Form (Iorm bean) Struts-conIig.xml
Action
Servlet
(web.xml)
Request
!rocessor
Action Class
login.jsp
welcome.jsp
First Name:
Last Name
SubmlL SubmlL
1
2,10 3,11 4,12 5,13
6 7
8 9
14
Welcome
XYZ.
Data Base
execute(4 param)
¦
. . .
. . . //Business Logic
. . .
return
mapping.IindForward('succes
s¨);
}
Architecture of Struts1.3 (MVC)
!resentation Layer Controller Layer Model Layer
!ersistence Layer
Interact|on between ComponentsŦStageŴ1ŦWebŦxm| I||e
Ƽ?xml verslonƹƍ1Ŧ0ƍ encodlngƹƍu1Ŵ8ƍ?ƽ
ƼebŴapp verslonƹƍ2Ŧ3ƍ xmlnsƹƍhLLpť//[avaŦsunŦcom/xml/ns/[avaeeƍ
xmlnsťxslƹƍhLLpť//ŦŦorg/2001/x,SchemaŴlnsLanceƍ
xslťschemaocaLlonƹƍhLLpť//[avaŦsunŦcom/xml/ns/[avaee
hLLpť//[avaŦsunŦcom/xml/ns/[avaee/ebŴapp_2_3Ŧxsdƍƽ
ƼservleLƽ
ƼservleLŴnameƽacLlonƼ/servleLŴnameƽ
ƼservleLŴclassƽorgŦapacheŦsLruLsŦacLlonŦcLlonServleLƼ/servleLŴclassƽ
ƼlnlLŴparamƽ
ƼparamŴnameƽconflgƼ/paramŴnameƽ
ƼparamŴvalueƽ/WL8Ŵln/sLruLsŴconflgŦxmlƼ/paramŴvalueƽ
Ƽ/lnlLŴparamƽ
ƼlnlLŴparamƽ
ƼparamŴnameƽdebugƼ/paramŴnameƽ
ƼparamŴvalueƽ2Ƽ/paramŴvalueƽ
Ƽ/lnlLŴparamƽ
ƼlnlLŴparamƽ
ƼparamŴnameƽdeLallƼ/paramŴnameƽ
ƼparamŴvalueƽ2Ƽ/paramŴvalueƽ
Ƽ/lnlLŴparamƽ
ƼloadŴonŴsLarLupƽ2Ƽ/loadŴonŴsLarLupƽ
Ƽ/servleLƽ
ƼservleLŴmapplngƽ
ƼservleLŴnameƽacLlonƼ/servleLŴnameƽ
ƼurlŴpaLLernƽ*ŦdoƼ/urlŴpaLLernƽ
Ƽ/servleLŴmapplngƽ
ƼsesslonŴconflgƽ
ƼsesslonŴLlmeouLƽ0Ƽ/sesslonŴLlmeouLƽ
Ƽ/sesslonŴconflgƽ
ƼelcomeŴflleŴllsLƽ
ƼelcomeŴflleƽloglnŦ[spƼ/elcomeŴflleƽ
Ƽ/elcomeŴflleŴllsLƽ
Ƽ[spŴconflgƽ
ƼLagllbƽƼLagllbŴurlƽ/WL8Ŵln/sLruLsŴbeanŦLldƼ/LagllbŴurlƽ
ƼLagllbŴlocaLlonƽ/WL8Ŵln/sLruLsŴbeanŦLldƼ/LagllbŴlocaLlonƽ
Ƽ/LagllbƽƼ/[spŴconflgƽ Ƽ/ebŴappƽ
lnLeracLlon beLeen ComponenLsŦSLageŴ2ŦSLruLsŴconflgŦxml lle
xml version÷"1.0" encoding÷"UTF-8"
DOCTY!E struts-conIig !UBLIC
"-//Apache SoItware Foundation//DTD Struts ConIiguration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-conIig¸1¸2.dtd"
struts-conIig
Iorm-beans
Iorm-bean name÷"LoginForm" type÷"com.tt.LoginForm"/
/Iorm-beans
global-exceptions/global-exceptions
global-Iorwards
Iorward name÷"welcome" path÷"/Welcome.do"/
/global-Iorwards
action-mappings
action input÷"/login.jsp"
name÷"LoginForm"
path÷"/Login" scope÷"session"
type÷"com.tt.LoginAction"
Iorward name÷"success" path÷"/success.jsp" /
Iorward name÷"Iailure" path÷"/Iailure.jsp" /
/action
action path÷"/Welcome" Iorward÷"/welcomeStruts.jsp"/
/action-mappings
Ƽ"Ÿ,applng ppllcaLlon 8esources ŴŴƽ
ƼmessageŴresources parameLerƹƍcom/LL/ppllcaLlon8esourceƍ/ƽ
ƼplugŴln classnameƹƍorgŦapacheŦsLruLsŦLllesŦ1llesÞluglnƍ ƽ
ƼseLŴproperLy properLyƹƍdeflnlLlonsŴconflgƍ valueƹƍ/WL8Ŵln/LllesŴdefsŦxmlƍ /ƽ
Ƽ/plugŴlnƽ
Ƽ"ŴŴ ƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹ IalldaLor plugln ƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹƹ ŴŴƽ
ƼplugŴln classnameƹƍorgŦapacheŦsLruLsŦvalldaLorŦIalldaLorÞluglnƍƽ
ƼseLŴproperLy
properLyƹƍpaLhnamesƍ
valueƹƍ/WL8Ŵln/valldaLorŴrulesŦxmlţ/WL8Ŵln/valldaLlonŦxmlƍ/ƽ
Ƽ/plugŴlnƽ
Ƽ/sLruLsŴconflgƽ
lnLeracLlon beLeen ComponenLsŦSLageŴŦCreaLlon and ueflnlLlon of orm 8eans
publlc class oglnorm exLends orgŦapacheŦsLruLsŦacLlonŦcLlonorm ž
prlvaLe SLrlng usernameŤ
prlvaLe SLrlng passordŤ
publlc SLrlng geLusername() ž
SysLemŦouLŦprlnLln(ƍlnslde geLLer ƍ+username)Ť
reLurn usernameŤ
ſ
publlc vold seLusername(SLrlng username) ž
SysLemŦouLŦprlnLln(ƍlnslde seLLer ƍ+username)Ť
LhlsŦusername ƹ usernameŤ
ſ
publlc SLrlng geLÞassord() ž
reLurn passordŤ
ſ
publlc vold seLÞassord(SLrlng passord) ž
LhlsŦpassord ƹ passordŤ
ſ
ſ
orm 8ean
W form bean ls a Lype of !ava` beanŦ form bean ls an lnsLance of a subclass of an
cLlonorm classţ hlch sLores P1, form daLa from a submlLLed cllenL requesL or
LhaL can sLore lnpuL daLa from a SLruLs acLlon llnk LhaL a user cllckedŦ n P1,
form comprlses flelds ln hlch Lhe user can enLer lnformaLlonŦ
W formŴbean mapplng ls an enLry ln a SLruLs conflguraLlon flle LhaL maps a form
bean Lo an acLlonŦ
W When a broser submlLs an P1, formţ Lhe SLruLs acLlon servleL does as follosť
W ooks aL Lhe fleld names from Lhe P1, form
W ,aLches Lhem Lo Lhe properLlesƌ names ln Lhe form bean
W uLomaLlcally calls Lhe seL meLhods of Lhese varlables Lo puL Lhe values reLrleved
from Lhe P1, form
lnLeracLlon beLeen ComponenLsŦSLageŴ4ŦppllcaLlon resourcesŦproperLles flle
W uslng Lhe resource lle for IalldaLlon
W # ŴŴ valldaLor ŴŴ
W errorsŦlnvalldƹž0ſ ls lnvalldŦ
W errorsŦmaxlengLhƹž0ſ can noL be greaLer Lhan ž1ſ characLersŦ
W errorsŦmlnlengLhƹž0ſ can noL be less Lhan ž1ſ characLersŦ
W errorsŦrangeƹž0ſ ls noL ln Lhe range ž1ſ Lhrough ž2ſŦ
W # ŴŴ uslng Lhe resource flle for abels ŴŴ
W elcomeŦLlLleƹSLruLs ppllcaLlon
W elcomeŦheadlngƹSLruLs ppllcaLlons ln neLbeans"
W elcomeŦmessageƹlLƌs easy Lo creaLe SLruLs appllcaLlons lLh neL8eans
lnLeracLlon beLeen ComponenLsŦSLageŴ3ŦppllcaLlon 1he cLlon Class flle
publlc class oglncLlon exLends orgŦapacheŦsLruLsŦacLlonŦcLlon ž
prlvaLe flnal sLaLlc SLrlng SuCCLSS ƹ ƍsuccessƍŤ
prlvaLe flnal sLaLlc SLrlng lu8L ƹ ƍfallureƍŤ
publlc cLlonorard execuLe(cLlon,applng mapplngţ
cLlonorm formţ
PLLpServleL8equesL requesLţ
PLLpServleL8esponse response)
Lhros LxcepLlon ž
oglnorm loglnorm ƹ (oglnorm) formŤ
lf (loglnormŦgeLusername()Ŧequals(loglnormŦgeLÞassord())) ž
reLurn mapplngŦflndorard(SuCCLSS)Ť
ſ else ž
reLurn mapplngŦflndorard(lu8L)Ť
ſ
ſ
ſ
lnLeracLlon beLeen ComponenLsŦSLageŴ6ŦppllcaLlon 1he !SÞ flle
8ululnC lnÞu1 !SÞ lL
ƼhLmlƽ
Ƽheadƽ
ƼmeLa hLLpŴequlvƹƍConLenLŴ1ypeƍ conLenLƹƍLexL/hLmlŤ charseLƹu1Ŵ8ƍƽ
ƼLlLleƽ!SÞ ÞageƼ/LlLleƽ
Ƽ/headƽ
Ƽbodyƽ
Ƽdlv sLyleƹƍcolorťredƍƽ
ƼhLmlťerrors /ƽ
Ƽ/dlvƽ
ƼhLmlťform acLlonƹƍ/oglnƍ ƽ
user name ť ƼhLmlťLexL nameƹƍoglnormƍ properLyƹƍusernameƍ /ƽ Ƽbrƽ
Þassord ť ƼhLmlťpassord nameƹƍoglnormƍ properLyƹƍpassordƍ /ƽ Ƽbrƽ
ƼhLmlťsubmlL valueƹƍoglnƍ /ƽ
Ƽ/hLmlťformƽ
Ƽ/bodyƽ
Ƽ/hLmlƽ
8ulldlng an ouLpuL !SÞ flleŦ
1he bean Lag
W ƼbeanťrlLe properLyƹƍunameƍ nameƹƍform1ƍ/ƽ
W 1hls Lag rlLes Lhe sLrlng equlvalenL of Lhe speclfled bean or bean properLy Lo Lhe currenL PLLp8esponse ob[ecLŦ
W Ƽbeanťmessage keyƹƍlabelŦCancelƍ/ƽ
W 1hls Lag allos locale speclflc messages Lo be dlsplayed by looklng up Lhe message ln Lhe appllcaLlon resources ŦproperLles flleŦ
W or more vlslL 8esourceť hLLpť//[2eeŦmassllghLŦcom/ChapLer4ŦhLml
1he oglc 1ag
W LxampleƼloglcťlLeraLeƽ
lsLƼSLrlngƽ llsL,sg ƹ newrraylsLƼSLrlngƽ()Ť
llsL,sgŦadd(ƍ,essage ƍ)Ť
llsL,sgŦadd(ƍ,essage 8ƍ)Ť
llsL,sgŦadd(ƍ,essage Cƍ)Ť
llsL,sgŦadd(ƍ,essage uƍ)Ť
requesLŦseLLLrlbuLe(ƍllsL,sgƍţ llsL,sg)Ť
W ƼloglcťlLeraLe nameƹƍllsL,sgƍ ldƹƍllsL,sgldƍƽ
Ƽpƽ lsL ,essages ƼbeanťrlLe nameƹƍllsL,sgldƍ/ƽ Ƽ/pƽ
W Ƽ/loglcťlLeraLeƽ
W oglc noLLmpLy 1ag (ƼloglcťnoLLmpLyƽŦŦŦƼ/loglcťnoLLmpLyƽ)
W oglc Lqual and noLLqual 1ags
W oglc greaLerLqual and greaLer1han 1ags
W oglc lessLqual and less1han 1ags
W
ulrecLory SLrucLure
W Where and haL
W Shopp|ng App||cat|on
$__loqloŦjsp
$__opploqŦjsp
$__JnomŦjsp
$__oŦjsp
$__WL8ŴINI
$__Ŧxm
$__sttsŴnooflqŦxm
$__sLruLsŴhLmlŦLld
$__sLruLsŴbeanŦLld
$__sLruLsŴloglcŦLld
$_%||b
» $__sLruLsŦ[ar
» $__commonsŴlogglngŦ[ar
$_%c|asses
$__src
$__pplnftlooŴsonsŦpoptls
$__oqlontlooŦjff
$__sopploqntlooŦjff
Ile ComponenLs
W HTML
W Struts ActionForms objects
W Java Server !ages
W Struts Tags
W Custom Tags
W Java Resource Bundles etc
View Components Action Forms:
WActionForm Beans
- Extends the ActionForm class
- Create one for each input form in the application
- JavaBeans style properties must match form entries
W There are 6 types oI Iorm beans:-
ActionForm.
DynaActionForm.
ValidatorForm.
DynaValidatorForm.
ValidatorActionForm.
DynaValidatorActionForm
W Override:
;,id,te() - optional
reset() - optional
ConLroller ComponenLs
ueploymenL descrlpLor for SLruLs
Controller Components:
org.,5,che.struts.ActionSer;et is a main component oI
Struts
org.,5,che.struts.,ction.Action - extension oI controller
component (deIined in action mapping in the conIiguration Iile)
Struts use conIiguration Iile called
strutsconfig.xm
This Iile provides Ior a conIiguration oI controller components (actions),
resources, model components: JavaBeans and other resources
ConLroller ComponenLs
W 1he acLlon mapplng example
W ,ctionm,55ings
W ,ction 5,th="/,ddBookAction°
ty5e=
W "edu.de5,u.struts_ex,m5e.BookIn5utAction"
W n,me=",ddBookBe,n"
W sco5e="request"
W in5ut="/,dd_book.js5"
W forw,rd n,me="Go"
5,th="/resut.js5"/
W forw,rd n,me="c,nce"
5,th="/index.htm"/
W /,ction
Action mapping name
mapping type
Form Bean name
Iorward deIinition
8equesL Þrocesslng
Web Browser
displays page
Controller
ActionForm
Action
Model
struts-conIig
JS! !age
1
2
3
4
5
6
7
8
8eneflLs of SLruLs rameorks
W !rovide developers ith modular, reusable and
extensible soft,re components
W Modular Irameworks reduce the time and eIIort and
investment required to understand and maintain
existing soItware
W Contributes improvements in programmer
productivity, as well as enhancing the quality,
perIormance, reliability oI the soItware.
W Reduces the cost oI building and maintaining
soItware