Jakarta Struts: An MVC Framework

utopianthreeSoftware and s/w Development

Jul 14, 2012 (5 years and 4 months ago)

814 views

© 2010 Marty Hall
Jakarta Struts:
An MVC Framework
Overview Installation and Setup
Overview
,
Installation
,
and

Setup
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
© 2010 Marty Hall
For live Struts training, please see training
courses at http://courses coreservlets com/
courses at http://courses
.
coreservlets
.
com/
.
Taught by the author of Core Servlets and JSP,
More Servlets and JSP
and this tutorial Available at
C d l d d t ht b M t H ll
More Servlets and JSP
,
and this tutorial
.
Available at
public venues, or customized versions can be held
on-site at your
organization.
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.

C
ourses
d
eve
l
ope
d
an
d

t
aug
ht

b
y
M
ar
t
y
H
a
ll
– Java 6, servlets/JSP (intermediate and advanced), Struts, JSF 1.x, JSF 2.0, Ajax, GWT 2.0 (with GXT), custom mix of topics
– Ajax courses can concentrate on 1 library (jQuery, Prototype/Scriptaculous, Ext-JS, Dojo, Google Closure) or survey several
• Courses developed and taught by coreservlets.com experts (edited by Marty)
– Spring, Hibernate/JPA, EJB3, SOAP-based and RESTful Web Services, Ruby/Rails
Contact hall@coreservlets.com for details
Agenda
• Understanding Struts
– Different views of Struts
– Advantages of Struts
Disadvantages of Struts

Disadvantages

of

Struts
• Setting Up Struts

Downloading and configuring Struts
Downloading

and

configuring

Struts
– Testing Struts
– Setting up Struts applications
– Accessing Struts documentation
– Adding Struts to an existing Web application
4
© 2010 Marty Hall
Overview
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
5
What is Apache Struts?
• An MVC Framework?
St t id ifi d f k f d l i l t

St
ru
t
s prov
id
es a un
ifi
e
d

f
ramewor
k

f
or
d
ep
l
oy
i
ng serv
l
e
t

and JSP applications that use the MVC architecture.
• A Collection of Utilities?
– Struts provides utility classes to handle many of the most
common tasks in Web application development

A Set of JSP CustomTag Libraries?
A

Set

of

JSP

Custom

Tag

Libraries?
– Struts provides custom tag libraries for outputting bean
properties, generating HTML forms, iterating over
various types of data structures and conditionally
various

types

of

data

structures
,
and

conditionally

outputting HTML.
• But which is the proper
way to view Struts?
h d d h i i f
– T
h
e answer
d
epen
d
s on w
h
at you are go
i
ng to use
i
t
f
or,
but the MVC framework is the most common way of
looking at Struts.
6
Advantages of Struts (vs. MVC
Using RequestDispatcher)
Using

RequestDispatcher)
• Centralized file-based configuration

Rather then hard
-
coding information into Java programs many
Rather

then

hard
-
coding

information

into

Java

programs
,
many

Struts values are represented in XML or property files. This loose
coupling means that many changes can be made without modifying
or recompiling Java code, and that wholesale changes can be made
by editing a single file This approach also lets Java and Web
by

editing

a

single

file
.
This

approach

also

lets

Java

and

Web

developers focus on their specific tasks (implementing business
logic, presenting certain values to clients, etc.) without needing to
know about the overall system layout.
F b

F
orm
b
eans
– In JSP, you can use property="*" with jsp:setProperty to
automatically populate a JavaBean component based on incoming
request parameters.Apache Struts extends this capability to Java
request

parameters.

Apache

Struts

extends

this

capability

to

Java

code and adds in several useful utilities, all of which serve to greatly
simplify the processing of request parameters.
• Bean tags
A h St t id t f t JSP t (
b it
i

A
pac
h
e
St
ru
t
s prov
id
es a se
t
o
f
cus
t
om
JSP

t
a
g
s
(
b
ean:wr
it
e,
i
n
particular) that let you easily output the properties of JavaBeans
components. Basically, these are concise and powerful variations of
the standard jsp:useBean and jsp:getProperty tags.
7
Advantages of Struts (vs.
Standard MVC) Continued
Standard

MVC)
,
Continued
• HTML tags
A h S id f JSP HTML

