You are on page 1of 5

1. What is the difference between Struts 1 vs Struts 2 ?

http://struts.apache.org/2.1.6/docs/comparing-struts-1-and-2.html
2. Which design pattern the Interceptors in Struts2 is based on ?
Interceptors in Struts2 are based on Intercepting Filters.
3. What are Pull-MVC and push-MVC based architecture ? Whicharchitecture does Struts2
follow ?
Pull-MV and Push-MV are better understood !ith ho! the "ie! la#er is getting data i.e. Model to
render. In case o$ Push-MV the data% Model& is constructed and gi"en to the "ie! la#er b# the
ontrollers b# putting it in the scoped "ariables li'e re(uest or session. )#pical e*ample is Spring MV
and Struts1. Pull-MV on the other hand puts the model data t#picall# constructed in ontrollers are
'ept in a common place i.e. in actions+ !hich then gets rendered b# "ie! la#er. Struts2 is a Pull-MV
based architecture+ in !hich all data is stored in Value Stac' and retrie"ed b# "ie! la#er $or rendering.
4. re Interceptors in Struts2 thread safe ?
,o+-nli'e Struts2 action+ Interceptors are shared bet!een re(uests+ so thread issues !ill come i$ not
ta'en care o$.
5. re Interceptors and !ilters different ? " If #es then how ?
.part $rom the $act that both Interceptors and $ilters are based on intercepting $ilter+there are $e!
di$$erences !hen it comes to Struts2.
!ilters: %1&/ased on Ser"let Speci$ication %2&0*ecutes on the pattern matches on the re(uest.%1& ,ot
con$igurable method calls
Interceptors$ %1&/ased on Struts2. %2&0*ecutes $or all the re(uest (uali$ies $or a $ront controller% .
Ser"let $ilter &..nd can be con$igured to e*ecute additional interceptor $or a particular action e*ecution.
%1&Methods in the Interceptors can be con$igured !hether to e*ecute or not b# means o$
e*cludemethods or includeMethods. % see tutorial on this ontrolling Interceptor /eha"ior&
6. In Struts1" the front-controller was a Servlet but in Struts2" it is a filter% What is the possible
reason to change it to a filter ?
)here are t!o possibilities !h# $ilter is designated as $ront controller in Strtus2
%1&Ser"let made as $ront controller needs de"eloper to pro"ide a right "alue in 2load-on-startup3 !hich
lets the $rame!or' to initiali4e man# important aspects% "i4. struts con$iguration $ile&as the container
starts.In absense o$ !hich the $rame!or' gets initiali4ed onl# as the $irst re(uest hits.Struts2 ma'es
our li$e eas# b# pro"iding $ront-controller as a $ilter+and b# nature the $ilters in !eb.*ml gets initiali4ed
automaticall# as the container starts.)here is no need o$ such load-on-startup tag.
%2&.)he second but important one is + the introduction o$ Interceptors in Struts2 $rame!or'.It not 5ust
reduce our coding e$$ort+but helps us !rite an# code !hich !e !ould ha"e used $ilters $or coding and
necessar# change in the !eb.*ml as opposed to Struts1.So no! an# code that $its better in Filter can
no! mo"ed to interceptors% !hich is more controllable than $ilters&+ all con$iguration can be controlled
in struts.*ml $ile+ no need to touch the !eb.*ml $ile.
%1&.)he $ront controller being a $ilter also helps to!ards the ne! $eature o$ Struts ie -I )hemes. .ll the
static resources in the )hemes no! ser"ed through the $ilter
7. Which class is the front-controller in Struts2 ?
)he class 6org.apache.struts2.dispatcher.Filter7ispatcher 6 is the $ront controller in Struts2. In recent
time Struts 2.1.3 this class is deprecated and ne! classes are introduced to do the 5ob.
8e$er: http://struts.apache.org/2.1.9/struts2-
core/apidocs/org/apache/struts2/dispatcher/Filter7ispatcher.html
8. What is the role of ction& Model ?
.ctions in Struts are P:;: + is also considered as a Model. )he role o$ .ction are to e*ecute business
logic or delegate call to business logic b# the means o$ action methods !hich is mapped to re(uest
and contains business data to be used b# the "ie! la#er b# means o$ setters and getters inside the
.ction class and $inall# helps the $rame!or' decide !hich result to render
9. 'ow does Interceptors help achieve Struts2 a better fra(ewor) than Struts1 ?
3 Most o$ the tri"ial !or' are made easier to achie"e $or e*ample automatic $orm population.
3 Intelligent con$iguration and de$aults $or e*ample #ou can ha"e struts.*ml or annotation based
con$iguration and out o$ bo* interceptors can pro"ide $acilities that a common !eb application needs
3,o! Struts2 can be used an#!here in des'top applications also+ !ith minimal or no change o$
e*isting !eb application+since actions are no! P:;:.P:;: actions are e"en easier to unit
test.)han's to interceptors
30asier -I and "alidation in $orm o$ themes and !ell 'no!n 7:;: $rame!or'.
3<ighl# plugable+Integrate other technologies li'e Spring+<ibernate etc at ease.
3 8ead# $or ne*t generation 80S)F-= ser"ices
10. What is the relation between ValueStac) and *+,- ?
. ValueStac' is a place !here all the data related to action and the action itsel$ is stored. :>,= is a
mean through !hich the data in the ValueStac' is manipulated.
11. Can annotation-based and .M- based configuration of actions coe/ists ?
?es
12. What is struts%devMode and wh# it is used ?
struts.de"Mode is a 'e# used in struts.properties $ile %an also be con$igured in struts.*ml $ile as
2constant name@6struts.de"Mode6 "alue@6true6 /3& + to represent !hether the $rame!or' is running
in de"elopment mode or production mode b# setting true or $alse. I$ set to de"elopment mode+ it
gi"es the $ollo!ing bene$its : -
0 8esource bundle reload on e"er# re(uestA i.e. all locali4ation properties $ile can be modi$ied and
the change !ill be re$lected !ithout restarting the ser"er.
0 struts.*ml or an# con$iguration $iles can be modi$ied !ithout restarting or redeplo#ing the
application
0 )he error occurs in the application !ill be reported+ as oppose to production mode.
.lso remember that struts.de"Mode should be mar'ed as $alse in production en"ironment to
reduce impact o$ per$ormance. /# de$ault it is 6$alse6.
13. 'ow do #ou configure the annotation based action (apping ?
14. What is the difference between e(pt# default na(espace and root na(e space ?
I$ the namespace attribute is not de$ined in the pac'age tag or assigned 66 "alue then it is called
empt# de$ault namespace.Bhile i$ 6/6 is assigned as "alue to the namespace attribute then it is
called as root namespace.
)he root namespace is treated as all other e*plicit namespaces and must be matched. ItCs
important to distinguish bet!een the empt# de$ault namespace+ !hich can catch all re(uest
patterns as long as the action name matches+ and the root namespace+ !hich is an actual
namespace that must be matched.
15. Which interceptor is responsible for setting action1s 2ava3ean properties ?
com.opens#mphon#.*!or'2.interceptor.ParametersInterceptor is the interceptor class !ho sets the
actionDs ;a"a/ean properties $rom re(uest.
16. What is the difference between ction and ctionSupport ?
.ction is inter$ace de$ines some string li'e S-0SS+088:8 etc and an e*ecute%& method. For
con"enience 7e"eloper implement this inter$ace to ha"e access to String $ield in action methods.
.ctionSupport on other hand implements .ction and some other inter$aces and pro"ides some
$eature li'e data "alidation and locali4ed error messaging !hen e*tended in the action classes b#
de"elopers.
17. 'ow do #ou get the 'ttpServlet4e5uest ob6ect in an interceptor ?
<ere is the intercept method
E
1
2
3
4
5
public String intercept(ActionInvocation invoke) throws Exception {
ActionContext action=invoke.getInvocationContext();
ttpServlet!e"uest
re"=(ttpServlet!e"uest)action.get(StrutsStatics.##$%!E&'ES#);
return null;
(
In the similar !a# #ou can get the response+ b# using StrutsStatics.<))PF80SP:,S0 in get%& method
as abo"e.
19. What is e/ecute and wait interceptor ?
)he 0*ecute.ndBaitInterceptor is great interceptor pro"ided out o$ bo* in Struts2 $or running long-
li"ed actions in the bac'ground !hile sho!ing the user a nice progress meter or a progress bar.
For e*ample !hile uploading a large $ile to the ser"er !e can use this interceptor to displa# a nice
running progress bar instead o$ lea"ing the user in con$usion that the application is not
responding.)his also pre"ents the <))P re(uest $rom timing out !hen the action ta'es more than G
or 1H minutes.
19. 7oes the order in which interceptors e/ecute (atters ? If #es then wh# ?
Bell+ the ans!er is #es and no.Some Interceptors are designed to be independent so the order
doesnDt matter+but some interceptors are totall# dependent on the pre"ious interceptors
e*ecution.For e*ample the "alidation and !or'$lo! interceptors+the "alidation interceptors chec's i$
there is an# error in the $orm being submitted to the action+ then comes the !or'$lo! interceptor
!ho chec's i$ "alidation % occured in the last& has an# error+in presence o$ error it !ill not let the
rest o$ the interceptors % i$ an# & in the stac' to e*ecute.So this is "er# important that the "alidation
interceptors e*ecute $irst be$ore the !or'$lo!. :n the other hand lets sa# #ou !rote an interceptors
!ho is doing the authentication and #ou ha"e the user credential pro"ided % b# the time this
e*ecutes& it doesnDt matter !here this interceptors is placed% It is a di$$erent $act that #ou !ould li'e
to 'eep it in the top &.
20. Who loads the struts%/(l file ? Which Struts2 PI loads the struts%/(l file?
In Struts2 FilterSer"let is the responsible class $or loading struts.*ml $ile as !e deplo# the
application on the container.-nli'e Ser"let %as !ith Struts1& needs the load-on-startup tag to load
the $ront controller+a $ilter doesnDt need to ha"e load on startup tag to be loaded as the application
is deplo#ed. .s !ith ser"let speci$ication a $ilter is loaded/e*ecuted as the application starts up.
21. What is the difference between 4e5uestware and Servlet4e5uestware interface ?
8e(uest.!are and Ser"let8e(uest.!are both ma'es #our action to deals !ith the ser"let re(uest+
but in a di$$$rent !a#s+8e(uest.!are gi"es #ou the attributes in the ser"let re(uest as a map% 'e#
as attribute name and "alue is the ob5ect added&+/ut Ser"let8e(uest.!are gi"es #ou the
<ttpSer"let8e(uest ob5ect itsle$ gi"ing #ou more $le*ibilit#+ !ith a price that Ser"let8e(uest.!are
ma'es #our .ction class too much tied to the Ser"let en"ironment ma'ing it di$icult to unit test. So
!hene"er a need to access onl# the attributes use the 8e(uest.!are inter$ace.
22. What is the difference between 8- and *+,- ?
:>,= is much li'e 0= in ;SPs+a language to tra"erse or manupulate ob5ects li'e re(uest + session
or application in !eb conte*t.:>,= stands $or :b5ect graph na"igation language+!hich is used
internall# b# Struts2+ ho!e"er !e are not bound to use :>,= in our ;SPs+ !e can use 0=./ut
:>,= pro"ides much more $acilities than plain 0=.For e*ample !hile 0l interacts !ith the ob5ects
b# means o$ getters/setters+ :>,= supports !hate"er 0= does along !ith lambda e*perssion+
helps create $unctions on $l#. :>,= has more $le*ible !a#s to deal !ith collection o$ ob5ects.
23. What are the difference between ctionConte/t and ServletctionConte/t ?
.ctiononte*t represents the conte*t in !hich .ction is e*ecuted+ !hich itsel$ doesnDt hold an# !eb
parameters li'e session+ re(uest etc. Ser"let.ctiononte*t+ e*tends the .ctiononte*t and
pro"ides the !eb parameters to the .ction.
24. What is the life c#cle of Interceptor ?
25. Who e/ecutes&Which class in Struts2 e/ecutes the interceptors ?
26. In what order the interceptors e/ecute in an interceptor stac) ?
27. What is action scope and how is it different fro( re5uest scope ?
28. What happens if #ou don1t call the invo)e9: (ethod in an# interceptors1 intercept9: (ethod ?
29. 'ow can two interceptors in a stac) co((unicate or If #ou were to pass so(e value fro(
one interceptor to another" b# using this value the ne/t interceptor e/ecutes so(e specific
state(ents" how would #ou do it ?
30. What is d#na(ic (ethod invocation ?
31. What is the 7ispatchction 9Struts1: e5uivalent in Strtus2 ?
Struts1 pro"ided the $acilit# o$ ha"ing related action methods in a Single .ction class+depending on
the method parameter+ the mapped methods !ere e*ecuted.)o achie"e this !e ha"e to e*tend the
7ispatch.ction class in Struts1./ut this comes as de$ault in Struts2+ Be need to pro"ide the
(uali$ied methods in the action class% no need to inherit an# class&+ and pro"ide the mapping o$
action path/name to the method attribute in action tag%struts.*ml& and proper code in "ie! la#er.
32. What is the difference between 7ispatchction and d#na(ic (ethod invocation in Struts2 ?
33. 'ow (an# different wa#s can #ou retrive the re5uest para(eters fro( within interceptor ?
)!o !a#s.
In the $irst !a# retrie"e the <ttpSer"let8e(uest $rom the .ctiononte*t ob5ect and retrie"e the
parameters the usual !a#. ode
E
1
2
3
ActionContext context=(ActionContext)invocation.getInvocationContext();
ttpServlet!e"uest
re"uest=(ttpServlet!e"uest)context.get(StrutsStatics.##$%!E&'ES#);
String userna)e=(String)re"uest.get$ara)eter(*user*);
)he second !a# is prett# much the Struts2+ b# in"o'ing the getParameters method on conte*t ob5ect.
ode
E
1
2
3
ActionContext context=(ActionContext)invocation.getInvocationContext();
+ap re"uest$ara)eters=context.get$ara)eters();
String userna)es=((String,-)).get(*user*)),.-;
.s #ou can see the map doesnDt return the parameter as String unli'e the Ser"let speci$ication+ but an
arra# o$ String %can be con"inient $or multi"alued -I controls chec' bo*+single "alue -I controls data
can be retri"ed as in abo"e code &.
34. What is abstract pac)age in Struts2" and what is its use ?
.n abstract pac'age usuall# de$ines inheritable components such as intercetpor+di$$erent
interceptor stac's+result t#pes etc./ut doesnDt contain an# actions.)he !a# !e declare a pac'age
as abstract is through the pac'age elements attribute as abstract and setting the "alue to 6true6. /#
de$ault % i$ abstract attribute is not mentioned& it is $alse.Struts-de$ault.*ml is an e*ample o$ abstract
pac'age.
35. 7oes Struts2 (andates of i(ple(enting the ction interface in action classes to have the
default ction (ethod 9e/ecute:?
Struts2 doesnDt mind i$ the action classes doesnDt implement the .ction inter$ace+ to ha"e the
e*ecute method e*ecuted on de$ault action method selection.0"en though it appears that .ction
inter$aces has the e*ecute method declaration and one must implement it to ha"e the e*ecute
method o"erriden in the action classes+Struts2 ta'es it as an in$ormal contract !ith the de"eloper
b# letting the de"eloper to ha"e an e*ecute method con$orming to the signature o$ e*ecute
method.Bhene"er Struts2 $inds an# "oilation to the declaration o$ the method in unimplemented
action class it thro!s the e*eception.

You might also like