First struts program using WSAD 5

minceillusionInternet και Εφαρμογές Web

30 Ιουλ 2012 (πριν από 5 χρόνια και 1 μήνα)

269 εμφανίσεις

Struts

Purpose: Provides information on how to create struts and tiles projects in WASD 5.1.2.
Document is useful for practical purpose for theoretical information go through the books
in References and Books section:

Prerequisites:

1)

Go

through the basic de
finitions of action
class,

action
forward, struts
-
config etc
.

2)

Basic WASD, jsp, servlet concepts.

References and Books

(free PDF):

http://www.theserverside.com/tt/b
ooks/sourcebeat/JakartaStrutsLive/index.tss


First struts program using WSAD 5.1.2

1)

file
-
>new
-
>dynamic web project
-
> give project name and check “configure
Advanced options” click on next

( my project name is strutsProject2)

2)

Click on next.

3)

Check “Add Stru
ts support” and “jsp tag libraries” (WASD will automatically
copy the required jar files and set up the struts configurations ) .Click next



4)

Click next.

5)

Click finish.


INFO

The project structure and web.xml was created by the editor. Now we need to creat
e a
jsp which just displays a message “hello world. Welcome to my site”.


For that we need welcomeAction.java , welcome.jsp,index.jsp

Index.jsp:
(webcontent/index.jsp)This is called by the server when we try to access
the application like

(
http://localhost:9080/strutsProject2
) .

This is because the web.xml will declare welcome list like below by default(WASD)


<
welcome
-
file
-
list
>



<
welcome
-
file
>
index.html
</
welcome
-
file
>



<
welcome
-
file
>
index.htm
</
welco
me
-
file
>



<
welcome
-
file
>
index.jsp
</
welcome
-
file
>



<
welcome
-
file
>
default.html
</
welcome
-
file
>



<
welcome
-
file
>
default.htm
</
welcome
-
file
>



<
welcome
-
file
>
default.jsp
</
welcome
-
file
>


</
welcome
-
file
-
list
>

From this jsp we

are
forward
ing the control

to another

jsp welcome.jsp which is
under webcontent/jsp through welcomeAction.



Create action class

1)right click the java resources folder
-
>new
-
>other
-
>Web(Struts)
-
>Action class
-
>next

2) give action class name
-
>next



3) finish.


INFO

Go to welcomeAction.java .
we should see execute method with try/catch blocks

That method can be replaced like the below as we don’t need try/catch blocks for this
example



public

ActionForward

execute(



ActionMapping

mapping,



ActionForm

form,



HttpServletRequest

request,



Htt
pServletResponse

response)



throws

Exception

{




ActionErrors

errors

=

new

ActionErrors();



ActionForward

forward

=

new

ActionForward();

// return
value






forward

=

mapping.findForward(
"
welcomeForward
"
);





return

(forward);



}

Change the forward n
ame to welcomeForward

and we need to update struts config to declare
the forward name.


Create index.jsp

1)under webcontent create a new jsp file with name index.jsp

The jsp looks like below

<%@
taglib
uri
=
"/WEB
-
INF/struts
-
logic.tld"
prefix
=
"logic"
%>

<
jsp
:forward
page
=
"/welcome.do?dispatcher=welcomeMethod"
/>

</
html:html
>


Create welcome.jsp

Create a
new folder jsp under webContent and create new jsp file welcome.jsp like
this


<!
DOCTYPE
HTML
PUBLIC
"
-
//W3C//DTD HTML 4.01 Transitional//EN"
>

<%@
taglib
uri
=
"/WEB
-
INF/struts
-
html.tld"
prefix
=
"html"
%>

<%@
taglib
uri
=
"/WEB
-
INF/struts
-
bean.tld"
prefix
=
"bean"
%>

<
html:html
>

<
BODY
>

Hello World.Welcome to my site.


</
BODY
>

</
html:html
>


Change the struts
-
config.xml
like this. We are mapping the action forward
“wel
comeForward” to “/jsp/Welcome.jsp”

<
action
path
=
"/welcome"
type
=
"com.lavanya.strutsproject2.actions.welcomeAction"
>

<
forward
name
=
"welcomeForward"
path
=
"/jsp/welcome.jsp"
/>

</
action
>


Finally restart the server and click on

run on server


by right clickin
g the project
. The
output will look like



What is happening when we click on
http://localhost:9080/strutsProject2

?


1)Go to WEB.xml

The controller first goes to web.xml as usual and dispatches to ind
ex.jsp

2) Go to index.jsp

Here we are forwarding the control to
/welcome.do?dispatcher=welcomeMethod"

3) Go to Web.xml again

In web.xml the controller will find only one servlet that is ActionServlet

with url
-
pattern
.do
. So it is forwarded to ActionServl
et to dispatch the request to jsp.