A
pac
h
e
S
truts prov
id
es a set o
f
custom
JSP
ta
g
s to create
HTML

forms that are associated with JavaBeans components. This
bean/form association serves two useful purposes:
It lets you get initial form
field values fromJava objects

It

lets

you

get

initial

form
-
field

values

from

Java

objects
.
• It lets you redisplay forms with some or all previously entered
values intact.

Formfield validation

Form

field

validation
– Apache Struts has builtin capabilities for checking that form values
are in the required format. If values are missing or in an improper
f t th f b t ti ll di l d ith
f
orma
t
,
th
e
f
orm can
b
e au
t
oma
ti
ca
lly
re
di
sp
l
a
y
e
d
w
ith
error
messages and with the previously entered values maintained.
• This validation can be performed on the server (in Java), or both
on the server and on the client (in JavaScript)
on

the

server

and

on

the

client

(in

JavaScript)
.
• Consistent approach
– Struts encourages consistent use of MVC throughout your app.
8
Disadvantages of Struts (vs.
MVC with RequestDispatcher)
MVC

with

RequestDispatcher)
• Bigger learning curve
T MVC ith th t d d R tDi t h

T
o use
MVC
w
ith

th
e s
t
an
d
ar
d

R
eques
tDi
spa
t
c
h
er, you
need to be comfortable with the standard JSP and servlet
APIs. To use MVC with Struts, you have to be
comfortable with the standard JSP and servlet APIs and a
comfortable

with

the

standard

JSP

and

servlet

APIs

and

a

large and elaborate framework that is almost equal in size
to the core system. This drawback is especially
si
g
nificant with smaller
p
ro
j
ects
,
near-term deadlines
,
and
g p j,
,
less experienced developers; you could spend as much
time learning Struts as building your actual system.

Worse documentation
Worse

documentation
– Compared to the standard servlet and JSP APIs, Struts
has fewer online resources, and many first-time users find
the online A
p
ache documentation confusin
g
and
p
oorl
y

p g p y
organized. There are also fewer books on Apache Struts
than on standard servlets and JSP.
9
Disadvantages of Struts (vs.
Standard MVC) Continued
Standard

MVC)
,
Continued
• Less transparent
– With Struts applications, there is a lot more going on
behind the scenes than with normal Java-based Web
applications.As a result,Struts applications are:
applications.

As

a

result,

Struts

applications

are:

• Harder to understand
• Harder to benchmark and optimize
Ri id h

Ri
g
id
approac
h
– The flip side of the benefit that Struts encourages a
consistent approach to MVC is that Struts makes it
consistent

approach

to

MVC

is

that

Struts

makes

it

difficult (but by no means impossible) to use other
approaches.
10
Advantages of JSF 1.x
(vs
Struts 1 x)
(vs
.
Struts

1
.
x)
• Custom components
– JSF makes it relatively easy to combine complex GUIs
into a single manageable component; Struts does not

Better support for Ajax
Better

support

for

Ajax
– Several third-party component libraries have extensive
Ajax support (Apache Tomahawk, JBoss
Ajax4jsf/RichFaces, Oracle ADF, IceFaces). Struts
doesn't have real component libraries (see above).

Support for other display technologies
Support

for

other

display

technologies
– JSF is not limited to HTML and HTTP; Struts is
• Access to beans by name
– JSF lets you assign names to beans, then you refer to
them by name in the forms. Struts has a complex process
with several levels of indirection.
11
Advantages of JSF
(vs Struts) Continued
(vs
.
Struts)
,
Continued
• Expression language
Th JSF i l i i d

Th
e
JSF
express
i
on
l
anguage
i
s more conc
i
se an
d

powerful than the Struts bean:write tag.
• This is less advantageous if using JSP 2.0 anyhow.
• Simpler controller and bean definitions
– JSF does not require your controller and bean classes to
extend any particular parent class (e.g.,Action) or use
extend

any

particular

parent

class

(e.g.,

Action)

or

use

any particular method (e.g., execute). Struts does.
• Simpler config file and overall structure
Th f
fi l fil i h i t th i th

