230 likes | 338 Views
This guide offers a detailed insight into how the Struts framework operates through a user scenario involving home.jsp and time.jsp pages. It covers the fundamental components of Struts, such as action classes, methods, and XML configurations, to illustrate how user interactions trigger specific back-end processes. Readers will learn how to create action objects, invoke methods, manage value stacks, and retrieve properties. This tutorial is designed for developers looking to enhance their knowledge of the Struts framework and its underlying principles.
E N D
How does struts work? Mohammad Seyed Alavi m.s.alavi@ece.ut.ac.ir
Document Map • User Interface • Source File • Memory Space • User
User’s scenario • First, look at this user scenario with home.jsp and time.jsp pages.
Click to show time: Click here Click the link Click to show time: Click here Time is: 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.
Primitives ObjectName Attr=value • Object Attr=value Attr=value ClassName ObjectName • Object maker object maker
Primitives (cont.) Class.Method(args) ObjectName Attr=value • Method call Class.Method(args) ObjectName Attr=value • Method return Return value
2.action class=“ “ 3.action method=“ “ 1 2 2.action=“ “ 3 home.jsp <%@ taglib prefix="s" uri="/struts-tags"%> <a href = ”<s:url action=” ”/>”> Click here </a> Find the action name from “home.jsp” file showTime showTime showTime Click to show time: Find the action class and action method from “struts.xml” Click here struts.xml <action name = “ ” class = “ ” method = “ ”> . . . MyTime MyTime currentTime currentTime
action class=“MyTime“ action method=“currentTime“ 1 2 action=“showTime“ 3 An object of action class will be created if there is not any action object in the value stack There wasn't any action object so make a new one Value Stack MyTime 1 myTime object maker time = null myTime time = null
action class=“MyTime“ action method=“currentTime“ 1 2 action=“showTime“ return value= 3 4 MyTime.currentTime() 1 2 Call the action method myTime time = null Return the return value MyTime.currentTime() myTime time = “10:10:10 pm” “OK” “OK”
action class=“MyTime“ action method=“currentTime“ 1 2 action=“showTime“ return value=“OK” 3 4 Push the action object on top of value stack myTime time = “10:10:10 pm” Value Stack
action class=“MyTime“ action method=“currentTime“ 1 2 action=“showTime“ return value=“OK” field=“ “ 3 4 5 struts.xml <action name=“showTime” …> <result name = “OK”> time.jsp </result> . . . </action> 3 4 Find the target page corresponding to return value from “struts.xml” time.jsp <%@ taglib prefix="s" uri="/struts-tags"%> Time is: <s:property value=” time ”/> Go to the result page time Time is: …………… refresh
action class=“MyTime“ action method=“currentTime“ 1 2 action=“showTime“ return value=“OK” field=“time“ 3 4 5 Retrieve time property from action object Find the object from the top of value stack to initialize the result page (first object that has “time” property) MyTime.getTime() myTime Value Stack 1 5 myTime time = “10:10:10 pm” MyTime.getTime() Time is: …………… myTime refresh entrance_o 10:10:10 pm time = “10:10:10 pm” “10:10:10 pm” Name = “Mohammad”
action class=“MyTime“ action method=“currentTime“ 1 2 action=“showTime“ return value=“OK” 3 4 Click the refresh link Time is: 10:10:10 pm refresh
2.action class=“ “ 3.action method=“ “ 1 2 2.action=“ “ 3 time.jsp <%@ taglib prefix="s" uri="/struts-tags"%> <a href = ”<s:url action=” ”/>”> refresh </a> Find the action name refreshTime refreshTime refreshTime from “time.jsp” file Find the action class and action method from “struts.xml” struts.xml <action name = “ ” class = “ ” method = “ ”> . . . MyTime MyTime refresh refresh
action class=“MyTime“ action method=“refresh“ 1 2 action=“refreshTime“ 3 New request So, new value stack There wasn't any object so make a new one Value Stack MyTime 1 myTime object maker time = null myTime time = null
action class=“MyTime“ action method=“refresh“ 1 2 action=“refreshTime“ return value= 3 4 MyTime.refresh() 1 2 Call the action method myTime time = null Return the return value MyTime.refresh() myTime time = “10:11:11 pm” “OK” “OK”
action class=“MyTime“ action method=“refresh“ 1 2 action=“refreshTime“ return value=“OK” 3 4 Push the new object on top of value stack myTime time = “10:11:11 pm” Value Stack
action class=“MyTime“ action method=“refresh“ 1 2 action=“refreshTime“ return value=“OK” field=“ “ 3 4 5 struts.xml <action name=“refreshTime” …> <result name = “OK”> time.jsp </result> </action> 3 Find the target page 4 corresponding to return value from “struts.xml” time.jsp <%@ taglib prefix="s" uri="/struts-tags"%> Time is: <s:property value=” time ”/> Find the name’s field Go to the result page time Time is: …………… refresh
action class=“MyTime“ action method=“refresh“ 1 2 action=“refreshTime“ return value=“OK” field=“time“ 3 4 5 Retrieve time property from action object Find the object from the top of value stack to initialize the result page (first object that has “time” property) MyTime.getTime() myTime 1 Value Stack 1 5 myTime time = “10:11:11 pm” MyTime.getTime() Time is: …………… myTime refresh entrance_o 10:11:11 pm time = “10:11:11 pm” “10:11:11 pm” Name = “Mohammad”
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 is. m.s.alavi@ece.ut.ac.ir