<
servlet
-
name
>
action
</
servlet
-
name
>

<
servlet
-
class
>
org.apache.struts.action.ActionServlet
</
servlet
-
class
>

<
init
-
param
>

<
param
-
name
>
config
</
param
-
name
>


<
param
-
value
>
WEB
-
INF/struts
-
config.xml
</
param
-
value
>

</
init
-
param
>

<
u
rl
-
pattern
>
*.do
</
url
-
pattern
>


4
) Go to Struts
-
config.xml

<
action
path
=
"/welcome"
type
=
"com.lavanya.strutsproject2.actions.welcomeAction"
>

<
forward
name
=
"welcomeForward"
path
=
"/jsp/welcome.jsp"
/>

</
action
>

Find the action mapping for “welcome” and enter th
e welcomeAction.java class

5) Go to welcomeAction.java

In the action class we only had an execute method so that method will get executed and
finally forwarded the control to “welcomeForward”

6) The control will go the struts config and finds the forward
name”welcomeForward”
under welcome action mapping and forwards to welcome.jsp



Struts + Tiles

+ DispatcherAction


References and Books (free PDF) :

http://www.lali
luna.de/tutorial/first
-
tiles/first_struts_tiles_tutorial.pdf

http://www.javaworld.com/javaworld/jw
-
01
-
2002/jw
-
0104
-
tilestrut.html

http://www2.lifl.fr/~dumoulin/tiles/




First tiles program (enhancement to previous project “strutsProject2”):


We are going to display a webpage

with header and footer




We need tiles
-
definitions.xml, header.jsp,

footer.jsp,

body.jsp,

BasicLayout.jsp


JSP changes:

Create header.jsp

Header.jsp just contains a titlebar .(I choose google logo for that). The code looks like

<%@
taglib
uri
=
"/WEB
-
INF/struts
-
html.tld"
prefix
=
"html"
%>

<
html:html
>

<
HEAD
>

<
TITLE
></
TITLE
>

</
HEAD
>

<
body
>

<
img
src
=
"/strutsProject/images/mothers_day06.gif"
border
=
"0"
>

</
body
>

</
html:html
>


Create Footer.jsp

It will generally contain the copyrights info, footer links, etc(I choose another google
logo) the code looks like



<%@
taglib
uri
=
"/WEB
-
INF/struts
-
html.tld"
pre
fix
=
"html"
%>

<
html:html
>

<
HEAD
>

<
TITLE
></
TITLE
>

</
HEAD
>

<
BODY
>

<
img
src
=
"/strutsProject/images/persian_newyear06.gif"
>

</
BODY
>

</
html:html
>


Create body.jsp.

It is just a Place holder which we are going to replace with our jsps.


<
BODY
>

hello

</
BODY
>


Cr
eate layout

basicLayout.jsp

The layout defines the page with header as header.jsp , footer as footer.jsp etc


<%@
taglib
uri
=
"/WEB
-
INF/struts
-
tiles.tld"
prefix
=
"tiles"
%>

<
tiles:insert
attribute
=
"header"
/>

<
br
>

<
tiles:insert
attribute
=
"body"
/>

<
br
>

<
tiles
:insert
attribute
=
"footer"
/>


Modifications to welcome.jsp

<!
DOCTYPE
HTML
PUBLIC
"
-
//W3C//DTD HTML 4.01 Transitional//EN"
>

<%@
taglib
uri
=
"/WEB
-
INF/struts
-
html.tld"
prefix
=
"html"
%>

<%@
taglib
uri
=
"/WEB
-
INF/struts
-
bean.tld"
prefix
=
"bean"
%>

<
html:html
>

<
HE
AD
>

<%@
page

language
=
"java"

contentType
=
"text/html; charset=ISO
-
8859
-
1"

pageEncoding
=
"ISO
-
8859
-
1"

%>

<
META
http
-
equiv
=
"Content
-
Type"
content
=
"text/html; charset=ISO
-
8859
-
1"
>

<
META
name
=
"GENERATOR"
content
=
"IBM WebSphere Studio"
>

<
META
http
-
equiv
=
"Content
-
Style
-
Type"
content
=
"text/css"
>

<
LINK
href
=
"../theme/Master.css"
rel
=
"stylesheet"


type
=
"text/css"
>

<
TITLE
></
TITLE
>

</
HEAD
>


<
BODY
>

<
html:form
action
=
"
/welcome.do?dispatcher=helloUserMethod">

Welcome to my site.Please enter your name

<
html:text
property
=
"
user"
name
=
"welcomeForm"
></
html:text
>

<
html:submit
></
html:submit
>

</
html:form
>

</
BODY
>

</
html:html
>

