Sie sind auf Seite 1von 23

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 ComponentsStage1Webxm| I||e
?xml verslon10 encodlngu18?
ebapp verslon23 xmlnshLLp//[avasuncom/xml/ns/[avaee
xmlnsxslhLLp//org/2001/x,SchemalnsLance
xslschemaocaLlonhLLp//[avasuncom/xml/ns/[avaee
hLLp//[avasuncom/xml/ns/[avaee/ebapp_2_3xsd
servleL
servleLnameacLlon/servleLname
servleLclassorgapachesLruLsacLloncLlonServleL/servleLclass
lnlLparam
paramnameconflg/paramname
paramvalue/WL8ln/sLruLsconflgxml/paramvalue
/lnlLparam
lnlLparam
paramnamedebug/paramname
paramvalue2/paramvalue
/lnlLparam
lnlLparam
paramnamedeLall/paramname
paramvalue2/paramvalue
/lnlLparam
loadonsLarLup2/loadonsLarLup
/servleL
servleLmapplng
servleLnameacLlon/servleLname
urlpaLLern*do/urlpaLLern
/servleLmapplng
sesslonconflg
sesslonLlmeouL0/sesslonLlmeouL
/sesslonconflg
elcomefllellsL
elcomefllelogln[sp/elcomeflle
/elcomefllellsL
[spconflg
LagllbLagllburl/WL8ln/sLruLsbeanLld/Lagllburl
LagllblocaLlon/WL8ln/sLruLsbeanLld/LagllblocaLlon
/Lagllb/[spconflg /ebapp
lnLeracLlon beLeen ComponenLsSLage2SLruLsconflgxml 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-conIig12.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
messageresources parameLercom/LL/ppllcaLlon8esource/
plugln classnameorgapachesLruLsLlles1lleslugln
seLproperLy properLydeflnlLlonsconflg value/WL8ln/Lllesdefsxml /
/plugln
" IalldaLor plugln
plugln classnameorgapachesLruLsvalldaLorIalldaLorlugln
seLproperLy
properLypaLhnames
value/WL8ln/valldaLorrulesxml/WL8ln/valldaLlonxml/
/plugln
/sLruLsconflg
lnLeracLlon beLeen ComponenLsSLageCreaLlon and ueflnlLlon of orm 8eans
publlc class oglnorm exLends orgapachesLruLsacLloncLlonorm
prlvaLe SLrlng username
prlvaLe SLrlng passord
publlc SLrlng geLusername()
SysLemouLprlnLln(lnslde geLLer +username)
reLurn username

publlc vold seLusername(SLrlng username)


SysLemouLprlnLln(lnslde seLLer +username)
Lhlsusername username

publlc SLrlng geLassord()


reLurn passord

publlc vold seLassord(SLrlng passord)


Lhlspassord 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 formbean 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 ComponenLsSLage4ppllcaLlon resourcesproperLles flle
W uslng Lhe resource lle for IalldaLlon
W # valldaLor
W errorslnvalld0 ls lnvalld
W errorsmaxlengLh0 can noL be greaLer Lhan 1 characLers
W errorsmlnlengLh0 can noL be less Lhan 1 characLers
W errorsrange0 ls noL ln Lhe range 1 Lhrough 2
W # uslng Lhe resource flle for abels
W elcomeLlLleSLruLs ppllcaLlon
W elcomeheadlngSLruLs ppllcaLlons ln neLbeans"
W elcomemessagelLs easy Lo creaLe SLruLs appllcaLlons lLh neL8eans
lnLeracLlon beLeen ComponenLsSLage3ppllcaLlon 1he cLlon Class flle
publlc class oglncLlon exLends orgapachesLruLsacLloncLlon
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 (loglnormgeLusername()equals(loglnormgeLassord()))
reLurn mapplngflndorard(SuCCLSS)
else
reLurn mapplngflndorard(lu8L)

lnLeracLlon beLeen ComponenLsSLage6ppllcaLlon 1he !S flle


8ululnC lnu1 !S lL
hLml
head
meLa hLLpequlvConLenL1ype conLenLLexL/hLml charseLu18
LlLle!S age/LlLle
/head
body
dlv sLylecolorred
hLmlerrors /
/dlv
hLmlform acLlon/ogln
user name hLmlLexL nameoglnorm properLyusername / br
assord hLmlpassord nameoglnorm properLypassord / br
hLmlsubmlL valueogln /
/hLmlform
/body
/hLml
8ulldlng an ouLpuL !S flle
1he bean Lag
W beanrlLe properLyuname nameform1/
W 1hls Lag rlLes Lhe sLrlng equlvalenL of Lhe speclfled bean or bean properLy Lo Lhe currenL PLLp8esponse ob[ecL
W beanmessage keylabelCancel/
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//[2eemassllghLcom/ChapLer4hLml
1he oglc 1ag
W LxampleloglclLeraLe
lsLSLrlng llsL,sg newrraylsLSLrlng()
llsL,sgadd(,essage )
llsL,sgadd(,essage 8)
llsL,sgadd(,essage C)
llsL,sgadd(,essage u)
requesLseLLLrlbuLe(llsL,sg llsL,sg)
W loglclLeraLe namellsL,sg ldllsL,sgld
p lsL ,essages beanrlLe namellsL,sgld/ /p
W /loglclLeraLe
W oglc noLLmpLy 1ag (loglcnoLLmpLy/loglcnoLLmpLy)
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
$__loqlojsp
$__opploqjsp
$__Jnomjsp
$__ojsp
$__WL8INI
$__xm
$__sttsnooflqxm
$__sLruLshLmlLld
$__sLruLsbeanLld
$__sLruLsloglcLld
$_%||b
$__sLruLs[ar
$__commonslogglng[ar
$_%c|asses
$__src
$__pplnftloosonspoptls
$__oqlontloojff
$__sopploqntloojff
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

Das könnte Ihnen auch gefallen