Th
e
f
aces-con
fi
g.xm
l

fil
e
i
s muc
h
eas
i
er
t
o use
th
an
i
s
th
e
struts-config.xml file. In general, JSF is simpler.
• More powerful potential tool support
– The orientation around GUI controls and their handlers
opens possibility of simple to use, drag-and-drop IDEs
12
Disadvantages of JSF (vs. Struts)
• Established base and industry momentum

Struts got there first and has more deployed
applications

Struts

got

there

first

and

has

more

deployed

applications
• 1/2010 search at dice.com and monster.com
– “struts”: 1276 jobs (dice.com), 596 jobs (monster.com)
– “jsf”: 480 (dice), 615 (monster) but some listings use “JavaServer Faces”
13
Disadvantages of JSF (vs. Struts),
Continued
Continued
• Support for other display technologies
JSF i t li it d t HTML d HTTP St t i

JSF

i
s no
t

li
m
it
e
d

t
o
HTML
an
d

HTTP
;
St
ru
t
s
i
s
• Hey! Didn't I say this was an advantage of JSF?
• Confusion vs. file names
– The actual pages used in JSF end in .jsp (JSF 1.x) or
.xhtml (JSF 2.0). But the URLs used end in .faces or .jsf.
This causes many problems; in particular, in JSF:
• Cannot browse directories and click on links
• Hard to protect raw JSP pages from access
• Hard to refer to non-faces pages in faces-config.xml
• Sel
f
-submit approach
– With Struts, the form (blah.jsp) and the handler
(
b
lah.do
)
have different URLs
;
with JSF the
y
are the
(
);y
same.
14
Disadvantages of JSF (vs.
Struts) Continued
Struts)
,
Continued
• No builtin equivalent to Tiles in JSF 1.x
St t i l d f l l t f ilit JSF 1 d t

St
ru
t
s
i
nc
l
u
d
es power
f
u
l
page
l
ayou
t

f
ac
ilit
y;
JSF

1
.x
d
oes no
t
• Facelets is not officially part of JSF 1.x (but is
in JSF 2.0)
• Much weaker automatic validation

Struts comes with validators for email address, credit card
numbers, regular expressions, and more. JSF only comes with
validators for missing values, length of input, and numbers in
a given range
a

given

range
.
• But MyFaces and other third-party component libraries have
several powerful validators

Lack of client
-
side validation
Lack

of

client
side

validation
– Struts supports JavaScript-based form-field validation; JSF
does not

Worse installation

Worse

installation
– Struts has the struts-blank application to use for a starting
point; JSF has nothing similar
15
© 2010 Marty Hall
Getting Started
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
16
Downloading and Configuring
Struts
Struts
• Download the Struts zip file
– Start at http://jakarta.apache.org/site/binindex.cgi, or
follow the link from http://jakarta.apache.org/struts/.

Unzip into a directory of your choice

Unzip

into

a

directory

of

your

choice
– For example, unzip into C:\jakarta-struts-1.2.4.

Add components to new projects
Add

components

to

new

projects
– Required
• web.xml with special settings, several JAR files, several Struts-
specific XML files several TLD files
specific

XML

files
,
several

TLD

files
– Obtaining files
• All can be found in the struts-blank project found in the installation
folder As discussed later you can make a new Web app and copy
folder
.
As

discussed

later
,
you

can

make

a

new

Web

app

and

copy

the individual pieces or copy and rename an entire existing app
17
Accessing Struts
Documentation
Documentation
• Main API (JavaDoc)
– http://struts.apache.org/struts-action/apidocs/
• User’s Guide
h//h/
i/G id/

h
ttp:
//
struts.apac
h
e.org
/
struts-act
i
on
/
user
G
u
id
e
/
• Alternative for main API: read a local copy
This is fastest but the documentation can get a bit out of

This

is

fastest
,
but

the

documentation

can

get

a

bit

out

of

date. To read a local copy, install the
struts-documentation.war Web app. Copy
i ll di
/
b
/
d i
s
truts_
i
nsta
ll
_
dir
/
we
b
apps
/
struts-
d
ocumentat
i
on.war to
tomcat_install_dir/webapps, restart server, and use the
URL htt
p
://localhost/struts-documentation/.
p
18
Testing Struts in an IDE
• Install the struts-blank project
S htt//l t/C
M t i l/t t ht l