1) here we are giving the dispatcher name as helloUserMethod so that if we submit the
page it will go to helloUserMethod and from there it will forwarded to

helloUSer.jsp.

Add hello.jsp


<!
DOCTYPE
HTML
PUBLIC
"
-
//W3C//DTD HTML 4.01 Transitional//EN"
>

<%@
taglib
uri
=
"/WEB
-
INF/struts
-
html.tld"
prefix
=
"html"
%>

<%@
taglib
uri
=
"/WEB
-
INF/struts
-
bean.tld"
prefix
=
"bean"
%>

<
html:html
>

<
HEAD
>

<%@
page

language
=
"java
"

contentType
=
"text/html; charset=ISO
-
8859
-
1"

pageEncoding
=
"ISO
-
8859
-
1"

%>

<
META
http
-
equiv
=
"Content
-
Type"
content
=
"text/html; charset=ISO
-
8859
-
1"
>

<
META
name
=
"GENERATOR"
content
=
"IBM WebSphere Studio"
>

<
META
http
-
equiv
=
"Content
-
Style
-
Type"
content
=
"text/c
ss"
>


<
TITLE
></
TITLE
>

</
HEAD
>


<
BODY
>

<
P
>
Hello
<
bean:write
property
=
"user"
name
=
"welcomeForm"
/>

.Welcome to
my site
</
P
>

</
BODY
>

</
html:html
>

1)

here we are displaying the name of the user entered in previous page by using the
form bean
. This will help

a lot

i
f we have so many input fields on the screen.

2)

formbean was declared in struts config in such a way that it will exist in request
scope.

3)

If we do not use form bean then we have to manually place the name in request
before forwarding to app page and on the p
age we need to display the name from
request object.





Change to index.jsp

<%@
taglib
uri
=
"/WEB
-
INF/struts
-
logic.tld"
prefix
=
"logic"
%>

<
jsp:forward
page
="/welcome.do?dispatcher=welcomeMethod
"
/>

</
html:html
>


1) The dispatcher name is welcomeMethod so

the control goes to this method in action
class


Action class and action form changes

Change welcomeAction


package

com..strutsproject.actions;


import

javax.servlet.http.HttpServletRequest;

import

javax.servlet.http.HttpServletResponse;

import

org.apach
e.struts.action.Action;

import

org.apache.struts.action.ActionError;

import

org.apache.struts.action.ActionErrors;

import

org.apache.struts.action.ActionForm;

import

org.apache.struts.action.ActionForward;

import

org.apache.struts.action.ActionMapping;

imp
ort

org.apache.struts.actions.DispatchAction;


import

com..strutsproject.forms.WelcomeForm;


/**


*

@version


1.0


*

@author


*/

public

class

WelcomeAction

extends

DispatchAction

{



public

ActionForward

welcomeMethod(



ActionMapping

mapping,



ActionForm

form,



HttpServletRequest

request,



HttpServletResponse

response)



throws

Exception

{



ActionErrors

errors

=

new

ActionErrors();



ActionForward

forward

=

new

ActionForward();





WelcomeForm

wForm=(WelcomeForm)form;



forward

=

mapping.findForwa
rd(
"welcomeForward"
);



return

(forward);


}


public

ActionForward

helloUserMethod(




ActionMapping

mapping,




ActionForm

form,




HttpServletRequest

request,




HttpServletResponse

response)




throws

Exception

{




ActionErrors

errors

=

new

ActionError
s();




ActionForward

forward

=

new

ActionForward();






forward

=

mapping.findForward(
"helloUserForward"
);




return

(forward);



}

}


Here we are extending DispatchAction instead of Action class. So that we can write our
own methods (other than execute)

and call appropriate method from jsp based on the
action(button clicks).

WelcomeMethod is same as previous one

helloUserMethod is called when user click on submit button from welcome.jsp page and
forwards it to helloUser.jsp


Create WelcomeForm.java

packa
ge

com..strutsproject.forms;


import

javax.servlet.http.HttpServletRequest;

import

org.apache.struts.action.ActionErrors;

import

org.apache.struts.action.ActionForm;

import

org.apache.struts.action.ActionMapping;


/**


*

Form

bean

for

a

Struts

application.


*

Users

may

access

1

field

on

this

form:


*

<ul>


*

<li>
user

-

[your

comment

here]


*

</ul>


*

@version


1.0


*

@author


*/

public

class

WelcomeForm

extends

ActionForm

{


private

String

user

=

null
;


public

String

getUser()

{



return

user;


}


public

vo
id

setUser(String

u)

{



this
.user

=

u;


}

public

void

reset(ActionMapping

mapping,

HttpServletRequest request)

{


user

=

null
;


}


public

ActionErrors

validate(



ActionMapping

mapping,



HttpServletRequest

request)

{



ActionErrors

errors

=

new

ActionErr
ors();



// Validate the fields in your form, adding



// adding each error to this.errors as found, e.g.




// if ((field == null) || (field.length() == 0)) {



// errors.add("field", new
org.apache.struts.action.ActionError("error.field.required"));



// }



return

errors;



}

}

