Documente Academic
Documente Profesional
Documente Cultură
Struts 2.0
Session - 10
June, 2009
Presentation By
Training & Development
Introduction
2
Introduction
Day 1 – History of Web applications
3
Ms. Natasha
4
Dumbo Vs. Jumbo
if there is a validation error in the XML then will the
validate method of action class be executed?
5
Mr. Thinker
6
Mr. Thinker
7
Mr. Thinker
Expression
8
Dumbo Vs. Jumbo
When is the error message displayed for a validator
type of expression?
9
Mr. Thinker
10
Dumbo Vs. Jumbo
What is the parameter for intercept method?
ActionInvocation
11
Dumbo Vs. Jumbo
In the intercept method what should you do not
break the chain of invocations?
12
Dumbo Vs. Jumbo
What is the name of the parameter which is used to
specify the name of the methods for which the
interceptors should not be invoked?
excludeMethods
13
Mr. Thinker
14
Mr. Thinker
15
Mr. Thinker
16
Mr. Thinker
17
Dumbo Vs. Jumbo
What is the signature of the method?
void prepare( )
18
Dumbo Vs. Jumbo
Which interceptor in turn uses this method
PrepareInterceptor
19
Dumbo Vs. Jumbo
The defaultStack already includes the
PrepareInterceptor. True or false?
True
20
Dumbo Vs. Jumbo
What kind of logic will you include in the prepare
method?
21
Mr. Thinker
22
Mr. Thinker
23
Mr. Thinker
24
Mr. Thinker
25
Dumbo Vs. Jumbo
What is the advantage of having the Model Object?
26
Dumbo Vs. Jumbo
How many model objects will be present in the
Value Stack at any point of time?
27
Ms. Natasha
Let us now learn today’s topic
Today we will be learning about the
various UI tags
28
Type of UI Tags
Data Tags
Control Tags
Component Tags
29
Data Tags – s:property
S:property provides an easy way of writing a property into the rendering
HTML
<s:property value="user.username"/>
30
Data Tags – s:set
The set tag is used to assign a value to a property
You can assign a complex OGNL to a variable with simple name, then keep
referring this simple name instead of the complex OGNL
31
Data Tags – s:set - usage
<s:set name="myName" value="user.username"/>
Hello, <s:property value="#myName"/>. How are you?
In the above case, an object with name myName is created in the default scope
which is Action Context
If you want to change the scope, here is an example
<s:set name="myName" scope=”application”
value="user.username"/>
Hello, <s:property value="#application[‘myName’]"/>. How
are you?
Please note that the above is also an example of retrieving data from within action
context’s various types of objects like application, session etc.
For example, if you had an attribute called USER_NAME in session which stores
the user’s name, you can retrieve it as
<s:property value="#session[‘USER_NAME’]"/>
32
Data Tags – s:push
While the set tag is used to create new references to existing values, push tag
allows you to push properties on to the value stack
This is useful when you want to do a lot of work revolving around a single
object
For example if you keep using user object, then instead of referring it every
time with a complex OGNL, you can push the user object so that it becomes a
top level object. Then referring it would be much simpler
33
Data Tags – s:push – usage
<s:push value="user">
This is the "<s:property value="portfolioName"/>"
portfolio,
created by none other than <s:property
value="username"/>
</s:push>
In the above example, the push tag pushes the user object onto top of the value
stack
The end of the push tag removes the user object from the top of value stack.
Between these two tags you can refer the attributes inside user object directly
34
Control Tags – s:iterator
Widely used
35
Control Tags – s:iterator - usage
<s:iterator value="users" status="stat">
<li>
<s:property value="#stat.index" />
<s:property value="userName"/>
<s:property value="firstName"/>
<s:property value="lastName"/>
<s:property value="age"/>
</li> </s:iterator>
In the above example, the action object exposes a collection of users. The iterator tag
iterates over this set of users
During the body of the tag, the corresponding user is placed on top of the value
stack so that you can refer to the attributes of that particular user object
Please note that there is another object with key stat.This is a convenience object
provided by the framework to get various values
36
Control Tags – s:iterator – IteratorStatus Object
The IteratorStatus object contains the following
– getCount – count of the iterator
– getIndex – the current index
– isEven – whether the current row is even
– isOdd – whether the current row is odd
– isFirst – whether the current row is first
– isLast – whether the current row is Last
You can use these methods just like any other getter methods
37
Control Tags – s:if, s:elseif, s:else
<s:if test="user.age > 35">This user is too old.</s:if>
<s:elseif test="user.age < 35">This user is too
young</s:elseif>
<s:else>This user is just right</s:else>
The test attribute of this tag is the attribute that provides the actual condition
You can just have s:if or all the three or just s:if and s:else
38
Control Tags – s:text
This is used for i18n
Correspondingly there should be an entry in the resource bundle with key as hello
39
Exercise
Create 5 users in the action class. You can hard code them
Take these headings from a resource bundle instead of hard coding them
40
UI Component Tags
Text field
<s:textfield required="true" name="person.myName"
label="My Name is" size="30"/>
Password
<s:password required="true" name="password"
label="password" />
Checkbox
<s:checkbox name="receiveJunkMail" fieldValue="true"
label="Check to receive junk mail” />
Select Box
<s:select name="user.name" list="{'Mike','Payal','Silas'}"
/>
41
UI Component Tags (Contd.)
The above is translated into
<select name="user.name" id="ViewPortfolio_user_name">
<option value="Mike">Mike</option>
<option value="Payal">Payal</option>
<option value="Silas">Silas</option>
</select>
Hidden
<s:hidden name="username" value=”user.name” />
42
How things happen internally
43
Exercise
• You need to create an application for User Account Creation of a
neighborhood pizza store. The following are the details
• Basic Details
• User name
• Password
• First Name
• Last Name
• Gender (Drop down with M and F being the codes)
• Age
• Address Details
• Address1, Address 2, city, Zip
• Miscellaneous
• Agree to terms? – checkbox
• Inform about offers? – checkbox
• On click of submit, provide a success message
44
Session 10
45
Session 10
Thank You !!
46