S
ee
htt
p:
//
courses.coreserv
l
e
t
s.com
/C
ourse-
M
a
t
er
i
a
l
s
/
s
t
ru
t
s.
ht
m
l
• Deploy the project (Eclipse)

De
p
lo
y

p
ro
j
ect
p y
p j
• Select “Server” tab at bottom, R-click on Tomcat, Choose “Add
and Remove”, Choose project, Click “Finish”

Start Tomcat
• Select “Server” tab at bottom, R-click on Tomcat, Choose “Start”
• Open browser and enter http://localhost/struts-blank/
• Will redirect to htt
p
://localhost/struts-blank/Welcome.do
p
• Deploy the project (JDeveloper )
– Right-click on WebContent/index.jsp and select “Run”
URL will pop up in browser automatically

URL

will

pop

up

in

browser

automatically
– Will redirect to http://localhost/struts-blank/Welcome.do
19
Testing Struts with Manual
Deployment
Deployment
• Install struts-blank.war.
– Install the Web application from
struts_install_dir/webapps/struts-blank.war on your
server. For exam
p
le, with A
p
ache Tomcat, co
py

p p py
s
truts_install_di
r
/webapps/struts-
b
lank.war to
tomcat_install_dir/webapps/.

Start or restart the server
Start

or

restart

the

server
.
– Most servers only recognize new Web apps when the
server is started.
• Access http://localhost/struts-blank/.
– Will redirect to http://localhost/struts-blank/Welcome.do
This URL assumes you are running the server on your

This

URL

assumes

you

are

running

the

server

on

your

desktop and are using port 80. In general, access
http://hostname:port/struts-blank/.
20
Testing Struts (Results)
21
Development Process with
Eclipse
Eclipse
• Option 1: copy existing Struts project
– Copy and rename struts-
b
lank or another Struts project
– Due to Eclipse bug, this requires you to manually edit a
component file
.
component

file
• Still, this is generally easier than pasting all of the
necessary Struts components into a new project
O ti 2 k j t f t h

O
p
ti
on
2
: ma
k
e pro
j
ec
t

f
rom scra
t
c
h
– Make new Dynamic Web project
Copy all the required pieces fromstruts
-
blank or another

Copy

all

the

required

pieces

from

struts
-
blank

or

another

Struts project
22
Developing in Eclipse by Copying

Blank

Project
Blank

Project
• Why copy?
C i
S
i E li
i b

C
reat
i
ng new
S
truts apps
i
n
E
c
li
pse
i
s cum
b
ersome
• There are many files to copy from struts-blank
• Issue
– Cutting/pasting Web app in Eclipse does not change the
context path and internal project deploy name

Context path can be changed by R
-
clicking project then
Context

path

can

be

changed

by

R
-
clicking

project
,
then

Properties Web Project Settings
– But deployed name is not accessible from within Eclipse!
S l ti

S
o
l
u
ti
on
– Go to file system, edit
.settings/org.eclipse.wst.common.component
– Change two instances of old project name to new one
– R-click on project and choose Refresh
23
Copying struts-blank: Example
• First, copy project
– R-click on struts-
b
lank, choose “Copy”
– R-click in Project Explorer window, choose “Paste”

E g name it my
struts
project

E
.
g
.,
name

it

my
-
struts
-
project
• Next, edit .component file

N
avi
g
ate to
E
cli
p
se-works
p
ace/
p
ro
j
ectName/.settin
g
s
g
p
p
p j
g
Open in normal text editor
When done editing R
-
click on project in Eclipse
24
When done editing
,
R
click on project in Eclipse
,
then choose “Refresh”
Copying struts-blank: Example
(Continued)
(Continued)
•.component file: before
<
?xml version="1.0" encoding="UTF-8"?
>
<project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="struts-blank">
<wb-resource deploy-path="/" source-path="/WebContent"/>
b
d l
t h"/W E B
I N F/l"
t h"/"/
<
wb
-resource
d
ep
l
oy-pa
th
=
"/WEB
-
INF/
c
l
asses
"
source-pa
th
=
"/
src
"/>
<property name="context-root" value="struts-blank"/>
<property name="java-output-path"/>
</wb-module>
</j t
d l >
</
p r o
j
ec
t
-mo
d
u
l
es
>
•.component file: after
<?xml version="1.0" encoding="UTF-8"?>
<project
modules id="
moduleCoreId
"project
version="1 5 0">
<project
-
modules

