How does struts work?
Mohammad Seyed [email protected]
•User Interface
•Source File
•Memory Space
•User
Document Map
User’s scenario
First, look at this user scenario with home.jsp and time.jsp pages.
Time is:
Click here
Click to show time:
Click here
Click to show time:
10:10:10 pm
refresh
Click the refresh link Time is: 10:10:10 pm
refresh
Time is: 10:11:11 pm
refresh
How does struts work?
Now, we want to show you how struts works in the background of this scenario.
But first, you must be familiar with some primitive symbols that will helps you through this tutorial.
ObjectName
Attr=value
Primitives
Attr=valueAttr=value
•Object
objectmaker
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.
<%@ taglib prefix="s" uri="/struts-tags"%>
<a href = ”<s:url action=” ”/>”>Click here
</a>
<action name = “ ”
class = “ ”
method = “ ”>...
Find the action namefrom “home.jsp” file
2.action class=“ “ 3.action method=“ “1 2
2.action=“ “3
MyTime
currentTime
MyTime
currentTime
home.jsp
struts.xml
showTimeshowTime
Find the action class and action method from “struts.xml”
showTime
Click here
Click to show time:
Value Stack
An object of action classwill be created if there is not any action object in the value stack
objectmaker
MyTime
myTime
myTime
There wasn't any action object so make a new one
1
action class=“MyTime“ action method=“currentTime“1 2
action=“showTime“3
time = null
time = null
Call the action methodmyTime
MyTime.currentTime()
myTime
MyTime.currentTime()
“OK”
1 2
return value= 4
“OK”
action class=“MyTime“ action method=“currentTime“1 2
action=“showTime“3
time = “10:10:10 pm”
time = null
myTime
time = “10:10:10 pm”
Value Stack
Push the action object ontop of value stack
return value=“OK”4
action class=“MyTime“ action method=“currentTime“1 2
action=“showTime“3
<action name=“showTime” …> <result name = “OK”>
time.jsp </result>
.
.
.</action>
Find the target page
corresponding to return value from “struts.xml”
34
struts.xml
return value=“OK”4
action class=“MyTime“ action method=“currentTime“1 2
action=“showTime“3
Time is: ……………
refresh
<%@ taglib prefix="s" uri="/struts-tags"%>
Time is:<s:property value=” time ”/>
time.jsp
time
field=“ “5
return value=“OK”4
action class=“MyTime“ action method=“currentTime“1 2
action=“showTime“3
Value Stack
Find the object from
the top of value stackto initialize the resultpage (first object that has “time” property)
entrance_o
Name = “Mohammad”
myTime
myTime
time = “10:10:10 pm”
MyTime.getTime()
myTime
time = “10:10:10 pm”
MyTime.getTime()
“10:10:10 pm”
1
Time is: ……………
refresh10:10:10 pm
field=“time“5
5
Retrieve time property from action object
return value=“OK”4
action class=“MyTime“ action method=“currentTime“1 2
action=“showTime“3
Click the refresh link
Time is: 10:10:10 pm
refresh
<%@ taglib prefix="s" uri="/struts-tags"%>
<a href = ”<s:url action=” ”/>”>refresh
</a>
<action name = “ ”
class = “ ”
method = “ ”>...
Find the action name
from “time.jsp” file
2.action class=“ “ 3.action method=“ “1 2
2.action=“ “3
MyTime
refresh
MyTime
refresh
time.jsp
struts.xml
refreshTimerefreshTime
Find the action class and action method from “struts.xml”
refreshTime
Value Stack
New request
So, new value stack
objectmaker
MyTime
myTime
myTime
There wasn't any object so make a new one
1
action class=“MyTime“ action method=“refresh“1 2
action=“refreshTime“3
time = null
time = null
Call the action methodmyTime
MyTime.refresh()
myTime
MyTime.refresh()
“OK”
1 2
return value= 4
“OK”
action class=“MyTime“ action method=“refresh“1 2
action=“refreshTime“3
time = “10:11:11 pm”
time = null
myTime
time = “10:11:11 pm”
Value Stack
Push the new object
return value=“OK”4
action class=“MyTime“ action method=“refresh“1 2
action=“refreshTime“3
on top of value stack
<action name=“refreshTime” …> <result name = “OK”>
time.jsp </result></action>
Find the target page
corresponding to return value from “struts.xml”
4
3
struts.xml
return value=“OK”4
action class=“MyTime“ action method=“refresh“1 2
action=“refreshTime“3
Time is: ……………
refresh
<%@ taglib prefix="s" uri="/struts-tags"%>
Time is:<s:property value=” time ”/>
time.jsp
time
Find the name’s field
field=“ “5
Value Stack
Find the object from
the top of value stackto initialize the resultpage (first object that has “time” property)
entrance_o
Name = “Mohammad”
myTime
myTime
time = “10:11:11 pm”
MyTime.getTime()
myTime
time = “10:11:11 pm”
MyTime.getTime()
“10:11:11 pm”
1
return value=“OK”4
action class=“MyTime“ action method=“refresh“1 2
action=“refreshTime“3
Time is: ……………
refresh10:11:11 pm
1
field=“time“5
5
Retrieve time propertyfrom action object
If there is some dark points in your mind, look at the second scenario!
If not, please report me about how much useful this document [email protected]