In the validate method we can validate the name entered on the screen

We can return errors to same page by giving input=”definition name” in struts config


Create tile defintions file under WEB
-
INF.

This file contains tiles wit
h tile associated with parameters and identified by a name
which we can use in jsps or directly give them in struts config as action forwards instead
of jsp urls.

The tiles definition looks like this
( tiles
-
definitions.xml)

<?
xml
version
=
"1.0"
encoding
=
"UT
F
-
8"
?>

<!
DOCTYPE
tiles
-
definitions
PUBLIC

"
-
//Apache Software Foundation//DTD Tiles Configuration//EN"

"http://jakarta.apache.org/struts/dtds/tiles
-
config.dtd"
>

<
tiles
-
definitions
>


<
definition
name
=
"basic.Def"
path
=
"/jsp/BasicLayout.jsp"
>



<
put
name
=
"he
ader"
value
=
"/jsp/header.jsp"
/>



<
put
name
=
"footer"
value
=
"/jsp/footer.jsp"
/>



<
put
name
=
"body"
value
=
"/jsp/body.jsp"
/>





</
definition
>




<
definition
name
=
"welcome.user"
extends
=
"basic.Def"
>





<
put
name
=
"body"
value
=
"/jsp/welcome.jsp"
/>


</
definiti
on
>


<
definition
name
=
"hello.user"
extends
=
"basic.Def"
>



<
put
name
=
"body"
value
=
"/jsp/hello.jsp"
/>


</
definition
>


</
tiles
-
definitions
>

Basic.Def is basic tile definition which we can extend to other definitions and override
the body.jsp or header.jsps.
if we extend this definition the new definition also gets the
same layout and offcourse we can add some more tiles like menu.jsp or change the body
content to different jsp other than body.jsp

Welcome.user and hello.user are two examples of tiles definiti
ons which are extending
the basic.Def.
welcome.user is definition for welcome.jsp and hello.user is definition for
hellUser.jsp


Changes in struts
-
config.xml

<?
xml
version
=
"1.0"
encoding
=
"UTF
-
8"
?>

<!
DOCTYPE
struts
-
config
PUBLIC
"
-
//Apache Software Foundati
on//DTD
Struts Configuration 1.1//EN"


"http://jakarta.apache.org/struts/dtds/struts
-
config_1_1.dtd"
>


<
struts
-
config
>


<!
--

Form Beans
--
>


<form
-
beans>



<form
-
bean name="welcomeForm"
type="com..strutsproject.forms.WelcomeFo
rm">



</form
-
bean>


</form
-
beans>




<!
--

Global Forwards
--
>


<
global
-
forwards
>




<
forward
name
=
"welcome"
path
=
"/welcome.do"
/>



</
global
-
forwards
>



<!
--

Action Mappings
--
>


<
action
-
mappings
>



<
action
path
=
"/welcome"
type
=
"com..strutsproject.actions
.WelcomeAction"
name="welcomeForm"

scope
=
"request"
parameter
=
"dispatcher"
>



<
forward
name
=
"welcomeForward"
path
=
"welcome.user"
/>



<
forward
name
=
"helloUserForward"
path
=
"hello.user"
/>



</
action
>



<
action
path
=
"/base"
type
=
"com..strutsproject.actions.Bas
eAction"
>



</
action
>


</
action
-
mappings
>

<controller
processorClass="org.apache.struts.tiles.TilesRequestProcessor" />


<!
--

Message Resources
--
>


<
message
-
resources
parameter
=
"com..strutsproject.resources.ApplicationResources"
/>

<plug
-
in className="org.
apache.struts.tiles.TilesPlugin">

</plug
-
in>

</
struts
-
config
>


1) The action /welcome now has two forwards declared and pointing to appropriate tiles


2) Declare the controller as tilesRequestProcessor

3) Insert the tiles plugin.

4) define the form bean an
d connect the form with action class

5) the parameter=”dispatcher” is used to dispatch request to different methods in action
class based on the dispatcher value given on jsp.


Changes to web.xml

<
init
-
param
>


<
param
-
name
>
definitions
-
config
</
param
-
name
>


<
param
-
value
>
/WEB
-
INF/tiles
-
definitions.xml
</
param
-
value
>

</
init
-
param
>

Define the parameter defintions
-
config ( we can even change the file name tiles
-
definitions.xml to another but that should be same as what we are giving here in
web.xml)



Resta
rt the server and run on the server

The out put looks like this