id="
moduleCoreId
"

project
-
version="1
.
5
.
0">
<wb-module deploy-name="my-struts-project">
<wb-resource deploy-path="/" source-path="/WebContent"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src"/>
<property name=
"
context
-
root
"
value=
"
my
-
struts
-
project
"
/>
<property

name= context
-
root

value=
my
-
struts
-
project
/>
<property name="java-output-path"/>
</wb-module>
</project-modules>
25
Copying Web Applications in
JDeveloper
JDeveloper
1.In Windows, find struts-blank folder

E g/
mywork
/NBME
Samples/struts
blank

E
.
g
., …
/
mywork
/NBME
-
Samples/struts
-
blank
2.Copy and rename the folder
• E.g., to my-struts-app
3
Open new folder and rename struts
blank jpr
3
.
Open

new

folder

and

rename

struts
-
blank
.
jpr
• E.g., to my-struts-app.jpr
4.In JDeveloper, File Open, select my-struts-app.jpr
5
I j t R
li k P j t P ti

䨲䕅
5
.
I
渠湥眠灲n
j

t

R


c
k

P

j

t

P
牯灥r

敳e

䨲䕅

䅰灬楣慴楯渠 J2EE Web Context Root
26
Adding Struts to an Existing
Web Application
Web

Application
• Copy files into your Web app

Copy JAR files fromstruts
-
blank/WEB
-
INF/lib to
Copy

JAR

files

from

struts
blank/WEB
INF/lib

to

your_web_app/WEB-INF/lib.
– Copy TLD files from struts-blank/WEB-INF to
your_web_app/WEB-INF.
Copy struts
config xml fromstruts
blank/WEB
INF to

Copy

struts
-
config
.
xml

from

struts
-
blank/WEB
-
INF

to

your_web_app/WEB-INF.
– Copy the application properties file from struts-blank/WEB-
INF/classes/resources to your_web_app/WEB-
INF/classes/resources
INF/classes/resources
.
– If you plan on using the automatic validator, copy validation.xml
and validator-rules.xml from struts-blank/WEB-INF to
your_web_app/WEB-INF.
If l i Til
il l f

If

y
ou p
l
an on us
i
n
g

Til
es, cop
y
struts-t
il
es.xm
l

f
rom struts-
blank/WEB-INF to your_web_app/WEB-INF.
• Update web.xml

Copy declarations out of struts
-
blank/WEB
-
INF/web xml into
Copy

declarations

out

of

struts
blank/WEB
INF/web
.
xml

into

your_web_app/WEB-INF/web.xml. Most importantly, copy the
servlet and servlet-mapping entries that map *.do to
org.apache.struts.action.ActionServlet.
27
© 2010 Marty Hall
Sites that Use Struts
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
28
Some Struts Sites: Travel
29
Some Struts Sites: Government
30
Some Struts Sites: Commercial
31
Struts is Mainstream:
Conservatives
and
LiberalsUseIt
Conservatives

and
Liberals

Use

It
32
Summary
• Struts is an MVC framework
– Also a collection of utilities and custom tag libraries
• Struts has significant advantages, but adds
significant complexity
significant

complexity
– Compare the use of Struts to the use of normal MVC
(
with the JSP 2.0 ex
p
ression lan
g
ua
g
e
)
(
p g g )
– Also consider other frameworks, especially JSF 2.0
• Struts is pretty widely used
• Using Struts
– Test by deploying struts-blank.war
– Develop by copying struts-
b
lank or other Struts project
• Don’t make app from scratch; too many changes needed
33
© 2010 Marty Hall
Questions?
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Struts, Ajax, GWT 2.0, Spring, Hibernate, SOAP & RESTful Web Services, Java 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.