Struts 2 Second Scenario

Post on 27-May-2015

930 views 1 download

Tags:

Transcript of Struts 2 Second Scenario

How does struts work?

Mohammad Seyed Alavim.s.alavi@ece.ut.ac.ir

•User Interface

•Source File

•Memory Space

•User

Document Map

User’s scenario

First, look at this user scenario with the entrance and welcome page.

Name:

Submit

Name:

Submit

Name:

Submit

Mohammad

MohammadMohammad

Name:

Submit

Incorrect name

Name:

Submit

Incorrect name

Hamed

Correct the name& trigger the actionMohammad

Welcome

………………Hamed

How does struts work?

Now, we want to show you how struts works in the background of this user scenario.

But first, you must be familiar with some primitive symbols that will help you through this tutorial.

ObjectName

Attr=value

Primitives

Attr=valueAttr=value

•Object

object maker

ClassNameObjectName

•Object maker

Primitives (cont.)

ObjectName

Attr=value

Class.Method(args)

Return value

•Method return

ObjectName

Attr=value

Class.Method(args)

•Method call

Now, let’s start it.

Name:

Submit

Name:

Submit

Name:

Submit

Mohammad

Mohammad

name =“ ”

Mohammad

1

<%@ taglib prefix="s" uri="/struts-tags"%>

<s:textfield name=” name ”...

entrance.jspFind the nam

e’s fieldname

2 field=“ ”

<s:form action = “ ” . . .

<action name = “ ”

class = “ ”

method = “ ”>...

Find the action name

from “entrance.jsp” file

1.name=“Mohammad” 2.action class=“ “

3.action method=“ “

1 3

52.action=“ “4

submit

Entrance

signIn

submit

Entrance

signIn

entrance.jsp

struts.xml

submit

1.field=“name”2

Value Stack

An object of action classwill be created if there is not any of that in value stack

objectmaker

Entrance

entrance

entrance

name = “Mohammad”

There wasn't any object so make new one

entrance_o

Name = “Mohammad”

entrance

Entrance.setName(“Mohammad”)

entrance

name = “Mohammad”

Entrance.setName(“Mohammad”)

void

call the setter

name=“Mohammad” action class=“Entrance“

3.action method=“signIn“

1 3

52.action=“submit“4

3

12

field=“name”2

3

Call the action methodentrance

name = “Mohammad”

Entrance.signIn()

entrance

name = “Mohammad”

Entrance.signIn()

“No”

name=“Mohammad” action class=“Entrance“

action method=“signIn“

1 3

52.action=“submit“4

3 5

return value= 6

“No”

field=“name”2

entrance

name = “Mohammad”

Value Stack

Push the object on top ofvalue stack

name=“Mohammad” action class=“Entrance“

action method=“signIn“

1 3

52.action=“submit“4 return value=“No”6

field=“name”2

<action name=“submit”> <result name = “No”>

main.jsp?msg = “Incorrect name” </result>

.

.

.</action>

Find the target page

corresponding to return value from “struts.xml”

Name:

Submit

Incorrect name

name=“Mohammad” action class=“Entrance“ action method=“signIn“1 2 3

action=“submit“4 return value=“No”5

4

5

struts.xml

field=“ ”6

<%@ taglib prefix="s" uri="/struts-tags"%>

Name:<s:textfield name=” name ”...

entrance.jsp

Find the name’s field

name

Value Stack

Find the object from

the top of value stackto initialize the resultpage (first object that has “name” property)

entrance_o

Name = “Mohammad”

entrance

entrance

name = “Mohammad”

Entrance.getName()

entrance

name = “Mohammad”

Entrance.getName()

“Mohammad”

Name:

Submit

Incorrect name

Mohammad

Name:

Submit

Incorrect name

HamedCorrect the nam

e

& trigger the action

name=“ ”

action class=“Entrance“ action method=“signIn“

5

2 3

action=“submit“4

Hamed

2 1

field=“name”1

Retrieve name propertyfrom action object

1.name=“Hamed”1 field=“ ”2

<%@ taglib prefix="s" uri="/struts-tags"%>

Name:<s:textfield name=” name ”...

entrance.jsp

name

Find the name’s field

<s:form action = “ ” . . .

<action name = “ ”

class = “ ”

method = “ ”>...

Find the action name

from “entrance.jsp” file

1.name=“Hamed” 2.action class=“ “

3.action method=“ “

1 3

52.action=“ “4

submit

Entrance

signIn

submit

Entrance

signIn

entrance.jsp

struts.xml

submit

field=“name”2

Value Stack

New Request

So, new value stack

objectmaker

Entrance

entrance

entrance

name = “Hamed”

There wasn't any object so make new one

entrance_o

Name = “Mohammad”

entrance

Entrance.setName(“Hamed”)

entrance

name = “Hamed”

Entrance.setName(“Hamed”)

void

call the setter

name=“Hamed” action class=“Entrance“

3.action method=“signIn“

1 3

52.action=“submit“4

3

12

field=“name”2

3

Call the action methodentrance

name = “Hamed”

Entrance.signIn()

entrance

name = “Hamed”

Entrance.signIn()

“Yes”

1.name=“Hamed” action class=“Entrance“

action method=“signIn“

1 3

5 action=“submit“4 return value= 6

“Yes”

3 5

field=“name”2

entrance

name = “Hamed”

Value Stack

Push the new object

name=“Hamed” action class=“Entrance“

action method=“signIn“

1 3

5 action=“submit“4 return value=“Yes”6

on top of value stack

field=“name”2

<action name=“submit”> <result name = “No”> … </result> <result name = “Yes”>

welcome.jsp </result></action>

Find the target page

corresponding to return value from “struts.xml”

Welcome

………………

5

name=“Hamed” action class=“Entrance“ action method=“signIn“1 2 3

action=“submit“4 return value=“Yes”5

4

struts.xml

<%@ taglib prefix="s" uri="/struts-tags"%>

Welcome<s:property value=” name ”...

welcome.jsp

name

field=“ ”6

Value Stack

Find the object from

the top of value stackto initialize the resultpage (first object that has “name” property)

entrance_o

Name = “Mohammad”

entrance

entrance

name = “Hamed”

Entrance.getName()

entrance

name = “Hamed”

Entrance.getName()

“Hamed”

Welcome

………………Hamed

1.name=“Hamed” action class=“Entrance“ action method=“signIn“1 2 3

action=“submit“4

2

field=“name”5

5

Retrieve name property from action object