Struts 2 Basics
source: Apache Struts 2 documentation
Topics
Key core features Request processing life-cycle Improvements made in Struts 2 (over Struts 1) Action Result & Result type Interceptor alidation !onfiguration files "ac#ages
source: Apache Struts 2 documentation
Key Core Features
source: Apache Struts 2 documentation
Key Core Features
"lugga$le frame%or# arc&itecture t&at allo%s request lifecycles to $e customi'ed for eac& action( )le*i$le validation frame%or# t&at allo%s validation rules to $e decoupled from action code( +ierarc&ical approac& to internationali'ation t&at simplifies locali'ing applications(
4
source: Apache Struts 2 documentation
Key Core Features
Integrated dependency in,ection engine t&at manages component lifecycle and dependencies( (-y default. t&e frame%or# utili'es Spring for dependency in,ection() /odular configuration files t&at use pac#ages and namespaces to simplify managing large pro,ects %it& &undreds of actions( 0ava 1 annotations t&at reduce configuration over&ead( (0ava 1(2 is t&e minimum platform()
source: Apache Struts 2 documentation
Struts 2 Request Processing Life-Cycle
source: Apache Struts 2 documentation
Struts 2 Architecture
source: Apache Struts 2 documentation
Struts 2 Request Processing
1(3&e %e$ $ro%ser requests a resource (4mypage(action. 4reports4myreport(pdf. et cetera) 2(3&e )ilter 5ispatc&er loo#s at t&e request and determines t&e appropriate Action 6(3&e Interceptors automatically apply common functionality to t&e request. li#e %or#flo%. validation. and file upload &andling 2(3&e Action met&od e*ecutes. usually storing and4or retrieving information from a data$ase 1(3&e Result renders t&e output to t&e $ro%ser. $e it +3/7. images. "5). or somet&ing else
source: Apache Struts 2 documentation
I pro!e ents a"e in Struts 2
source: Apache Struts 2 documentation
Si plifie" #esign
"rogramming t&e a$stract classes instead of interfaces is one of design pro$lem of struts1 frame%or# t&at &as $een resolved in t&e struts 2 frame%or#( /ost of t&e Struts 2 classes are $ased on interfaces and most of its core interfaces are +33" independent(
source: Apache Struts 2 documentation
10
Intelligent #efaults
/ost configuration elements &ave a default value %&ic& can $e set according to t&e need( 3&ere are *ml-$ased default configuration files t&at can $e e*tended according to t&e need(
source: Apache Struts 2 documentation
11
I pro!e" Results
8nli#e ActionForwards. Struts 2 Results provide fle*i$ility to create multiple type of outputs
source: Apache Struts 2 documentation
12
P$%$ Action&s
Struts 1
Actions in Struts1 &ave dependencies on t&e servlet A"I since t&e HttpServletRequest and HttpServletResponse o$,ects are passed to t&e execute() met&od Any ,ava class %it& execute() met&od can $e used as an Action class( Actions to $e neutral to t&e underlying frame%or#
source: Apache Struts 2 documentation
Struts 2
13
'o (ore ActionFor &s
ActionForms feature is no more #no%n to t&e Struts 2 frame%or#( Simple 0ava-ean flavored actions are used to put properties directly
source: Apache Struts 2 documentation
14
)nhance" Testa*ility
Struts 2 Actions are +33" independent and frame%or# neutral(
3&is ena$les to test struts applications very easily %it&out resorting to moc# o$,ects(
source: Apache Struts 2 documentation
15
Better Tag Features
Struts 2 tags ena$les to add style s&eetdriven mar#up capa$ilities
9ou can create consistent pages %it& less code Struts 2 tag mar#up can $e altered $y c&anging an underlying styles&eet(
Struts 2 tags are more capa$le and result oriented( -ot& 0S" and )ree/ar#er tags are fully supported(
source: Apache Struts 2 documentation
16
Annotation Support
0ava 1 annotations can $e used as an alternative to :/7 and 0ava properties configuration
source: Apache Struts 2 documentation
17
Stateful Chec+*o,es
Struts 2 c&ec#$o*es do not require special &andling for false values
source: Apache Struts 2 documentation
18
-uic+ Start
/any c&anges can $e made on t&e fly %it&out restarting a %e$ container(
source: Apache Struts 2 documentation
19
Custo i.a*le Controller
Struts 2 lets to customi'e t&e request &andling per action. if desired(
Struts 1 lets to customi'e t&e request processor per module.
source: Apache Struts 2 documentation
20
)asy Spring Integration
Struts 2 Actions are Spring-a%are( 0ust need to add Spring $eans;
source: Apache Struts 2 documentation
21
)asy Plug-in&s
Struts 2 e*tensions can $e added $y dropping in a 0AR(
<o additional :/7 or properties files( /etadata is e*pressed t&roug& convention and annotation
source: Apache Struts 2 documentation
22
A/a, Support
A0A: client side validation Remote form su$mission support (%or#s %it& t&e su$mit tag as %ell) An advanced div template t&at provides dynamic reloading of partial +3/7 An advanced template t&at provides t&e a$ility to load and evaluate 0avaScript remotely An A0A:-only ta$$ed "anel implementation A ric& pu$-su$ event model Interactive auto complete tag
source: Apache Struts 2 documentation
23
Struts 0
!s1 Struts 2
Action Action)orm Action)or%ard struts-config(*ml ActionServlet Request"rocessor validation(*ml
Action Action or "=0= Result struts(*ml )ilter5ispatc&er Interceptors Action-validation(*ml
24
source: Apache Struts 2 documentation
Action
source: Apache Struts 2 documentation
25
Action
Is a $asic >unit-of-%or#> "=0= class %&ic& &as execute() met&od
source: Apache Struts 2 documentation
26
Action (apping Configuration
/aps an identifier to a Action class( ?&en a request matc&es t&e action@s name. t&e frame%or# uses t&e mapping to determine &o% to process t&e request( Also specifies
A set of result types A set of e*ception &andlers An interceptor stac#
27
source: Apache Struts 2 documentation
),a ple2 Action (apping
Aaction nameB>7ogon> classB>tutorial(7ogon>C Aresult typeB>redirect-action>C/enuA4resultC Aresult nameB>input>C4tutorial47ogon(,spA4resultC A4actionC
source: Apache Struts 2 documentation
28
Action 'a es 3name attri*ute4
3&e name attri$ute is matc&ed a part of t&e location requested $y a $ro%ser (or ot&er +33" client)( 3&e frame%or# %ill drop t&e &ost and application name and t&e e*tension. and matc& %&at@s in t&e middle( D*ampleE
A request for [Link] ! mailreader/"[Link] %ill map to t&e "elcome action(
source: Apache Struts 2 documentation
29
Action 'a es
?it&in an application. t&e lin# to an action is usually generated $y a Struts 3ag( 3&e tag can specify t&e action $y name. and t&e frame%or# %ill render t&e default e*tension and anyt&ing else t&at is needed(
AsEform actionB>+ello>C AsEte*tfield la$elB>"lease enter your name> nameB>name>4C AsEsu$mit4C A4sEformC
source: Apache Struts 2 documentation
30
),a ple2 Plain %SP
A&tmlC A&eadCAtitleCAdd -log DntryA4titleCA4&eadC A$odyC Aform actionB>save(action> met&odB>post>C 3itleE Ainput typeB>te*t> nameB>title> 4CA$r4C DntryE Ate*tarea ro%sB>6> colsB>21> nameB>entry>CA4te*tareaC A$r4C Ainput typeB>su$mit> valueB>Add>4C A4formC A4$odyC A4&tmlC
31
source: Apache Struts 2 documentation
),a ple2 5sing Struts Tag
AFG tagli$ prefi*B>s> uriB>4?D--I<)4struts-tags(tld> FC A&tmlC A&eadCAtitleCAdd -log DntryA4titleCA4&eadC A$odyC AsEform actionB>save> met&odB>post> C AsEte*tfield la$elB>3itle> nameB>title> 4C AsEte*tarea la$elB>Dntry> nameB>entry> ro%sB>6> colsB>21> 4C AsEsu$mit valueB>Add>4C A4sEformC A4$odyC A4&tmlC
source: Apache Struts 2 documentation
32
Action Interface
3&e default entry met&od to t&e &andler class is defined $y t&e Action interface(
pu#lic inter$ace Action % pu#lic String execute() throws &xception' (
Implementing t&e Action interface is optional(
If Action is not implemented. t&e frame%or# %ill use reflection to loo# for an execute met&od(
source: Apache Struts 2 documentation
33
Action (etho"s
Sometimes. developers li#e to create more t&an one entry point to an Action(
)or e*ample. in t&e case of of a data-access Action. a developer mig&t %ant separate entry-points for create. retrieve. update. and delete( A different entry point can $e specified $y t&e met&od attri$ute( Aaction nameB>delete> classB>e*ample(!rudAction> met&odB>delete>C
If t&ere is no e*ecute met&od. and no ot&er met&od. specified in t&e configuration. t&e frame%or# %ill t&ro% an e*ception(
source: Apache Struts 2 documentation
34
6il"car" (etho"
/any times. a set of action mappings %ill s&are a common pattern Rat&er t&an code a separate mapping for eac& action class. you can %rite it once as a %ildcard mapping(
source: Apache Struts 2 documentation
35
),a ple2 6il"car" (etho"
D*ample 1
Aaction nameB>H!rud> classB>e*ample(!rud> met&odB>I1J>C
+ere. a reference to >edit)rud> %ill call t&e edit met&od on an instance of t&e !rud Action class( 7i#e%ise. a reference to >delete)rud> %ill call t&e delete met&od instead(
D*ample 2
Aaction nameB>!rudKH> classB>e*ample(!rud> met&odB>I1J>C
source: Apache Struts 2 documentation
36
Action #efault
If you %ould prefer t&at an omni$us action &andle any unmatc&ed requests. you can specify a default action( If no ot&er action matc&es. t&e default action is used instead
Apac#age nameB>+ello> e*tendsB>action-default>C Adefault-action-ref nameB>8nder!onstruction>C Aaction nameB>8nder!onstruction>C AresultC48nder!onstruction(,spA4resultC A4actionC
source: Apache Struts 2 documentation
37
6il"car" #efault
8sing %ildcards is anot&er approac& to default actions( A %ildcard action at t&e end of t&e configuration can $e used to catc& unmatc&ed references(
Aaction nameB>H> C AresultC4I1J(,spA4resultC A4actionC
source: Apache Struts 2 documentation
38
Result
source: Apache Struts 2 documentation
39
Result
?&en an Action class met&od completes. it returns a String(
3&e value of t&e String is used to select a result element( An action mapping %ill often &ave a set of results representing different possi$le outcomes(
3&ere are predefined result names (to#ens) Applications can define ot&er result names (to#ens) to matc& specific cases(
source: Apache Struts 2 documentation
40
Pre-"efine" result na es 3to+ens4
String S8!!DSS B >success>L String <=<D B >none>L String DRR=R B >error>L String I<"83 B >input>L String 7=MI< B >login>L
source: Apache Struts 2 documentation
41
Result )le ent
"rovides a logical name (%it& name attri$ute)
An Action can pass $ac# a to#en li#e >success> or >error> %it&out #no%ing any ot&er implementation details( If t&e name attri$ute is not specified. t&e frame%or# %ill give it t&e name >success>( /ost results simply for%ard to a server page or template. $ut ot&er Result 3ypes can $e used to do more interesting t&ings( If a t*pe attri$ute is not specified. t&e frame%or# %ill use t&e dispatcher
source: Apache Struts 2 documentation
"rovides a Result 3ype (%it& t*pe attri$ute)
42
Result ele ent
Result element %it&out defaults
7result na e89success9 type89"ispatcher9: 7para 7;result: na e89location9:;Than+<ou1/sp7;para :
Result element using some defaults (as as a$ove)
7result: 7para 7;result: na e89location9:;Than+<ou1/sp7;para :
Result element using default for t&e AparamC as %ell
7result:;Than+<ou1/sp7;result:
source: Apache Struts 2 documentation
43
),a ple2 (ultiple Results
Aaction nameB>+ello>C AresultC4&ello4Result(,spA4resultC A;-- nameBNsuccessN --C Aresult nameB>error>C4&ello4Drror(,spA4resultC Aresult nameB>input>C4&ello4Input(,spA4resultC A4actionC
source: Apache Struts 2 documentation
44
Result Types
source: Apache Struts 2 documentation
45
Pre"efine" Result Types
5ispatc&er Result Redirect Action Result !&ain Result Redirect Result )ree/ar#er Result elocity Result "lain3e*t Result 3iles Result +ttp+eader Result Stream Result
source: Apache Struts 2 documentation
46
Setting a "efault Result Type
If a t*pe attri$ute is not specified. t&e frame%or# %ill use t&e dispatcher(
3&e default Result 3ype. dispatc&er. for%ards to anot&er %e$ resource(
A default Result 3ype can $e set as part of t&e configuration for eac& pac#age(
7result-types: 7result-type na e89"ispatcher9 class89org1apache1struts21"ispatcher1Ser!let#ispatcherResult9 "efault89true9;: 7;result-types:
source: Apache Struts 2 documentation
47
=lo*al Results
/ost often. results are nested %it& t&e action element( -ut some results apply to multiple actions(
D*ampleE In a secure application. a client mig&t try to access a page %it&out $eing aut&ori'ed. and many actions may need access to a >logon> result(
If actions need to s&are results. a set of glo$al results can $e defined for eac& pac#age( 3&e frame%or# %ill first loo# for a local result nested in t&e action( If a local matc& is not found. t&en t&e glo$al results are c&ec#ed(
source: Apache Struts 2 documentation
48
),a ple2 =lo*al Results
Aglo$al-resultsC Aresult nameB>error>C4Drror(,spA4resultC Aresult nameB>invalid(to#en>C4Drror(,spA4resultC Aresult nameB>login> typeB>redirect-action>C7ogon;inputA4resultC A4glo$al-resultsC
source: Apache Struts 2 documentation
49
#yna ic Results
A result may not $e #no%n until e*ecution time( Result values may $e retrieved from its corresponding Action implementation $y using D7 e*pressions t&at access t&e Action@s properties
source: Apache Struts 2 documentation
50
),a ple2 #yna ic Results
Mive t&e follo%ing Action fragment
pri!ate String ne,tAction> pu*lic String get'e,tAction34 ? return ne,tAction> @
9ou mig&t define a result li#e follo%ing
7action na e89frag ent9 class89Frag entAction9: 7result na e89ne,t9 type89re"irect-action9:A?ne,tAction@7;result: 7;action:
source: Apache Struts 2 documentation
51
),a ple2 #yna ic Results
In t&e code $elo%. if it returns success. t&en t&e $ro%ser %ill $e for%arded to
;7app-prefi,:; y'a espace;otherAction1actionBi"87!alue of i": 7action na e89 yAction9 class89co 1pro/ect1(yAction9: 7result na e89success9 type89re"irect-action9:otherActionBi"8A?i"@7;result: 7result na e89*ac+9 type89re"irect9:A?re"irect5RL@7;result: 7;action:
52
source: Apache Struts 2 documentation
Action Chaining
3&e frame%or# provides t&e a$ility to c&ain multiple actions into a defined sequence or %or#flo%( 3&is feature %or#s $y applying a !&ain Result to a given Action
source: Apache Struts 2 documentation
53
Chain Result
3&e !&ain Result is a result type t&at invo#es an Action %it& its o%n Interceptor Stac# and Result( 3&is Interceptor allo%s an Action to for%ard requests to a target Action. %&ile propagating t&e state of t&e source Action(
source: Apache Struts 2 documentation
54
Apac#age nameB>pu$lic> e*tendsB>struts-default>C A;-- !&ain creatAccount to login. using t&e default parameter --C Aaction nameB>createAccount> classB>(((>C Aresult typeB>c&ain>CloginA4resultC A4actionC Aaction nameB>login> classB>(((>C A;-- !&ain to anot&er namespace --C Aresult typeB>c&ain>C Aparam nameB>action<ame>Cdas&$oardA4paramC Aparam nameB>namespace>C4secureA4paramC A4resultC A4actionC A4pac#ageC Apac#age nameB>secure> e*tendsB>struts-default> namespaceB>4secure>C Aaction nameB>das&$oard> classB>(((>C AresultCdas&$oard(,spA4resultC A4actionC A4pac#ageC 55 source: Apache Struts 2 documentation
Interceptor
source: Apache Struts 2 documentation
56
6hy InterceptorsB
/any Actions s&are common concerns(
D*ampleE Some Actions need input validated( =t&er Actions may need a file upload to $e preprocessed( Anot&er Action mig&t need protection from a dou$le su$mit( /any Actions need dropdo%n lists and ot&er controls pre-populated $efore t&e page displays(
3&e frame%or# ma#es it easy to s&are solutions to t&ese concerns using an >Interceptor> strategy(
source: Apache Struts 2 documentation
57
Interceptor in Action Life-cycle
source: Apache Struts 2 documentation
58
Interceptors
Interceptors can e*ecute code $efore and after an Action is invo#ed( /ost of t&e frame%or#@s core functionality is implemented as Interceptors(
)eatures li#e dou$le-su$mit guards. type conversion. o$,ect population. validation. file upload. page preparation. and more. are all implemented %it& t&e &elp of Interceptors(
Dac& and every Interceptor is plugga$le
source: Apache Struts 2 documentation
59
Interceptors C Actions
In some cases. an Interceptor mig&t #eep an Action from firing. $ecause of a dou$le-su$mit or $ecause validation failed( Interceptors can also c&ange t&e state of an Action $efore it e*ecutes(
source: Apache Struts 2 documentation
60
Configuration of Interceptors
Interceptors can $e configured on a peraction $asis( 9our o%n custom Interceptors can $e mi*edand-matc&ed %it& t&e Interceptors $undled %it& t&e frame%or#( 3&e Interceptors are defined in a stac# t&at specifies t&e e*ecution order(
In some cases. t&e order of t&e Interceptors on t&e stac# can $e very important(
source: Apache Struts 2 documentation
61
Configurating Interceptors
Apac#age nameB>default> e*tendsB>struts-default>C AinterceptorsC Ainterceptor nameB>timer> classB>((>4C Ainterceptor nameB>logger> classB>((>4C A4interceptorsC Aaction nameB>login> classB>tutorial(7ogin>C Ainterceptor-ref nameB>timer>4C Ainterceptor-ref nameB>logger>4C Aresult nameB>input>Clogin(,spA4resultC Aresult nameB>success> typeB>redirect-action>C4secure4&omeA4resultC A4actionC A4pac#ageC
source: Apache Struts 2 documentation
62
Stac+ing Interceptors
?it& most %e$ applications. %e find ourselves %anting to apply t&e same set of Interceptors over and over again( Rat&er t&an reiterate t&e same list of Interceptors. %e can $undle t&ese Interceptors toget&er using an Interceptor Stac#(
source: Apache Struts 2 documentation
63
Stac+ing Interceptors
Apac#age nameB>default> e*tendsB>struts-default>C AinterceptorsC Ainterceptor nameB>timer> classB>((>4C Ainterceptor nameB>logger> classB>((>4C Ainterceptor-stac# nameB>myStac#>C Ainterceptor-ref nameB>timer>4C Ainterceptor-ref nameB>logger>4C A4interceptor-stac#C A4interceptorsC Aaction nameB>login> classB>tutuorial(7ogin>C Ainterceptor-ref nameB>myStac#>4C Aresult nameB>input>Clogin(,spA4resultC Aresult nameB>success> typeB>redirect-action>C4secure4&omeA4resultC A4actionC A4pac#ageC source: Apache Struts 2 documentation
64
Interceptor Interface
Interceptors must implement t&e [Link]*mphon*.xwor+ .interceptor.,nterceptor interface 3&e A#stract,nterceptor class provides an empty implementation of init and destroy. and can $e used if t&ese met&ods are not going to $e implemented(
pu$lic interface Interceptor e*tends Seriali'a$le I void destroy()L void init()L String intercept(ActionInvocation invocation) t&ro%s D*ceptionL J source: Apache Struts 2 documentation
65
),a ple2 Interceptor
import com(opensymp&ony(*%or#2(ActionInvocationL import com(opensymp&ony(*%or#2(interceptor(A$stractInterceptorL pu$lic class SimpleInterceptor e*tends A$stractInterceptor I pu$lic String intercept(ActionInvocation invocation) t&ro%s D*ception I /yAction action B (/yAction)invocation(getAction()L action(set5ate(ne% 5ate())L return invocation(invo#e()L J
source: Apache Struts 2 documentation
66
Fra eDor+ Interceptors
Struts 2 frame%or# provides an e*tensive set of ready-to-use interceptors
"arameter interceptorE Sets t&e request parameters onto t&e Action( Scope interceptorE Simple mec&anism for storing Action state in t&e session or application scope( alidation interceptorE "erforms validation using t&e validators defined in action-validation(*ml /any more
67
!onfigured in struts!de$[Link]
source: Apache Struts 2 documentation
EieD Support
source: Apache Struts 2 documentation
68
EieD
Reusa$le user interface tags t&at allo% for easy component-oriented development using t&emes and templates( -undled tags ranges from simple te*t fields to advanced tags li#e date pic#ers and tree vie%s( 0S37-compati$le e*pression language (=M<7) t&at e*poses properties on multiple o$,ects as if t&ey %ere a single 0ava-ean( "lugga$le Result 3ypes t&at support multiple vie% tec&nologies. including 0S". )ree/ar#er. elocity. "5). and 0asperReports(
source: Apache Struts 2 documentation
69
EieD
=ptional A0A: t&eme t&at simplifes creating interactive %e$ applications( =ptional Interceptor plugins t&at can e*ecute long-running queries in t&e $ac#ground. prevent multiple form su$missions. or &andle custom security sc&emes(
source: Apache Struts 2 documentation
70
EieD Technologies
0S". elocity. )ree/a#er. "5). :S73
source: Apache Struts 2 documentation
71
Eali"ation
source: Apache Struts 2 documentation
72
Built-in Eali"ators
7!ali"ators: 7!ali"ator na 7!ali"ator na 7!ali"ator na 7!ali"ator na 7!ali"ator na 7!ali"ator na 7!ali"ator na 7!ali"ator na 7!ali"ator na 7!ali"ator na 7!ali"ator na 7!ali"ator na 7!ali"ator na 7;!ali"ators: e89require"9 class89119;: e89require"string9 class89119;: e89int9 class89119;: e89"ou*le9 class89119;: e89"ate9 class89119;: e89e,pression9 class89119;: e89fiel"e,pression9 class89119;: e89e ail9 class89119;: e89url9 class89119;: e89!isitor9 class89119;: e89con!ersion9 class8911;: e89stringlength9 class89119;: e89rege,9 class89119;:
source: Apache Struts 2 documentation
73
#efining Eali"ation Rules
"er Action classE
in a file named -[Link]/![Link] in a file named -[Link]!alias/![Link]
"er Action aliasE
In&eritance &ierarc&y and interfaces implemented $y Action class
Searc&es up t&e in&eritance tree of t&e action to find default validations for parent classes of t&e Action and interfaces implemented
74
source: Apache Struts 2 documentation
),a ple2 Si pleAction-!ali"ation1, l
AvalidatorsC Afield nameB>$ar>C Afield-validator typeB>required>C AmessageC9ou must enter a value for $ar(A4messageC A4field-validatorC Afield-validator typeB>int>C Aparam nameB>min>COA4paramC Aparam nameB>ma*>C1PA4paramC AmessageC$ar must $e $et%een QIminJ and QIma*J. current value is QI$arJ(A4messageC A4field-validatorC A4fieldC Afield nameB>$ar2>C Afield-validator typeB>rege*>C Aparam nameB>rege*>[Link]-STA4paramC AmessageC3&e value of $ar2 must $e in t&e format >*. y>. %&ere * and y are $et%een P and SA4messageC A4field-validatorC source: Apache Struts 2 documentation A4fieldC
75
Afield nameB>date>C Afield-validator typeB>date>C Aparam nameB>min>C1242242PP2A4paramC Aparam nameB>ma*>C1242142PP2A4paramC AmessageC3&e date must $e $et%een 12-22-2PP2 and 12-212PP2(A4messageC A4field-validatorC A4fieldC Afield nameB>foo>C Afield-validator typeB>int>C Aparam nameB>min>CPA4paramC Aparam nameB>ma*>C1PPA4paramC Amessage #eyB>foo(range>C!ould not find foo(range;A4messageC A4field-validatorC A4fieldC Avalidator typeB>e*pression>C Aparam nameB>e*pression>Cfoo lt $ar A4paramC AmessageC)oo must $e greater t&an -ar( )oo B QIfooJ. -ar B QI$arJ(A4messageC A4validatorC 76 source: Apache Struts 2 documentation
Client Eali"ation
0avaScript client validation code is generated $y t&e frame%or# !ode you %rite
AsEform actionB>7ogin> validateB>true>C AsEte*tfield #eyB>username>4C AsEpass%ord #eyB>pass%ord> 4C AsEsu$mit4C A4sEformC
source: Apache Struts 2 documentation
77
Client Eali"ation
0avaScript code t&at gets generated $y frame%or#
Aform namespaceB>4e*ample> idB>7ogin> nameB>7ogin> onsu$mitB>return validate)ormK7ogin()L> actionB>4struts2-loginclientsidevalidation4e*ample47ogin(action> met&odB>post>CAta$le classB>%%)orm3a$le>C (( Ascript typeB>te*t4,avascript>C function validate)ormK7ogin() I form B document(getDlement-yId(>7ogin>)L clearDrror/essages(form)L clearDrror7a$els(form)L
source: Apache Struts 2 documentation
78
),a ple2 type8Fe,pressionF
Afield nameB>pass%ord>C Afield-validator typeB>requiredstring>C Amessage #eyB>error(pass%ord(required>4C A4field-validatorC A4fieldC Afield nameB>pass%ord2>C Afield-validator typeB>requiredstring>C Amessage #eyB>error(pass%ord2(required>4C A4field-validatorC A4fieldC Avalidator typeB>e*pression>C Aparam nameB>e*pression>Cpass%ord eq pass%ord2A4paramC Amessage #eyB>error(pass%ord(matc&>4C A4validatorC
source: Apache Struts 2 documentation
79
Configuration Files
source: Apache Struts 2 documentation
80
struts-"efault1, l
5efines all of t&e default $undled results and interceptors and many interceptor stac#s %&ic& you can use eit&er as-is or as a $asis for your o%n application-specific interceptor stac#s( Automatically included into [Link] file Included in t&e struts2(,ar file( In order to provide your o%n version. you can c&ange t&e [Link]$iguration.$iles setting in [Link] file(
source: Apache Struts 2 documentation
81
struts-"efault1, l
Apac#age nameB>struts-default>C Aresult-typesC Aresult-type nameB>c&ain> classB>(((>4C Aresult-type nameB>dispatc&er> classB>(((> defaultB>true>4C ((( A4result-typesC AinterceptorsC Ainterceptor nameB>alias> classB>(((>4C Ainterceptor nameB>auto%iring> classB>(((r>4C ((( A;-- -asic stac# --C Ainterceptor-stac# nameB>$asicStac#>C Ainterceptor-ref nameB>(((>4C Ainterceptor-ref nameB>servlet-config>4C Ainterceptor-ref nameB>prepare>4C Ainterceptor-ref nameB>c&ec#$o*>4C Ainterceptor-ref nameB>params>4C Ainterceptor-ref nameB>conversionDrror>4C
source: Apache Struts 2 documentation
82
struts1, l
3&e core configuration file for t&e frame%or# S&ould reside on t&e classpat& of t&e %e$app (generally 4?D--I<)4classes)(
source: Apache Struts 2 documentation
83
Pac+ages
source: Apache Struts 2 documentation
84
Pac+age
"ac#ages are a %ay to group actions. results. result types. interceptors. and interceptorstac#s into a logical configuration unit( !onceptually. pac#ages are similiar to classes in t&at t&ey can $e e*tended and &ave individual parts t&at can $e overridden $y >su$> pac#ages(
source: Apache Struts 2 documentation
85
),a ple2 7pac+age 11:
Apac#age nameB>employee> e*tendsB>struts-default> namespaceB>4employee>C Adefault-interceptor-ref nameB>crudStac#>4C Aaction nameB>list> met&odB>list> classB>org(apac&e(struts2(s&o%case(action(DmployeeAction> C AresultC4empmanager4listDmployees(,spA4resultC Ainterceptor-ref nameB>$asicStac#>4C A4actionC
86
source: Apache Struts 2 documentation
'a espace attri*ute
3&e namespace attri$ute su$divides action configurations into logical modules. eac& %it& its o%n identifying prefi*(
<amespaces avoid conflicts $et%een action names( Dac& namespace can &ave it@s o%n >menu> or >&elp> action. eac& %it& its o%n implementation(
source: Apache Struts 2 documentation
87
),a ple2 'a espace attri*ute
7pac+age na e89"efault9: 7action na e89foo9 class89 ypac+age1si pleAction: 7result na e89success9 type89"ispatcher9:greeting1/sp7;result: 7;action: 7action na e89*ar9 class89 ypac+age1si pleAction9: 7result na e89success9 type89"ispatcher9:*ar01/sp7;result: 7;action: 7;pac+age: 7pac+age na e89 ypac+age09 na espace89;9: 7action na e89 oo9 class89 ypac+age1si pleAction9: 7result na e89success9 type89"ispatcher9: oo1/sp7;result: 7;action: 7;pac+age: 7pac+age na e89 ypac+age29 na espace89;*arspace9: 7action na e89*ar9 class89 ypac+age1si pleAction9: 7result na e89success9 type89"ispatcher9:*ar21/sp7;result: 7;action: 7;pac+age:
source: Apache Struts 2 documentation
88
Plug-in
source: Apache Struts 2 documentation
89
Struts 2 Plug-in Architecture
"lug-in@s are used to e*tend t&e frame%or# ,ust $y adding a 0AR to t&e application@s classpat&( Since plugins are contained in a 0AR. t&ey are easy to s&are %it& ot&ers(
source: Apache Struts 2 documentation
90
Bun"le" Plug-in&s
0S) plug-in RDS3 plug-in Spring plug-in 3iles plug-in SiteMrap& plug-in Sitemes& plug-in 0asperReports plug-in 0)ree!&art plug-in !onfig -ro%ser plug-in Struts 1 plug-in
source: Apache Struts 2 documentation
91
Struts 2 Tags
source: Apache Struts 2 documentation
92
Struts 2 Tags
3&e Struts 3ags &elp you create ric& %e$ applications %it& a minimum of coding(
source: Apache Struts 2 documentation
93
For e,a ple page li+e folloDing111
source: Apache Struts 2 documentation
94
6ithout Struts Tags 3a partial for 4
7G 5ser user 8 ActionConte,t1getConte,t34 G: 7for action89ProfileHup"ate1action9 etho"89post9: 7ta*le: 7tr: 7t": align89right97la*el:First na e27;la*el:7;t": 7t":7input type89te,t9 na e89user1firstna e9 !alue897G8user1getFirstna e34 G: ;:7;t": 7;tr: 7tr: 7t": 7input type89ra"io9 na e89user1gen"er9 !alue89I9 i"89user1gen"erI9 7G if 3user1get=en"er3488I4 ? G: chec+e"89chec+e"9 G: @ G: ;: 7la*el for89user1gen"erI9:Fe ale7;la*el: 111 source: Apache Struts 2 documentation
95
After Struts Tags 3a co plete for 4
7s2actionerror;: 7s2for action89ProfileHup"ate9 !ali"ate89true9: 7s2te,tfiel" la*el895serna e9 na e89userna e9;: 7s2passDor" la*el89PassDor"9 na e89passDor"9;: 7s2passDor" la*el893Repeat4 PassDor"9 na e89passDor"29;: 7s2te,tfiel" la*el89Full 'a e9 na e89full'a e9;: 7s2te,tfiel" la*el89Fro A""ress9 na e89fro A""ress9;: 7s2te,tfiel" la*el89Reply To A""ress9 na e89replyToA""ress9;: 7s2su* it !alue89Sa!e9 na e89Sa!e9;: 7s2su* it action89RegisterHcancel9 !alue89Cancel9 na e89Cancel9 onclic+89for 1onsu* it8null9;: 7;s2for :
source: Apache Struts 2 documentation
96
Struts 2 Basics
source: Apache Struts 2 documentation
97