Appl ying a Model View Cont rol l er Pat t ern in J2EE Plat f orm Using St rut s Framework
Niko Ibrahim
137 addit ional t ime, we can end up wit h a richer, more compl et e
appl icat ion t hat is comparat ivel y more pol ished t han it coul d have been wit hout t he f ramework.
An appl icat ion f ramework is dif f icul t t o creat e. It is dif f icul t , not so much because t he code is dif f icul t t o writ e, or t hat t here is a l arge amount of
code. Indeed, many ent erprise appl icat ions have several t imes more code t han t he f rameworks t hat hel ped creat e t hem. The code of a f ramework is
of t en compl ex; however, proper design hel ps t o make it underst andabl e. This compl exit y, t hough, is not t he reason f or f rameworks being so dif f icul t
t o creat e. The dif f icul t y l ies in obt aining t he experience t hat is dist il l ed int o t hem. Frameworks evol ve f rom t he ef f ort t o reuse bot h design and
code, and f rom repeat ed ref act oring t hat resul t s in widel y appl icabl e services and component s. This is what is dif f icul t about t heir creat ion.
Because f rameworks t end t o be l arge and f airl y compl ex pieces of sof t ware, t hey are of t en not a cost -ef f ect ive proj ect f or a singl e
organizat ion. It is al so a good t hing when a part icul ar f ramework is used in many proj ect s - it grows, it becomes a de f act o st andard t o a degree, and
t he organizat ions using it get t he benef it s. Al l of t hese f act ors mean t hat f rameworks are good candidat es f or open-source proj ect s, and indeed
many of t he best f rameworks are open source.
4. 1. Overview of Struts Frameworks
The most widel y adopt ed MVC f ramework is t he open source Apache St rut s ht t p: j akart a. apache. org st rut s
. St rut s was original l y writ t en by Craig McCl anahan, t he main devel oper of t he Tomcat servl et engine, and was
rel eased in mid 2000, making it t he l ongest -est abl ished open source web appl icat ion f ramework. Part l y because of it s rel at ivel y l ong hist ory, St rut s
has achieved remarkabl e buy-in by devel opers, wit h many add-ons now avail abl e around it [ Goo04] .
St rut s provides key capabil it ies t o t he devel oper f or buil ding virt ual l y any Web appl icat ion - especial l y t hose t hat make use of JSP pages as t heir user
int erf ace t echnol ogy. St rut s can, however, int eract wit h many ot her present at ion t echnol ogies t hat incl ude XML XSLT [ Nas03] .
The primary el ement s of St rut s are [ Bro03] :
• A cont roller Act ionServlet t hat delegat es specif ic request handling
t o Act ion cl asses •
An ext ensive JSP cust om t ag library •
A library of ut ilit y classes t o support Web applicat ion devel opment
138 The basic St rut s archit ect ure is shown in Figure 6:
Figure 6: T he basic St rut s archit ect ure
In f igure 6, we can see t hat t he St rut s cont rol l er l ayer has a component cal l ed Act ions. Act ion is essent ial l y a Java cl ass t hat is responsibl e f or
examining inf ormat ion f rom t he request , perf orming some operat ion, opt ional l y popul at ing dat a t hat wil l l at er be used by t he view l ayer, and
t hen communicat ing t o t he Act ionServl et where cont rol shoul d be f orwarded next .
4. 2. Benefits and Issues of Struts Framework
St rut s addresses a gap in t he J2EE t echnol ogical st ack not covered by Servl et JSP or EJB. St rut s shoul d be one of t he t op choices when we are
considering t he adopt ion of an MVC f ramework due t o it s numerous benef it s. However, despit e of t he benef it s of St rut s, we shoul d al so
consider some issues of using it [ Rod03] . The f ol l owing are some benef it s of using St rut s:
•
Use of JSP tag mechanism The t ag f eat ure promot es reusabl e code and abst ract s Java code
f rom t he JSP f il e. This f eat ure al l ows nice int egrat ion int o JSP- based devel opment t ool s t hat al l ow aut horing wit h t ags.
•
Tag library St rut s provides devel opers wit h an ext ensive range of t ag l ibrary.
•
Open source We have al l t he advant ages of open source, such as being abl e t o
see t he code and having everyone el se using t he l ibrary reviewing t he code. Many eyes make f or great code review.
•
Manage the problem space Divide and conquer is a nice way of sol ving t he probl em and making
t he probl em manageabl e.
Controller
Client
Browser
Servlet
ActionServlet
Business Logic
Model
Application
View
JSP
Event
HTTP Request
Dispatch
Forward
Update
HTTP Response
Get
tag
struts-config.xml Access
Appl ying a Model View Cont rol l er Pat t ern in J2EE Plat f orm Using St rut s Framework
Niko Ibrahim
139 The f ol l owing are some issues of using St rut s t hat might be considered by
devel opers: •
Limited scope St rut s is a Web-based MVC sol ut ion t hat is meant be impl ement ed
wit h HTML, JSP f il es, and servl et s. Devel opers must be expert in J2EE pl at f orm if t hey int ended t o use St rut s in t heir devel opment
phases.
•
Complexity Separat ing t he probl em int o part s int roduces compl exit y. There is
no quest ion t hat some educat ion wil l have t o go on t o underst and St rut s. Wit h t he const ant changes occurring, t his can be f rust rat ing
at t imes.
4. 3. Example of Struts Application: Login