>> Delivering Value

Session - 10
June, 2009 Presentation By Training & Development

Struts 2.0

Introduction
Good Evening everybody! Welcome to the Day 10 of the Struts 2 training course

2

Introduction
 Day 1 – History of Web applications
It will be another exciting day of learning. Let us now do a quick recap on what we have learned so far:

 Day 2 – Concepts to be known before

learning Struts 2

 Day 3 – Struts 2 environment setup, Hello

World Project, PhD Project Stack, OGNL

 Day 4 – Dissection of Hello World, Value  Day 5 – Action Interface, Action Support

class, Validation, Resource Bundle and Internationalization Struts 2

 Day 6 – Validation Framework provided by  Day 7 – Writing your own interceptors  Day 8 – Action Context, accessing Session

and SessionAware interface Interfaces

 Day 9 – Preparable and ModelDriven

3

Ms. Natasha
Let us now let Thinker, Dumbo and Jumbo refresh your memory by providing reviews of important points over the past week

4

Dumbo Vs. Jumbo
if there is a validation error in the XML then will the validate method of action class be executed?

Yes, it will be executed because it is always better to display the errors at one go to the user

5

Mr. Thinker
What should be the name of the validation xml if I have just one business method inside the action class? <action class>validation.xml

6

Mr. Thinker
What should be the name of the validation xml if I have more than one business method inside the action class and I want various validations for various business methods?

<action class>-<action name>-validation.xml

7

Mr. Thinker
What is the validator type to be used for complex validations? Expression

8

Dumbo Vs. Jumbo
When is the error message displayed for a validator type of expression?

If the expression mentioned inside param returns false

9

Mr. Thinker
If your validation is not inside the field element, where is the error message displayed in the page?

The error message is displayed as action error under the page scope

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?

You should call the method actionInvocation.invoke( )

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
What is the name of the parameter which is used to specify the name of the methods for which the interceptors should be invoked? includeMethods

14

Mr. Thinker
How do you pass parameters to an interceptor? Using the param element

15

Mr. Thinker
How can I pass different values for the same parameter in different actions? Use param name=”interceptor.para mname” instead of just parameter rname

16

Mr. Thinker
How many methods does the Preparable Interface provide? One

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 PrepareInterceptor. True or false? includes the

True

20

Dumbo Vs. Jumbo
What kind of logic will you include in the prepare method?

We will include preparatory logic in this method (Eg. Preparing the model object by getting the fields values from the session/DB and setting in the model object etc)

21

Mr. Thinker
What is the name of the interface used to create a Model Object in the value stack? Model Driven

22

Mr. Thinker
What is the method exposed by this interface? Object getModel( )

23

Mr. Thinker
What is the name of the interceptor that uses this interface? ModelDriven Interceptor

24

Mr. Thinker
The defaultStack does not include ModelDrivenInterceptor. True or false? False. The defaultStack includes this interceptor

25

Dumbo Vs. Jumbo
What is the advantage of having the Model Object?

You can directly start referring from inside the model object so the ognl will be less complex. The other thing is that it is stored similar to the action object (separately) in the value stack

26

Dumbo Vs. Jumbo
How many model objects will be present in the Value Stack at any point of time?

Only one, since there can be only one action object at any time. One action object can contain only one model object

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  In simple programming language terms it is similar to assigning a value to a

variable

 In Struts terms the simplest reason to use this tag is for convenience  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. the user’s name, you can retrieve it as

 For example, if you had an attribute called USER_NAME in session which stores
 <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 object

 This is useful when you want to do a lot of work revolving around a single  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  You use iterator tag to loop over collections of objects easily  It can be used to loop over Collection, Map, Enumeration, Iterator or array

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 provided by the framework to get various values

 Please note that there is another object with key stat.This is a convenience object

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  The usage is pretty simple <s:text name="hello"></s:text>  Correspondingly there should be an entry in the resource bundle with key as hello  The corresponding value will be rendered in the HTML

39

Exercise
 Create 5 users in the action class. You can hard code them  In the JSP, display these 5 users following data in a tabular form – S.No – First Name – Last Name – Age – Occupation  Take these headings from a resource bundle instead of hard coding them  If there are no elements in the user collection, then display a

corresponding error message

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>  Select Box – Dynamic data in drop down <s:select name="username" list='users' listKey="username" listValue="username" label="Select an artist" />  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
We hope you thoroughly enjoyed today’s session. Have a good evening and we will meet you in the next session

45

Session 10
Thank You !!

46

Sign up to vote on this title
UsefulNot useful